Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

콘서트 좌석정보 수정, 콘서트 상태 close로 변경 API 개발 및 콘서트 상태 open 변경 스케줄러 작업 추가 #4

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions admin/admin-concert/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
buildscript {
ext {
queryDslVersion = "5.0.0"
}
}

dependencies {
// database
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.mysql:mysql-connector-j'

// querydsl
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}:jakarta"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

// validator
implementation 'org.springframework.boot:spring-boot-starter-validation'

//scheduler lock
implementation 'net.javacrumbs.shedlock:shedlock-spring:5.10.0'
implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:5.10.0'
}

bootJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package co.kr.ticketing.adminconcert.common.repository;

import java.util.List;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaEntityInformationSupport;
import org.springframework.data.jpa.repository.support.Querydsl;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.util.Assert;

import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;

public abstract class QuerydslRepositorySupport {

private final Class<?> domainClass;
private Querydsl querydsl;
private EntityManager entityManager;
private JPAQueryFactory queryFactory;

protected QuerydslRepositorySupport(Class<?> domainClass) {
Assert.notNull(domainClass, "Domain class must not be null!");
this.domainClass = domainClass;
}

@Autowired
public void setEntityManager(EntityManager entityManager) {
Assert.notNull(entityManager, "EntityManager must not be null!");
JpaEntityInformation<?, ?> entityInformation = JpaEntityInformationSupport.getEntityInformation(
domainClass, entityManager);
SimpleEntityPathResolver resolver = SimpleEntityPathResolver.INSTANCE;
EntityPath<?> path = resolver.createPath(entityInformation.getJavaType());
this.entityManager = entityManager;
this.querydsl = new Querydsl(entityManager,
new PathBuilder<>(path.getType(), path.getMetadata()));
this.queryFactory = new JPAQueryFactory(entityManager);
}

@PostConstruct
public void validate() {
Assert.notNull(entityManager, "EntityManager must not be null!");
Assert.notNull(querydsl, "Querydsl must not be null!");
Assert.notNull(queryFactory, "QueryFactory must not be null!");
}

protected JPAQueryFactory getQueryFactory() {
return queryFactory;
}

protected Querydsl getQuerydsl() {
return querydsl;
}

protected EntityManager getEntityManager() {
return entityManager;
}

protected <T> JPAQuery<T> select(Expression<T> expr) {
return getQueryFactory().select(expr);
}

protected <T> JPAQuery<T> selectFrom(EntityPath<T> from) {
return getQueryFactory().selectFrom(from);
}

protected <T> Page<T> applyPagination(
Pageable pageable,
Function<JPAQueryFactory, JPAQuery<T>> contentQuery
) {
JPAQuery<T> jpaQuery = contentQuery.apply(getQueryFactory());
List<T> content = getQuerydsl().applyPagination(pageable, jpaQuery).fetch();

return PageableExecutionUtils.getPage(content, pageable, content::size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -15,15 +16,21 @@
import co.kr.ticketing.adminconcert.common.dto.ResponseDto;
import co.kr.ticketing.adminconcert.common.dto.UpdatedDto;
import co.kr.ticketing.adminconcert.concert.controller.request.CreateConcertRequest;
import co.kr.ticketing.adminconcert.concert.controller.request.GetConcertListRequest;
import co.kr.ticketing.adminconcert.concert.controller.request.ModifyConcertSeatsRequest;
import co.kr.ticketing.adminconcert.concert.controller.request.ModifyRoundsRequest;
import co.kr.ticketing.adminconcert.concert.controller.request.SetOpenTimeRequest;
import co.kr.ticketing.adminconcert.concert.controller.request.SetTicketingStartTimeRequest;
import co.kr.ticketing.adminconcert.concert.controller.request.UpdateConcertRequest;
import co.kr.ticketing.adminconcert.concert.controller.request.UpdatePlaceRequest;
import co.kr.ticketing.adminconcert.concert.controller.response.ConcertResponseCode;
import co.kr.ticketing.adminconcert.concert.controller.response.GetConcertListResponse;
import co.kr.ticketing.adminconcert.concert.controller.response.GetConcertResponse;
import co.kr.ticketing.adminconcert.concert.usecase.reader.GetConcertListUseCase;
import co.kr.ticketing.adminconcert.concert.usecase.reader.GetConcertUseCase;
import co.kr.ticketing.adminconcert.concert.usecase.writer.ChangeStateToCloseUseCase;
import co.kr.ticketing.adminconcert.concert.usecase.writer.CreateConcertUseCase;
import co.kr.ticketing.adminconcert.concert.usecase.writer.ModifyConcertSeatsUseCase;
import co.kr.ticketing.adminconcert.concert.usecase.writer.ModifyRoundsUseCase;
import co.kr.ticketing.adminconcert.concert.usecase.writer.SetOpenTimeUseCase;
import co.kr.ticketing.adminconcert.concert.usecase.writer.SetTicketingStartTimeUseCase;
Expand All @@ -41,13 +48,25 @@
@RequestMapping(value = "/concerts")
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class ConcertController {
GetConcertListUseCase getConcertListUseCase;
CreateConcertUseCase createConcertUseCase;
GetConcertUseCase getConcertUseCase;
UpdateConcertUseCase updateConcertUseCase;
SetOpenTimeUseCase setOpenTimeUseCase;
SetTicketingStartTimeUseCase setTicketingStartTimeUseCase;
ModifyRoundsUseCase modifyRoundsUseCase;
UpdatePlaceUseCase updatePlaceUseCase;
ModifyConcertSeatsUseCase modifyConcertSeatsUseCase;
ChangeStateToCloseUseCase changeStateToCloseUseCase;

@GetMapping
public ResponseEntity<ResponseDto<GetConcertListResponse>> getList(
@ModelAttribute @Valid GetConcertListRequest request) {
var response = getConcertListUseCase.execute(request);

return ResponseEntity.ok(
new ResponseDto<>(ConcertResponseCode.CREATED_CONCERT.name(), response));
}

@PostMapping
public ResponseEntity<ResponseDto<CreatedDto>> createConcert(@RequestBody @Valid CreateConcertRequest request) {
Expand Down Expand Up @@ -122,4 +141,25 @@ public ResponseEntity<ResponseDto<UpdatedDto>> updatePlace(
new ResponseDto<>(ConcertResponseCode.UPDATE_CONCERT_PLACE.name(), UpdatedDto.from(updatedId))
);
}

@PatchMapping("/{id}/seats")
public ResponseEntity<ResponseDto<UpdatedDto>> modifyConcertSeats(
@PathVariable @Positive Long id,
@RequestBody @Valid ModifyConcertSeatsRequest request
) {
long updatedId = modifyConcertSeatsUseCase.execute(id, request);

return ResponseEntity.ok(
new ResponseDto<>(ConcertResponseCode.UPDATE_CONCERT_SEATS.name(), UpdatedDto.from(updatedId))
);
}

@PutMapping("/{id}/state/close")

Choose a reason for hiding this comment

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

/{id}/close 로 해도 될것 같습니다.

public ResponseEntity<ResponseDto<UpdatedDto>> changeStateToClose(@PathVariable @Positive Long id) {
long updatedId = changeStateToCloseUseCase.execute(id);

return ResponseEntity.ok(
new ResponseDto<>(ConcertResponseCode.CHANGE_STATE_TO_CLOSE.name(), UpdatedDto.from(updatedId))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package co.kr.ticketing.adminconcert.concert.controller.request;

import co.kr.ticketing.adminconcert.concert.domain.model.ConcertState;
import co.kr.ticketing.adminconcert.concert.service.dto.GetConcertListVo;

public record GetConcertListRequest(
String name,
String placeName,
ConcertState state
) {
public GetConcertListVo toVo() {
return GetConcertListVo.builder()
.name(name)
.placeName(placeName)
.state(state)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package co.kr.ticketing.adminconcert.concert.controller.request;

import java.util.List;

import co.kr.ticketing.adminconcert.concert.service.dto.ModifyConcertSeatVo;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

public record ModifyConcertSeatsRequest(
@Valid
@NotEmpty
List<ConcertSeatRequest> seats
) {
public record ConcertSeatRequest(
@NotNull
Long id,
@NotBlank
String grade,
@NotNull
Integer price
) {
public ModifyConcertSeatVo toVo() {
return ModifyConcertSeatVo.builder()
.id(id)
.grade(grade)
.price(price)
.build();
}
}

public List<ModifyConcertSeatVo> toVo() {
return seats.stream().map(ConcertSeatRequest::toVo).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public enum ConcertResponseCode {
SET_CONCERT_OPEN_TIME,
SET_CONCERT_TICKETING_START_TIME,
MODIFY_ROUNDS,
UPDATE_CONCERT_PLACE
UPDATE_CONCERT_PLACE,
UPDATE_CONCERT_SEATS,
CHANGE_STATE_TO_CLOSE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package co.kr.ticketing.adminconcert.concert.controller.response;

import java.time.LocalDateTime;
import java.util.List;

import co.kr.ticketing.adminconcert.concert.domain.model.Concert;
import co.kr.ticketing.adminconcert.concert.domain.model.ConcertState;
import lombok.Builder;

@Builder
public record GetConcertListResponse(
List<ConcertResponse> concerts
) {
@Builder
public record ConcertResponse(
Long id,
String name,
ConcertState state,
LocalDateTime openTime,
LocalDateTime ticketingStartTime,
LocalDateTime lastRunningEndTime,
String placeName
) {
public static ConcertResponse from(Concert concert) {
return ConcertResponse.builder()
.id(concert.id())
.name(concert.name())
.state(concert.state())
.openTime(concert.openTime())
.ticketingStartTime(concert.ticketingStartTime())
.lastRunningEndTime(concert.lastRunningEndTime())
.placeName(concert.place().name())
.build();
}
}

public static GetConcertListResponse from(List<Concert> concerts) {
return GetConcertListResponse.builder()
.concerts(concerts.stream().map(ConcertResponse::from).toList())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package co.kr.ticketing.adminconcert.concert.domain.infrastructure;

import java.util.List;
import java.util.Optional;

import co.kr.ticketing.adminconcert.concert.domain.model.Concert;
import co.kr.ticketing.adminconcert.concert.service.dto.GetConcertListVo;

public interface ConcertRepository {
Optional<Concert> find(Long id);

List<Concert> getList(GetConcertListVo getListVo);

long create(Concert concert);

long update(Concert concert);
Expand All @@ -16,4 +20,6 @@ public interface ConcertRepository {
long setTicketingStartTime(Concert concert);

long updatePlace(Concert concert);

long changeState(Concert concert);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

import co.kr.ticketing.adminconcert.concert.domain.model.Concert;
import co.kr.ticketing.adminconcert.concert.domain.model.ConcertSeat;
import co.kr.ticketing.adminconcert.concert.service.dto.ModifyConcertSeatVo;

public interface ConcertSeatRepository {
void create(long concertId, List<ConcertSeat> concertSeats);

void update(Concert concert);

void modify(Concert concert, List<ModifyConcertSeatVo> modifyVo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public boolean isPossibleUpdate() {
return !state.equals(ConcertState.CLOSE);
}

public boolean isPossibleUpdateSeats() {
return !isPossibleUpdate() || ticketingStartTime.isBefore(LocalDateTime.now());
}

public Concert setOpenTime(LocalDateTime openTime) {
if (!this.state.isSetOpenTimeState() ||
openTime.isBefore(LocalDateTime.now()) ||
Expand Down Expand Up @@ -141,4 +145,43 @@ public Concert updatePlace(Place place, List<ConcertSeat> seats) {
.seats(List.copyOf(seats))
.build();
}

public Concert changeState(ConcertState state) {

Choose a reason for hiding this comment

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

메서드명이 changeState 인데 실제로는 get 을 수행하는것 같습니다.

if (state == ConcertState.READY) {
throw new BadRequestException("READY 상태로 변경할 수 없습니다");
} else if (state == ConcertState.OPEN) {
isPossibleChangeStateToOpen();
} else {
isPossibleChangeStateToClose();
}

return Concert.builder()
.id(this.id)
.name(this.name)
.detailInfo(this.detailInfo)
.runningTime(this.runningTime)
.state(state)
.ticketingStartTime(this.ticketingStartTime)
.lastRunningEndTime(this.lastRunningEndTime)
.openTime(this.openTime)
.rounds(List.copyOf(this.rounds))
.place(this.place)
.seats(List.copyOf(this.seats))
.build();
}

private void isPossibleChangeStateToOpen() {
if (this.state == ConcertState.CLOSE || (this.openTime != null && this.openTime.isAfter(LocalDateTime.now()))) {
throw new BadRequestException("OPEN 상태로 변경할 수 없습니다");
}
}

private void isPossibleChangeStateToClose() {
if (this.state == ConcertState.OPEN) {
if (this.lastRunningEndTime.isAfter(LocalDateTime.now())) {
//todo) 예매된 공연이 있으면 변경 불가능한 로직 추가
throw new BadRequestException("CLOSE 상태로 변경할 수 없습니다");
}
}
}
}
Loading
Loading