diff --git a/doc/admin-spec.yaml b/doc/admin-spec.yaml index 1ebd213..25a969b 100644 --- a/doc/admin-spec.yaml +++ b/doc/admin-spec.yaml @@ -679,7 +679,7 @@ components: - title - startTime - endTime - - roomId + - speakerIds properties: title: type: string @@ -697,7 +697,6 @@ components: format: uuid capacity: type: integer - minimum: 0 speakerIds: type: array items: @@ -710,7 +709,7 @@ components: - title - startTime - endTime - - roomId + - speakerIds properties: title: type: string @@ -728,7 +727,6 @@ components: format: uuid capacity: type: integer - minimum: 0 speakerIds: type: array items: diff --git a/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/GlobalExceptionHandler.java b/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/GlobalExceptionHandler.java index 1fb122a..e8cae61 100644 --- a/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/GlobalExceptionHandler.java +++ b/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/GlobalExceptionHandler.java @@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.security.access.AccessDeniedException; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -76,6 +77,16 @@ public ResponseEntity handleMessageNotReadable( return ResponseEntity.status(BAD_REQUEST).body(new ErrorResponse("Malformed request body")); } + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleValidation(MethodArgumentNotValidException ex) { + var message = + ex.getBindingResult().getFieldErrors().stream() + .map(e -> e.getField() + ": " + e.getDefaultMessage()) + .reduce((a, b) -> a + "; " + b) + .orElse("Validation failed"); + return ResponseEntity.status(BAD_REQUEST).body(new ErrorResponse(message)); + } + @ExceptionHandler(AccessDeniedException.class) public ResponseEntity handleAccessDenied(AccessDeniedException ex) { return ResponseEntity.status(FORBIDDEN) diff --git a/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/CreateSession.java b/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/CreateSession.java index 9a6cc6e..3948543 100644 --- a/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/CreateSession.java +++ b/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/CreateSession.java @@ -29,7 +29,6 @@ public class CreateSession { @NotNull(message = "End time is required") private Instant endTime; - @NotNull(message = "Room ID is required") private UUID roomId; private Integer capacity; diff --git a/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/UpdateSession.java b/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/UpdateSession.java index 8f026c7..a6c778d 100644 --- a/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/UpdateSession.java +++ b/src/main/java/org/onlydevs/hibento/endpoint/rest/controller/dto/request/UpdateSession.java @@ -29,7 +29,6 @@ public class UpdateSession { @NotNull(message = "End time is required") private Instant endTime; - @NotNull(message = "Room ID is required") private UUID roomId; private Integer capacity; diff --git a/src/main/java/org/onlydevs/hibento/mapper/SessionMapper.java b/src/main/java/org/onlydevs/hibento/mapper/SessionMapper.java index cc9252a..6fc5981 100644 --- a/src/main/java/org/onlydevs/hibento/mapper/SessionMapper.java +++ b/src/main/java/org/onlydevs/hibento/mapper/SessionMapper.java @@ -1,5 +1,6 @@ package org.onlydevs.hibento.mapper; +import java.util.UUID; import org.onlydevs.hibento.endpoint.rest.controller.dto.response.CreatedSession; import org.onlydevs.hibento.endpoint.rest.controller.dto.response.UpdatedSession; import org.onlydevs.hibento.model.EventSession; @@ -8,6 +9,14 @@ @Component public class SessionMapper { + private UUID nullableRoomId(EventSession session) { + return session.getRoom() != null ? session.getRoom().getId() : null; + } + + private String nullableRoomName(EventSession session) { + return session.getRoom() != null ? session.getRoom().getName() : null; + } + public CreatedSession toCreatedSession(EventSession session) { return new CreatedSession( session.getId(), @@ -16,8 +25,8 @@ public CreatedSession toCreatedSession(EventSession session) { session.getDescription(), session.getStartTime(), session.getEndTime(), - session.getRoom().getId(), - session.getRoom().getName(), + nullableRoomId(session), + nullableRoomName(session), session.getCapacity(), session.getEventSessionSpeakers().stream().map(ess -> ess.getSpeaker().getId()).toList(), session.getCreatedAt(), @@ -32,8 +41,8 @@ public UpdatedSession toUpdatedSession(EventSession session) { session.getDescription(), session.getStartTime(), session.getEndTime(), - session.getRoom().getId(), - session.getRoom().getName(), + nullableRoomId(session), + nullableRoomName(session), session.getCapacity(), session.getEventSessionSpeakers().stream().map(ess -> ess.getSpeaker().getId()).toList(), session.getCreatedAt(), diff --git a/src/main/java/org/onlydevs/hibento/service/SessionService.java b/src/main/java/org/onlydevs/hibento/service/SessionService.java index 72d54d1..d8a0848 100644 --- a/src/main/java/org/onlydevs/hibento/service/SessionService.java +++ b/src/main/java/org/onlydevs/hibento/service/SessionService.java @@ -43,14 +43,17 @@ public CreatedSession createSession(UUID eventId, CreateSession request) { .findById(eventId) .orElseThrow(() -> new NotFoundException("Event not found")); - Room room = - roomRepository - .findById(request.getRoomId()) - .orElseThrow(() -> new NotFoundException("Room not found")); - - if (sessionRepository.existsByRoomIdAndStartTimeLessThanEqualAndEndTimeGreaterThanEqual( - request.getRoomId(), request.getEndTime(), request.getStartTime())) { - throw new ConflictException("Room already has a session during this time"); + Room room = null; + if (request.getRoomId() != null) { + room = + roomRepository + .findById(request.getRoomId()) + .orElseThrow(() -> new NotFoundException("Room not found")); + + if (sessionRepository.existsByRoomIdAndStartTimeLessThanEqualAndEndTimeGreaterThanEqual( + request.getRoomId(), request.getEndTime(), request.getStartTime())) { + throw new ConflictException("Room already has a session during this time"); + } } EventSession session = new EventSession(); @@ -96,14 +99,18 @@ public UpdatedSession updateSession(UUID id, UpdateSession request) { throw new BadRequestException("Start time must be before end time"); } - Room room = - roomRepository - .findById(request.getRoomId()) - .orElseThrow(() -> new NotFoundException("Room not found")); - - if (sessionRepository.existsByRoomIdAndStartTimeLessThanEqualAndEndTimeGreaterThanEqualAndIdNot( - request.getRoomId(), request.getEndTime(), request.getStartTime(), id)) { - throw new ConflictException("Room already has a session during this time"); + Room room = session.getRoom(); + if (request.getRoomId() != null) { + room = + roomRepository + .findById(request.getRoomId()) + .orElseThrow(() -> new NotFoundException("Room not found")); + + if (sessionRepository + .existsByRoomIdAndStartTimeLessThanEqualAndEndTimeGreaterThanEqualAndIdNot( + request.getRoomId(), request.getEndTime(), request.getStartTime(), id)) { + throw new ConflictException("Room already has a session during this time"); + } } session.setTitle(request.getTitle());