RoomController PUT fixed
This commit is contained in:
parent
1204451e47
commit
f4d6a3f3c7
1 changed files with 31 additions and 21 deletions
|
@ -33,40 +33,50 @@ public class RoomController {
|
|||
return roomRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
private Room save(final RoomSaveRequest r, final Principal principal, boolean setWhenNull) {
|
||||
Room newRoom = new Room();
|
||||
@PostMapping
|
||||
public @ResponseBody Room create(
|
||||
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||
|
||||
final String username = principal.getName();
|
||||
final Long userId = userRepository.findByUsername(username).getId();
|
||||
final String img = r.getImage();
|
||||
final Room.Icon icon = r.getIcon();
|
||||
|
||||
final Room newRoom = new Room();
|
||||
newRoom.setUserId(userId);
|
||||
newRoom.setName(r.getName());
|
||||
if (img != null) {
|
||||
newRoom.setImage(img);
|
||||
} else if (setWhenNull) {
|
||||
newRoom.setImage(null);
|
||||
}
|
||||
if (icon != null) {
|
||||
newRoom.setIcon(icon);
|
||||
} else if (setWhenNull) {
|
||||
newRoom.setIcon(null);
|
||||
}
|
||||
newRoom.setImage(img);
|
||||
newRoom.setIcon(icon);
|
||||
|
||||
return roomRepository.save(newRoom);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public @ResponseBody Room create(
|
||||
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||
return this.save(r, principal, true);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@PutMapping("/{id}")
|
||||
public @ResponseBody Room update(
|
||||
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||
return this.save(r, principal, false);
|
||||
@PathVariable("id") long id, @RequestBody RoomSaveRequest r, final Principal principal)
|
||||
throws NotFoundException {
|
||||
final Room newRoom =
|
||||
roomRepository
|
||||
.findByIdAndUsername(id, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
final String img = r.getImage();
|
||||
final Room.Icon icon = r.getIcon();
|
||||
|
||||
if (r.getName() != null) {
|
||||
newRoom.setName(r.getName());
|
||||
}
|
||||
|
||||
if ("".equals(img)) {
|
||||
newRoom.setImage(null);
|
||||
} else if (img != null) {
|
||||
newRoom.setImage(img);
|
||||
}
|
||||
|
||||
if (icon != null) {
|
||||
newRoom.setIcon(icon);
|
||||
}
|
||||
|
||||
return roomRepository.save(newRoom);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
Loading…
Reference in a new issue