In service class of a REST API, I am receiving a list of roll no and mobile no. I am fetching student details by sending roll no. from repository. For each student I want to update mobile no corresponding to the roll no in the sequence I received.
Service class
public List<Student> updateContactNoOfStudents (List<Long> rollNo, List<String> ContactNo)
{
List <Student> studentList = new ArrayList<>();
try{
studentList.add(studentRepository.fetchStudentListForUpdatingContactNo(rollNo));
studentList.forEach((student->{
student.setContactNo(contactNo.get(0));
studentRepository.storeStudent(student);
}));
}catch (StudentException StudentException){
Logger.error("Updation of Contact No Failed : {}",studentException);
}
return studentList;
}
Above code is not working as expected, What change shall I do, so that. For each student contact no gets updated in the sequence I received for corresponding roll No.
Controller class
@PostMapping (value = "/updateContactNo/")
public ResponseEntity <List<Student>> FetchingStudentForUpdatingContactNo (@RequestBody StudentUpdateDto studentupdateDto){
try{
return ResponseREntity.ok().body(studentService.updateContactNoOfStudents(studentUpdatDto.getRollNo(), studentUpdatDto.getContactNo()));
}catch (Exception e){
throw new StudentException("Exception in fetching Student List for updating contact no",e);
}
}