Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions doc/admin-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ components:
- title
- startTime
- endTime
- roomId
- speakerIds
properties:
title:
type: string
Expand All @@ -697,7 +697,6 @@ components:
format: uuid
capacity:
type: integer
minimum: 0
speakerIds:
type: array
items:
Expand All @@ -710,7 +709,7 @@ components:
- title
- startTime
- endTime
- roomId
- speakerIds
properties:
title:
type: string
Expand All @@ -728,7 +727,6 @@ components:
format: uuid
capacity:
type: integer
minimum: 0
speakerIds:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +77,16 @@ public ResponseEntity<ErrorResponse> handleMessageNotReadable(
return ResponseEntity.status(BAD_REQUEST).body(new ErrorResponse("Malformed request body"));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> 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<ErrorResponse> handleAccessDenied(AccessDeniedException ex) {
return ResponseEntity.status(FORBIDDEN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/org/onlydevs/hibento/mapper/SessionMapper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
39 changes: 23 additions & 16 deletions src/main/java/org/onlydevs/hibento/service/SessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down
Loading