Skip to content

Commit

Permalink
feat: 쿼리 수정 및 controller 완성
Browse files Browse the repository at this point in the history
  • Loading branch information
EunjiShin committed Jul 16, 2024
1 parent ff63f8f commit 86b4536
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 165 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
Expand Up @@ -11,6 +11,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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class BlockRow {

private final Long id;
private final Long blockId;
private final Block block;
private final Integer number;
private final Integer maxSeats;
}
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,8 +20,12 @@
@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 Integer number;
Expand All @@ -26,10 +35,12 @@ public class BlockRowEntity extends BaseEntity {

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
@@ -1,16 +1,15 @@
package org.depromeet.spot.jpa.seat.repository;
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 static org.depromeet.spot.jpa.seat.entity.QSeatEntity.seatEntity;

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

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

import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -19,18 +18,16 @@

@Repository
@RequiredArgsConstructor
public class SeatCustomRepository {
public class BlockCustomRepository {

private final JPAQueryFactory queryFactory;

public Map<BlockEntity, List<SeatEntity>> findBlockSeatsBy(final Long sectionId) {
public Map<BlockEntity, List<BlockRowEntity>> findRowInfosBy(final Long sectionId) {
return queryFactory
.from(seatEntity)
.from(blockRowEntity)
.join(blockEntity)
.on(seatEntity.blockId.eq(blockEntity.id))
.join(blockRowEntity)
.on(seatEntity.rowId.eq(blockRowEntity.id))
.where(seatEntity.sectionId.eq(sectionId))
.transform(groupBy(blockEntity).as(list(seatEntity)));
.on(blockRowEntity.block.id.eq(blockEntity.id))
.where(blockEntity.sectionId.eq(sectionId))
.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()));
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion infrastructure/jpa/src/main/resources/application-jpa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spring:

sql:
init:
mode: always # 필요한 경우 'never'로 변경
mode: never # 필요한 경우 'never'로 변경

server:
port: 8080
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@
import java.util.List;

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

public interface BlockReadUsecase {

List<BlockCodeInfo> findCodeInfosByStadium(Long stadiumId, Long sectionId);

List<BlockInfo> findAllBlockInfoBy(final Long stadiumId, final Long sectionId);

@Getter
@AllArgsConstructor
class BlockCodeInfo {
private final Long id;
private final String code;
}

@Getter
@AllArgsConstructor
class BlockInfo {
private Long id;
private String code;
private List<RowInfo> rowInfo;
}

@Getter
@Builder
class RowInfo {
private Long id;
private int number;
private int minSeatNum;
private int maxSeatNum;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.depromeet.spot.usecase.port.out.block;

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

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

public interface BlockRepository {

List<Block> findAllBySection(Long sectionId);

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

This file was deleted.

Loading

0 comments on commit 86b4536

Please sign in to comment.