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);
|
return roomRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Room save(final RoomSaveRequest r, final Principal principal, boolean setWhenNull) {
|
@PostMapping
|
||||||
Room newRoom = new Room();
|
public @ResponseBody Room create(
|
||||||
|
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||||
|
|
||||||
final String username = principal.getName();
|
final String username = principal.getName();
|
||||||
final Long userId = userRepository.findByUsername(username).getId();
|
final Long userId = userRepository.findByUsername(username).getId();
|
||||||
final String img = r.getImage();
|
final String img = r.getImage();
|
||||||
final Room.Icon icon = r.getIcon();
|
final Room.Icon icon = r.getIcon();
|
||||||
|
|
||||||
|
final Room newRoom = new Room();
|
||||||
newRoom.setUserId(userId);
|
newRoom.setUserId(userId);
|
||||||
newRoom.setName(r.getName());
|
newRoom.setName(r.getName());
|
||||||
if (img != null) {
|
|
||||||
newRoom.setImage(img);
|
newRoom.setImage(img);
|
||||||
} else if (setWhenNull) {
|
|
||||||
newRoom.setImage(null);
|
|
||||||
}
|
|
||||||
if (icon != null) {
|
|
||||||
newRoom.setIcon(icon);
|
newRoom.setIcon(icon);
|
||||||
} else if (setWhenNull) {
|
|
||||||
newRoom.setIcon(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
return roomRepository.save(newRoom);
|
return roomRepository.save(newRoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PutMapping("/{id}")
|
||||||
public @ResponseBody Room create(
|
public @ResponseBody Room update(
|
||||||
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
@PathVariable("id") long id, @RequestBody RoomSaveRequest r, final Principal principal)
|
||||||
return this.save(r, principal, true);
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
if ("".equals(img)) {
|
||||||
public @ResponseBody Room update(
|
newRoom.setImage(null);
|
||||||
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
} else if (img != null) {
|
||||||
return this.save(r, principal, false);
|
newRoom.setImage(img);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (icon != null) {
|
||||||
|
newRoom.setIcon(icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
return roomRepository.save(newRoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
Loading…
Reference in a new issue