Skip to content

Commit

Permalink
feat: 블록의 열별 좌석 범위 조회 API (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
EunjiShin authored Jul 17, 2024
1 parent 875e569 commit 761b172
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public List<BlockCodeInfoResponse> findCodeInfosByStadium(

@ResponseStatus(HttpStatus.OK)
@GetMapping("stadiums/{stadiumId}/sections/{sectionId}/blocks/rows")
@Operation(summary = "특정 야구장 섹션 내에 있는 모든 블록 열/번 정보를 조회한다.")
@Operation(summary = "특정 야구장 구역 내에 있는 모든 블록 열/번 정보를 조회한다.")
public List<BlockInfoResponse> findAllBlockInfoBy(
@PathVariable("stadiumId")
@NotNull
Expand All @@ -65,4 +65,17 @@ public List<BlockInfoResponse> findAllBlockInfoBy(
List<BlockInfo> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@
public class BlockRepositoryImpl implements BlockRepository {

private final BlockJpaRepository blockJpaRepository;
private final BlockRowJpaRepository blockRowJpaRepository;
private final BlockCustomRepository blockCustomRepository;

@Override
Expand All @@ -39,4 +41,23 @@ public Map<Block, List<BlockRow>> findRowInfosBy(final Long sectionId) {
.map(BlockRowEntity::toDomain)
.toList()));
}

@Override
public List<BlockRow> findAllByBlock(final Long blockId) {
List<BlockRowEntity> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<BlockRowEntity, Long> {

List<BlockRowEntity> findAllByBlockIdOrderByNumberAsc(Long blockId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.depromeet.spot.domain.block.Block;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -12,6 +14,14 @@ public interface BlockReadUsecase {

List<BlockInfo> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ public interface BlockRepository {
List<Block> findAllBySection(Long sectionId);

Map<Block, List<BlockRow>> findRowInfosBy(Long sectionId);

List<BlockRow> findAllByBlock(Long blockId);

boolean existsById(Long blockId);

Block findById(Long blockId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,11 +58,36 @@ public List<BlockInfo> findAllBlockInfoBy(final Long stadiumId, final Long secti
return result;
}

public List<RowInfo> getBlockRowInfos(List<BlockRow> seats) {
@Override
public BlockInfo findBlockInfoBy(final Long blockId) {
Block block = findById(blockId);
List<BlockRow> infos = blockRepository.findAllByBlock(blockId);
List<RowInfo> 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<RowInfo> getBlockRowInfos(List<BlockRow> rows) {
List<RowInfo> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,4 +28,23 @@ public List<Block> findAllBySection(Long sectionId) {
public Map<Block, List<BlockRow>> findRowInfosBy(Long sectionId) {
return rowData.stream().collect(Collectors.groupingBy(BlockRow::getBlock));
}

@Override
public List<BlockRow> 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<Block> getById(Long id) {
return blockData.stream().filter(block -> block.getId().equals(id)).findAny();
}
}

0 comments on commit 761b172

Please sign in to comment.