Skip to content

Commit

Permalink
[BSVR-98] 블록별 열/번 조회 API (#25)
Browse files Browse the repository at this point in the history
* feat: block별 좌석 조회 쿼리 추가

* feat: 쿼리 수정 및 controller 완성

* test: 열별 좌석 번호 범위 계산 테스트 코드 추가

* refactor: stream 적용, 공통 메서드 분리
  • Loading branch information
EunjiShin authored Jul 16, 2024
1 parent d8309ce commit 71eefb0
Show file tree
Hide file tree
Showing 21 changed files with 394 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import jakarta.validation.constraints.Positive;

import org.depromeet.spot.application.block.dto.response.BlockCodeInfoResponse;
import org.depromeet.spot.application.block.dto.response.BlockInfoResponse;
import org.depromeet.spot.usecase.port.in.block.BlockReadUsecase;
import org.depromeet.spot.usecase.port.in.block.BlockReadUsecase.BlockCodeInfo;
import org.depromeet.spot.usecase.port.in.block.BlockReadUsecase.BlockInfo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -45,4 +47,22 @@ public List<BlockCodeInfoResponse> findCodeInfosByStadium(
List<BlockCodeInfo> infos = blockReadUsecase.findCodeInfosByStadium(stadiumId, sectionId);
return infos.stream().map(BlockCodeInfoResponse::from).toList();
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("stadiums/{stadiumId}/sections/{sectionId}/blocks/rows")
@Operation(summary = "특정 야구장 섹션 내에 있는 모든 블록 열/번 정보를 조회한다.")
public List<BlockInfoResponse> findAllBlockInfoBy(
@PathVariable("stadiumId")
@NotNull
@Positive
@Parameter(name = "stadiumId", description = "야구 경기장 PK", required = true)
final Long stadiumId,
@PathVariable("sectionId")
@NotNull
@Positive
@Parameter(name = "sectionId", description = "구역 PK", required = true)
final Long sectionId) {
List<BlockInfo> infos = blockReadUsecase.findAllBlockInfoBy(stadiumId, sectionId);
return infos.stream().map(BlockInfoResponse::from).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.depromeet.spot.application.block.dto.response;

import java.util.List;

import org.depromeet.spot.usecase.port.in.block.BlockReadUsecase.BlockInfo;
import org.depromeet.spot.usecase.port.in.block.BlockReadUsecase.RowInfo;

public record BlockInfoResponse(Long id, String code, List<RowInfoResponse> rowInfo) {
public static BlockInfoResponse from(BlockInfo blockInfo) {
List<RowInfo> rowInfos = blockInfo.getRowInfo();
List<RowInfoResponse> rowInfoResponses =
rowInfos.stream().map(RowInfoResponse::from).toList();
return new BlockInfoResponse(blockInfo.getId(), blockInfo.getCode(), rowInfoResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.depromeet.spot.application.block.dto.response;

import org.depromeet.spot.usecase.port.in.block.BlockReadUsecase.RowInfo;

import lombok.Builder;

@Builder
public record RowInfoResponse(Long id, int number, int minSeatNum, int maxSeatNum) {

public static RowInfoResponse from(RowInfo rowInfo) {
return RowInfoResponse.builder()
.id(rowInfo.getId())
.number(rowInfo.getNumber())
.minSeatNum(rowInfo.getMinSeatNum())
.maxSeatNum(rowInfo.getMaxSeatNum())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.depromeet.spot.domain.block;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class Block {

private final Long id;
Expand All @@ -11,6 +13,8 @@ public class Block {
private final String code;
private final Integer maxRows;

public static final int BLOCK_SEAT_START_NUM = 1;

public Block(Long id, Long stadiumId, Long sectionId, String code, Integer maxRows) {
this.id = id;
this.stadiumId = stadiumId;
Expand Down
17 changes: 7 additions & 10 deletions domain/src/main/java/org/depromeet/spot/domain/block/BlockRow.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package org.depromeet.spot.domain.block;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class BlockRow {

private final Long id;
private final Long blockId;
private final Long number;
private final Long maxSeats;

public BlockRow(Long id, Long blockId, Long number, Long max_seats) {
this.id = id;
this.blockId = blockId;
this.number = number;
this.maxSeats = max_seats;
}
private final Block block;
private final Integer number;
private final Integer maxSeats;
}
25 changes: 11 additions & 14 deletions domain/src/main/java/org/depromeet/spot/domain/seat/Seat.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package org.depromeet.spot.domain.seat;

import org.depromeet.spot.domain.block.Block;
import org.depromeet.spot.domain.block.BlockRow;
import org.depromeet.spot.domain.section.Section;
import org.depromeet.spot.domain.stadium.Stadium;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class Seat {

private final Long id;
private final Long stadiumId;
private final Long sectionId;
private final Long blockId;
private final Long rowId;
private final Stadium stadium;
private final Section section;
private final Block block;
private final BlockRow row;
private final Integer seatNumber;

public Seat(
Long id, Long stadiumId, Long sectionId, Long blockId, Long rowId, Integer seatNumber) {
this.id = id;
this.stadiumId = stadiumId;
this.sectionId = sectionId;
this.blockId = blockId;
this.rowId = rowId;
this.seatNumber = seatNumber;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.depromeet.spot.jpa.block.entity;

import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

import org.depromeet.spot.domain.block.BlockRow;
Expand All @@ -15,21 +20,27 @@
@NoArgsConstructor
@AllArgsConstructor
public class BlockRowEntity extends BaseEntity {
@Column(name = "block_id", nullable = false)
private Long blockId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "block_id",
nullable = false,
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private BlockEntity block;

@Column(name = "number", nullable = false)
private Long number;
private Integer number;

@Column(name = "max_seats", nullable = false)
private Long maxSeats;
private Integer maxSeats;

public static BlockRowEntity from(BlockRow blockRow) {
return new BlockRowEntity(
blockRow.getBlockId(), blockRow.getNumber(), blockRow.getMaxSeats());
BlockEntity.from(blockRow.getBlock()),
blockRow.getNumber(),
blockRow.getMaxSeats());
}

public BlockRow toDomain() {
return new BlockRow(this.getId(), blockId, number, maxSeats);
return new BlockRow(this.getId(), block.toDomain(), number, maxSeats);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.depromeet.spot.jpa.block.repository;

import static com.querydsl.core.group.GroupBy.groupBy;
import static com.querydsl.core.group.GroupBy.list;
import static org.depromeet.spot.jpa.block.entity.QBlockEntity.blockEntity;
import static org.depromeet.spot.jpa.block.entity.QBlockRowEntity.blockRowEntity;

import java.util.List;
import java.util.Map;

import org.depromeet.spot.jpa.block.entity.BlockEntity;
import org.depromeet.spot.jpa.block.entity.BlockRowEntity;
import org.springframework.stereotype.Repository;

import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class BlockCustomRepository {

private final JPAQueryFactory queryFactory;

public Map<BlockEntity, List<BlockRowEntity>> findRowInfosBy(final Long sectionId) {
return queryFactory
.from(blockRowEntity)
.join(blockEntity)
.on(blockRowEntity.block.id.eq(blockEntity.id))
.where(blockEntity.sectionId.eq(sectionId))
.orderBy(blockRowEntity.number.asc())
.transform(groupBy(blockEntity).as(list(blockRowEntity)));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.depromeet.spot.jpa.block.repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.depromeet.spot.domain.block.Block;
import org.depromeet.spot.domain.block.BlockRow;
import org.depromeet.spot.jpa.block.entity.BlockEntity;
import org.depromeet.spot.jpa.block.entity.BlockRowEntity;
import org.depromeet.spot.usecase.port.out.block.BlockRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -14,10 +18,25 @@
public class BlockRepositoryImpl implements BlockRepository {

private final BlockJpaRepository blockJpaRepository;
private final BlockCustomRepository blockCustomRepository;

@Override
public List<Block> findAllBySection(final Long sectionId) {
List<BlockEntity> entities = blockJpaRepository.findAllBySectionId(sectionId);
return entities.stream().map(BlockEntity::toDomain).toList();
}

@Override
public Map<Block, List<BlockRow>> findRowInfosBy(final Long sectionId) {
Map<BlockEntity, List<BlockRowEntity>> entityMap =
blockCustomRepository.findRowInfosBy(sectionId);
return entityMap.entrySet().stream()
.collect(
Collectors.toMap(
entry -> entry.getKey().toDomain(),
entry ->
entry.getValue().stream()
.map(BlockRowEntity::toDomain)
.toList()));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package org.depromeet.spot.jpa.seat.entity;

import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

import org.depromeet.spot.domain.seat.Seat;
import org.depromeet.spot.jpa.block.entity.BlockEntity;
import org.depromeet.spot.jpa.block.entity.BlockRowEntity;
import org.depromeet.spot.jpa.common.entity.BaseEntity;
import org.depromeet.spot.jpa.section.entity.SectionEntity;
import org.depromeet.spot.jpa.stadium.entity.StadiumEntity;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
Expand All @@ -16,31 +25,53 @@
@AllArgsConstructor
public class SeatEntity extends BaseEntity {

@Column(name = "stadium_id", nullable = false)
private Long stadiumId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "stadium_id",
nullable = false,
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private StadiumEntity stadium;

@Column(name = "section_id", nullable = false)
private Long sectionId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "section_id",
nullable = false,
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private SectionEntity section;

@Column(name = "block_id", nullable = false)
private Long blockId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "block_id",
nullable = false,
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private BlockEntity block;

@Column(name = "row_id", nullable = false)
private Long rowId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "row_id",
nullable = false,
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private BlockRowEntity row;

@Column(name = "seat_number", nullable = false)
private Integer seatNumber;

public static SeatEntity from(Seat seat) {
return new SeatEntity(
seat.getStadiumId(),
seat.getSectionId(),
seat.getBlockId(),
seat.getRowId(),
StadiumEntity.from(seat.getStadium()),
SectionEntity.from(seat.getSection()),
BlockEntity.from(seat.getBlock()),
BlockRowEntity.from(seat.getRow()),
seat.getSeatNumber());
}

public Seat toDomain() {
return new Seat(this.getId(), stadiumId, sectionId, blockId, rowId, seatNumber);
return new Seat(
this.getId(),
stadium.toDomain(),
section.toDomain(),
block.toDomain(),
row.toDomain(),
seatNumber);
}
}
1 change: 0 additions & 1 deletion infrastructure/jpa/src/main/resources/application-jpa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ spring:
hibernate:
use_sql_comments: true
defer-datasource-initialization: true

sql:
init:
mode: never # 필요한 경우 'never'로 변경
Expand Down
15 changes: 15 additions & 0 deletions infrastructure/jpa/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ VALUES (1, 1, '오렌지석', '응원석', 255, 255, 255),
(2, 1, '네이비석', '프리미엄석', 100, 100, 100),
(3, 1, '레드석', null, 100, 0, 0);

-- Block
INSERT INTO blocks (id, stadium_id, section_id, code, max_rows)
VALUES (1, 1, 1, "207", 3),
(2, 1, 1, "208", 2),
(3, 1, 1, "209", 1);

-- Row
INSERT INTO block_rows (id, block_id, number, max_seats)
VALUES (1, 1, 1, 3),
(2, 1, 2, 5),
(3, 1, 3, 10),
(4, 2, 1, 13),
(5, 2, 2, 20),
(6, 3, 1, 15);

-- Reviews
INSERT INTO reviews (id, user_id, stadium_id, block_id, row_id, seat_number, date_time, content, created_at, updated_at)
VALUES
Expand Down
Loading

0 comments on commit 71eefb0

Please sign in to comment.