Skip to content
Binary file added pr-review.patch
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryDto;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryResponseDto;

Expand All @@ -25,39 +24,32 @@
@Tag(name = "Room-Category Assignments", description = "Manage the association of rooms with categories")
public class RoomCategoryController {

private final RoomCategoryService roomCategoryService;
private final RoomCategoryMapper roomCategoryMapper;
private final RoomCategoryWebFacade roomCategoryWebFacade;

@PostMapping("/add")
public ResponseEntity<RoomCategoryResponseDto> createRoomCategory(
public ResponseEntity<Void> createRoomCategory(
@Valid @NotNull @RequestBody final RoomCategoryRequestDto roomCategoryRequestDto) {

RoomCategoryDto roomCategoryDto = roomCategoryService.createRoomCategory(
roomCategoryMapper.toInternalDto(roomCategoryRequestDto));
roomCategoryWebFacade.createRoomCategory(roomCategoryRequestDto);

return new ResponseEntity<>(roomCategoryMapper.toResponseDto(roomCategoryDto),
HttpStatus.CREATED);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@GetMapping("/getAll")
public List<RoomCategoryResponseDto> getAllRoomCategories() {
return roomCategoryMapper.toResponseDtoList(roomCategoryService.getAllRoomCategories());
public ResponseEntity<List<RoomCategoryResponseDto>> getAllRoomCategories() {
return ResponseEntity.ok(roomCategoryWebFacade.getAllRoomCategories());
}

@GetMapping("/getById")
public ResponseEntity<RoomCategoryResponseDto> getRoomCategoryById(@RequestParam final UUID roomId,
@RequestParam final UUID categoryId) {
RoomCategoryDto roomCategoryDto = roomCategoryService.getRoomCategoryById(roomId,
categoryId);

return ResponseEntity.ok(roomCategoryMapper.toResponseDto(roomCategoryDto));
return ResponseEntity.ok(roomCategoryWebFacade.getRoomCategoryById(roomId, categoryId));
}


@DeleteMapping("/delete")
public ResponseEntity<Void> deleteRoomCategory(@RequestParam final UUID roomId,
@RequestParam final UUID categoryId) {
roomCategoryService.deleteRoomCategory(roomId, categoryId);
roomCategoryWebFacade.deleteRoomCategory(roomId, categoryId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.UUID;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryDto;
import org.mapstruct.MappingTarget;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryResponseDto;

Expand All @@ -14,19 +14,19 @@ public interface RoomCategoryMapper {
@Mapping(target = "id", expression = "java(new RoomCategoryId(dto.roomId(), dto.categoryId()))")
@Mapping(target = "room", ignore = true)
@Mapping(target = "category", ignore = true)
RoomCategory toEntity(RoomCategoryDto dto);
RoomCategory toEntity(RoomCategoryRequestDto dto);

@Mapping(source = "id.roomId", target = "roomId")
@Mapping(source = "id.categoryId", target = "categoryId")
RoomCategoryDto toDto(RoomCategory entity);

RoomCategoryDto toInternalDto(RoomCategoryRequestDto requestDto);
RoomCategoryResponseDto toResponseDto(final RoomCategory roomCategory);

RoomCategoryResponseDto toResponseDto(RoomCategoryDto roomCategoryDto);
List<RoomCategoryResponseDto> toResponseDtoList(List<RoomCategory> roomCategories);

List<RoomCategoryDto> toDtoList(List<RoomCategory> entities);

List<RoomCategoryResponseDto> toResponseDtoList(List<RoomCategoryDto> roomCategories);
@Mapping(target = "id", expression = "java(new RoomCategoryId(requestDto.roomId(), requestDto.categoryId()))")
@Mapping(target = "room", ignore = true)
@Mapping(target = "category", ignore = true)
void updateEntity(final RoomCategoryRequestDto requestDto,
@MappingTarget final RoomCategory entity);

RoomCategoryId toRoomCategoryId(UUID roomId, UUID categoryId);
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
package org.unilab.uniplan.roomcategory;

import static org.unilab.uniplan.utils.ErrorConstants.ROOM_CATEGORY_NOT_FOUND;

import java.util.List;
import java.util.UUID;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryDto;

@Service
@RequiredArgsConstructor
public class RoomCategoryService {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement BaseService. (Could BaseService be generic so it supports both UUID and RoomCategoryId?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseService could be made generic but that would affect all services so I think it should be a separate task


private final RoomCategoryRepository roomCategoryRepository;
private final RoomCategoryMapper roomCategoryMapper;

@Transactional
public RoomCategoryDto createRoomCategory(final RoomCategoryDto roomCategoryDto) {
final RoomCategory roomCategory = roomCategoryMapper.toEntity(roomCategoryDto);

return roomCategoryMapper.toDto(roomCategoryRepository.save(roomCategory));
public void save(final RoomCategory roomCategory) {
roomCategoryRepository.save(roomCategory);
}

public List<RoomCategoryDto> getAllRoomCategories() {
final List<RoomCategory> roomCategories = roomCategoryRepository.findAll();
return roomCategoryMapper.toDtoList(roomCategories);
public List<RoomCategory> getAll() {
return roomCategoryRepository.findAll();
}

public RoomCategoryDto getRoomCategoryById(final UUID roomId, final UUID categoryId) {
final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId);

return roomCategoryRepository.findById(id)
.map(roomCategoryMapper::toDto)
.orElseThrow(() -> new ResourceNotFoundException(
ROOM_CATEGORY_NOT_FOUND.getMessage(String.valueOf(id))));
public Optional<RoomCategory> getById(final RoomCategoryId id) {
return roomCategoryRepository.findById(id);
}

@Transactional
public void deleteRoomCategory(final UUID roomId, final UUID categoryId) {
final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId);

final RoomCategory roomCategory = roomCategoryRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(
ROOM_CATEGORY_NOT_FOUND.getMessage(
String.valueOf(id))));

public void delete(RoomCategory roomCategory) {
roomCategoryRepository.delete(roomCategory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.unilab.uniplan.roomcategory;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.unilab.uniplan.category.CategoryRepository;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.room.RoomRepository;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto;

import java.util.UUID;

import static org.unilab.uniplan.utils.ErrorConstants.CATEGORY_NOT_FOUND;
import static org.unilab.uniplan.utils.ErrorConstants.ROOM_NOT_FOUND;

@Component
@RequiredArgsConstructor
public class RoomCategoryValidator {
private final CategoryRepository categoryRepository;
private final RoomRepository roomRepository;

public void validateForCreate(final RoomCategoryRequestDto requestDto) {
validateCategoryExists(requestDto.categoryId());
validateRoomExists(requestDto.roomId());
}

public void validateForUpdate(final RoomCategoryId id, final RoomCategoryRequestDto requestDto) {
validateCategoryExists(requestDto.categoryId());
validateRoomExists(requestDto.roomId());
}

public void validateCategoryExists(final UUID categoryId) {
if (!categoryRepository.existsById(categoryId)) {
throw new ResourceNotFoundException(
CATEGORY_NOT_FOUND.getMessage(String.valueOf(categoryId))
);
}
}

public void validateRoomExists(final UUID roomId) {
if (!roomRepository.existsById(roomId)) {
throw new ResourceNotFoundException(
ROOM_NOT_FOUND.getMessage(String.valueOf(roomId))
);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.unilab.uniplan.roomcategory;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto;
import org.unilab.uniplan.roomcategory.dto.RoomCategoryResponseDto;
import java.util.List;
import java.util.UUID;

import static org.unilab.uniplan.utils.ErrorConstants.ROOM_CATEGORY_NOT_FOUND;

@Component
@Slf4j
@RequiredArgsConstructor
public class RoomCategoryWebFacade {

private final RoomCategoryService roomCategoryService;
private final RoomCategoryMapper roomCategoryMapper;
private final RoomCategoryValidator roomCategoryValidator;

private RoomCategory getRoomCategoryOrThrow(final RoomCategoryId id) {
return roomCategoryService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(ROOM_CATEGORY_NOT_FOUND.getMessage(
String.valueOf(id))));
}

@Transactional
public void createRoomCategory(final RoomCategoryRequestDto requestDto) {
roomCategoryValidator.validateForCreate(requestDto);

final RoomCategory roomCategory = roomCategoryMapper.toEntity(requestDto);
roomCategoryService.save(roomCategory);

log.info("created room category with roomId: {} and categoryId: {}",
requestDto.roomId(),
requestDto.categoryId());
}

@Transactional(readOnly = true)
public List<RoomCategoryResponseDto> getAllRoomCategories() {
return roomCategoryMapper.toResponseDtoList(roomCategoryService.getAll());
}

@Transactional
public void deleteRoomCategory(final UUID roomId, final UUID categoryId) {
final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId);
final RoomCategory roomCategory = getRoomCategoryOrThrow(id);

roomCategoryService.delete(roomCategory);

log.info("deleted room category with ID: {}", id);
}

@Transactional(readOnly = true)
public RoomCategoryResponseDto getRoomCategoryById(final UUID roomId, final UUID categoryId) {
final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId);
final RoomCategory roomCategory = getRoomCategoryOrThrow(id);

return roomCategoryMapper.toResponseDto(roomCategory);
}

@Transactional
public void updateRoomCategory(final UUID roomId, final UUID categoryId, final RoomCategoryRequestDto requestDto) {
final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId);

roomCategoryValidator.validateForUpdate(id, requestDto);

final RoomCategory roomCategory = getRoomCategoryOrThrow(id);
roomCategoryMapper.updateEntity(requestDto, roomCategory);
roomCategoryService.save(roomCategory);

log.info("updated room category with ID: {}", roomCategory.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

public record RoomCategoryRequestDto(

@NotNull
@NotNull(message = "Room ID cannot be null")
UUID roomId,

@NotNull
@NotNull(message = "Category ID cannot be null")
UUID categoryId
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

public record RoomCategoryResponseDto(

@NotNull
UUID roomId,

@NotNull
UUID categoryId
) {

Expand Down
Loading
Loading