diff --git a/application/src/main/java/org/depromeet/spot/application/block/BlockReadController.java b/application/src/main/java/org/depromeet/spot/application/block/BlockReadController.java index 4b542e0e..81a293d1 100644 --- a/application/src/main/java/org/depromeet/spot/application/block/BlockReadController.java +++ b/application/src/main/java/org/depromeet/spot/application/block/BlockReadController.java @@ -50,7 +50,7 @@ public List findCodeInfosByStadium( @ResponseStatus(HttpStatus.OK) @GetMapping("stadiums/{stadiumId}/sections/{sectionId}/blocks/rows") - @Operation(summary = "특정 야구장 섹션 내에 있는 모든 블록 열/번 정보를 조회한다.") + @Operation(summary = "특정 야구장 구역 내에 있는 모든 블록 열/번 정보를 조회한다.") public List findAllBlockInfoBy( @PathVariable("stadiumId") @NotNull @@ -65,4 +65,17 @@ public List findAllBlockInfoBy( List infos = blockReadUsecase.findAllBlockInfoBy(stadiumId, sectionId); return infos.stream().map(BlockInfoResponse::from).toList(); } + + @ResponseStatus(HttpStatus.OK) + @GetMapping("blocks/{blockId}/rows") + @Operation(summary = "특정 블록 내에 있는 열별 좌석 범위 정보를 조회한다.") + public BlockInfoResponse findBlockInfoBy( + @PathVariable("blockId") + @NotNull + @Positive + @Parameter(name = "blockId", description = "블록 PK", required = true) + final Long blockId) { + BlockInfo infos = blockReadUsecase.findBlockInfoBy(blockId); + return BlockInfoResponse.from(infos); + } } diff --git a/common/src/main/java/org/depromeet/spot/common/exception/block/BlockErrorCode.java b/common/src/main/java/org/depromeet/spot/common/exception/block/BlockErrorCode.java new file mode 100644 index 00000000..48e9b035 --- /dev/null +++ b/common/src/main/java/org/depromeet/spot/common/exception/block/BlockErrorCode.java @@ -0,0 +1,27 @@ +package org.depromeet.spot.common.exception.block; + +import org.depromeet.spot.common.exception.ErrorCode; +import org.springframework.http.HttpStatus; + +import lombok.Getter; + +@Getter +public enum BlockErrorCode implements ErrorCode { + BLOCK_NOT_FOUND(HttpStatus.NOT_FOUND, "BL001", "요청한 블록을 찾을 수 없습니다."), + ; + + private final HttpStatus status; + private final String code; + private String message; + + BlockErrorCode(HttpStatus status, String code, String message) { + this.status = status; + this.code = code; + this.message = message; + } + + public BlockErrorCode appended(Object o) { + message = message + " {" + o.toString() + "}"; + return this; + } +} diff --git a/common/src/main/java/org/depromeet/spot/common/exception/block/BlockException.java b/common/src/main/java/org/depromeet/spot/common/exception/block/BlockException.java new file mode 100644 index 00000000..910a3f2c --- /dev/null +++ b/common/src/main/java/org/depromeet/spot/common/exception/block/BlockException.java @@ -0,0 +1,20 @@ +package org.depromeet.spot.common.exception.block; + +import org.depromeet.spot.common.exception.BusinessException; + +public abstract class BlockException extends BusinessException { + + protected BlockException(BlockErrorCode errorCode) { + super(errorCode); + } + + public static class BlockNotFoundException extends BlockException { + public BlockNotFoundException() { + super(BlockErrorCode.BLOCK_NOT_FOUND); + } + + public BlockNotFoundException(Object obj) { + super(BlockErrorCode.BLOCK_NOT_FOUND.appended(obj)); + } + } +} diff --git a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/block/repository/BlockRepositoryImpl.java b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/block/repository/BlockRepositoryImpl.java index 3e55fa7b..e99f56d5 100644 --- a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/block/repository/BlockRepositoryImpl.java +++ b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/block/repository/BlockRepositoryImpl.java @@ -4,6 +4,7 @@ import java.util.Map; import java.util.stream.Collectors; +import org.depromeet.spot.common.exception.block.BlockException.BlockNotFoundException; import org.depromeet.spot.domain.block.Block; import org.depromeet.spot.domain.block.BlockRow; import org.depromeet.spot.jpa.block.entity.BlockEntity; @@ -18,6 +19,7 @@ public class BlockRepositoryImpl implements BlockRepository { private final BlockJpaRepository blockJpaRepository; + private final BlockRowJpaRepository blockRowJpaRepository; private final BlockCustomRepository blockCustomRepository; @Override @@ -39,4 +41,23 @@ public Map> findRowInfosBy(final Long sectionId) { .map(BlockRowEntity::toDomain) .toList())); } + + @Override + public List findAllByBlock(final Long blockId) { + List entities = + blockRowJpaRepository.findAllByBlockIdOrderByNumberAsc(blockId); + return entities.stream().map(BlockRowEntity::toDomain).toList(); + } + + @Override + public boolean existsById(final Long blockId) { + return blockJpaRepository.existsById(blockId); + } + + @Override + public Block findById(final Long blockId) { + BlockEntity entity = + blockJpaRepository.findById(blockId).orElseThrow(BlockNotFoundException::new); + return entity.toDomain(); + } } diff --git a/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/block/repository/BlockRowJpaRepository.java b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/block/repository/BlockRowJpaRepository.java new file mode 100644 index 00000000..9257519b --- /dev/null +++ b/infrastructure/jpa/src/main/java/org/depromeet/spot/jpa/block/repository/BlockRowJpaRepository.java @@ -0,0 +1,11 @@ +package org.depromeet.spot.jpa.block.repository; + +import java.util.List; + +import org.depromeet.spot.jpa.block.entity.BlockRowEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BlockRowJpaRepository extends JpaRepository { + + List findAllByBlockIdOrderByNumberAsc(Long blockId); +} diff --git a/usecase/src/main/java/org/depromeet/spot/usecase/port/in/block/BlockReadUsecase.java b/usecase/src/main/java/org/depromeet/spot/usecase/port/in/block/BlockReadUsecase.java index b5565217..0f0669d8 100644 --- a/usecase/src/main/java/org/depromeet/spot/usecase/port/in/block/BlockReadUsecase.java +++ b/usecase/src/main/java/org/depromeet/spot/usecase/port/in/block/BlockReadUsecase.java @@ -2,6 +2,8 @@ import java.util.List; +import org.depromeet.spot.domain.block.Block; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -12,6 +14,14 @@ public interface BlockReadUsecase { List findAllBlockInfoBy(final Long stadiumId, final Long sectionId); + BlockInfo findBlockInfoBy(final Long blockId); + + Block findById(Long blockId); + + boolean existsById(Long blockId); + + void checkExistsById(Long blockId); + @Getter @AllArgsConstructor class BlockCodeInfo { diff --git a/usecase/src/main/java/org/depromeet/spot/usecase/port/out/block/BlockRepository.java b/usecase/src/main/java/org/depromeet/spot/usecase/port/out/block/BlockRepository.java index bc559cb8..d33d356c 100644 --- a/usecase/src/main/java/org/depromeet/spot/usecase/port/out/block/BlockRepository.java +++ b/usecase/src/main/java/org/depromeet/spot/usecase/port/out/block/BlockRepository.java @@ -11,4 +11,10 @@ public interface BlockRepository { List findAllBySection(Long sectionId); Map> findRowInfosBy(Long sectionId); + + List findAllByBlock(Long blockId); + + boolean existsById(Long blockId); + + Block findById(Long blockId); } diff --git a/usecase/src/main/java/org/depromeet/spot/usecase/service/block/BlockReadService.java b/usecase/src/main/java/org/depromeet/spot/usecase/service/block/BlockReadService.java index b608e2d7..aee60ed4 100644 --- a/usecase/src/main/java/org/depromeet/spot/usecase/service/block/BlockReadService.java +++ b/usecase/src/main/java/org/depromeet/spot/usecase/service/block/BlockReadService.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; +import org.depromeet.spot.common.exception.block.BlockException.BlockNotFoundException; import org.depromeet.spot.domain.block.Block; import org.depromeet.spot.domain.block.BlockRow; import org.depromeet.spot.usecase.port.in.block.BlockReadUsecase; @@ -57,11 +58,36 @@ public List findAllBlockInfoBy(final Long stadiumId, final Long secti return result; } - public List getBlockRowInfos(List seats) { + @Override + public BlockInfo findBlockInfoBy(final Long blockId) { + Block block = findById(blockId); + List infos = blockRepository.findAllByBlock(blockId); + List rowInfos = getBlockRowInfos(infos); + return new BlockInfo(blockId, block.getCode(), rowInfos); + } + + @Override + public Block findById(final Long blockId) { + return blockRepository.findById(blockId); + } + + @Override + public boolean existsById(final Long blockId) { + return blockRepository.existsById(blockId); + } + + @Override + public void checkExistsById(final Long blockId) { + if (!existsById(blockId)) { + throw new BlockNotFoundException("id : " + blockId); + } + } + + public List getBlockRowInfos(List rows) { List rowInfos = new ArrayList<>(); int lastSeatNum = BLOCK_SEAT_START_NUM; - for (BlockRow row : seats) { + for (BlockRow row : rows) { int minSeatNum = lastSeatNum; int maxSeatNum = row.getMaxSeats(); rowInfos.add( diff --git a/usecase/src/test/java/org/depromeet/spot/usecase/service/fake/FakeBlockRepository.java b/usecase/src/test/java/org/depromeet/spot/usecase/service/fake/FakeBlockRepository.java index 777a8d85..4911c269 100644 --- a/usecase/src/test/java/org/depromeet/spot/usecase/service/fake/FakeBlockRepository.java +++ b/usecase/src/test/java/org/depromeet/spot/usecase/service/fake/FakeBlockRepository.java @@ -4,9 +4,11 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; +import org.depromeet.spot.common.exception.block.BlockException.BlockNotFoundException; import org.depromeet.spot.domain.block.Block; import org.depromeet.spot.domain.block.BlockRow; import org.depromeet.spot.usecase.port.out.block.BlockRepository; @@ -26,4 +28,23 @@ public List findAllBySection(Long sectionId) { public Map> findRowInfosBy(Long sectionId) { return rowData.stream().collect(Collectors.groupingBy(BlockRow::getBlock)); } + + @Override + public List findAllByBlock(Long blockId) { + return rowData.stream().filter(row -> row.getBlock().getId().equals(blockId)).toList(); + } + + @Override + public boolean existsById(Long blockId) { + return blockData.stream().anyMatch(block -> block.getId().equals(blockId)); + } + + @Override + public Block findById(Long blockId) { + return getById(blockId).orElseThrow(BlockNotFoundException::new); + } + + private Optional getById(Long id) { + return blockData.stream().filter(block -> block.getId().equals(id)).findAny(); + } }