Merge branch 'room-update-fix' into 'dev'

RoomController PUT fixed

See merge request sa4-2020/the-sanmarinoes/backend!53
This commit is contained in:
Claudio Maggioni 2020-03-23 16:46:22 +01:00
commit b9320ea078

View file

@ -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}")