1
[I want this function to work, i.e. change a lot of data at once][1]

I want to change a lot of data at once, how?

Under this DAO to change just one data, what if I want to change a lot?

    @PutMapping("/roomstatus/update/{id}")
public ResponseEntity<RoomStatus> updatRoomStatus(@PathVariable(value="id") Integer empid,@Valid @RequestBody RoomStatus RsDetails){

    RoomStatus emp=roomStatusDAO.findOne(empid);
    if(emp==null) {
        return ResponseEntity.notFound().build();
    }

    emp.setRoomNumber(RsDetails.getRoomNumber());
    emp.setFloor(RsDetails.getFloor());
    emp.setGuestName(RsDetails.getGuestName());
    emp.setRoomType(RsDetails.getRoomType());
    emp.setBedType(RsDetails.getBedType());
    emp.setRoomStatus(RsDetails.getRoomStatus());
    emp.setConditions(RsDetails.getConditions());

    RoomStatus updateRoomStatus=roomStatusDAO.save(emp);
    return ResponseEntity.ok().body(updateRoomStatus);                      
}
Jhon Smith
  • 93
  • 12

4 Answers4

2

Using ModelMapper you can change a lot of data check links

https://www.appsdeveloperblog.com/java-objects-mapping-with-modelmapper/

Or

https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application

Anas Mehar
  • 2,739
  • 14
  • 25
Nullz
  • 41
  • 5
2

The simplest solution is to pass as @RequestBody RoomStatus collection.

@PatchMapping("/roomstatus/update")
public ResponseEntity<RoomStatus> updatRoomsStatus(@Valid @RequestBody List<RoomStatus> rsDetails){
  List<RoomStatus> updated = rsDetails.stream().map(rs -> updateRoomStatus(rs)).collect(Collectors.toList());
  return ResponseEntity.ok().body(updated);
}

private RoomStatus updateRoomStatus(RoomStatus emp) {
  if(emp==null) {
    return ResponseEntity.notFound().build();
  }
  emp.setRoomNumber(RsDetails.getRoomNumber());
  emp.setFloor(RsDetails.getFloor());
  emp.setGuestName(RsDetails.getGuestName());
  emp.setRoomType(RsDetails.getRoomType());
  emp.setBedType(RsDetails.getBedType());
  emp.setRoomStatus(RsDetails.getRoomStatus());
  emp.setConditions(RsDetails.getConditions());
  return roomStatusDAO.save(emp);
}

But be careful of created heap size - all the objects can be really heavy.
Take note about that question - passed objects on List will not be validated, so you can follow these answers.

PS. If modification is the same for every object - just require custom object from RequestBody that contains a list od ID and modification. Then call custom JPQL query from spring data that calls DB once instead of making n calls.

B_Osipiuk
  • 888
  • 7
  • 16
1

if you want to update your resource completely use @PutMapping to update partially use @PatchMapping. For reference check here.

user_xyz.LOL
  • 337
  • 1
  • 4
  • 14
1

First of all, your question is not specific and not formulated well enough.

But as I have understood, empId is employeeId. Employee itself has a One-to-Many relation to RoomStatus. Your method takes employeeId and roomStatusDetails and for each RoomStatus associated with employeeId, you want to set roomStatusDetails. In that case you can do the following. First, in your roomStatusDAO interface add a method to get a collection of RoomStatus by empId: findAllByEmpId(Integer empId) and provide its implementation in respective class. Then, the solution will be like this:

@PutMapping("/roomstatus/update/{id}")
public ResponseEntity<RoomStatus> updatRoomStatus(@PathVariable(value="id") Integer empid,@Valid @RequestBody RoomStatus RsDetails){    
   List<RoomStatus> updatedRoomStatuses = roomStatusDAO.findAllByEmpId(empid).stream().map(rs -> setRsDetailsAndSave(rs, RsDetails)).collect(Collectors.toList());
   return updatedRoomStatuses.isEmpty() ? ResponseEntity.notFound().build() : ResponseEntity.ok().body(updatedRoomStatus);                   
}

private RoomStatus setRsDetailsAndSave (RoomStatus rs, RoomStatus RsDetails) {
    rs.setRoomNumber(RsDetails.getRoomNumber());
    rs.setFloor(RsDetails.getFloor());
    rs.setGuestName(RsDetails.getGuestName());
    rs.setRoomType(RsDetails.getRoomType());
    rs.setBedType(RsDetails.getBedType());
    rs.setRoomStatus(RsDetails.getRoomStatus());
    rs.setConditions(RsDetails.getConditions());
    return roomStatusDAO.save(rs);
}
Alish_strong
  • 99
  • 1
  • 8