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

[BE] feat: StageTicket 생성, 삭제 로직 추가 (#1007-2) #1009

Open
wants to merge 1 commit into
base: feat/#1007-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.festago.stage.application.command;

import com.festago.stage.domain.validator.StageDeleteValidator;
import com.festago.stage.dto.event.StageDeletedEvent;
import com.festago.stage.repository.StageRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
Expand All @@ -13,10 +15,12 @@
public class StageDeleteService {

private final StageRepository stageRepository;
private final List<StageDeleteValidator> validators;
private final ApplicationEventPublisher eventPublisher;

public void deleteStage(Long stageId) {
stageRepository.findById(stageId).ifPresent(stage -> {
validators.forEach(validator -> validator.validate(stage));
stageRepository.deleteById(stageId);
eventPublisher.publishEvent(new StageDeletedEvent(stage));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.festago.common.exception.NotFoundException;
import com.festago.common.util.Validator;
import com.festago.stage.domain.Stage;
import com.festago.stage.domain.validator.StageUpdateValidator;
import com.festago.stage.dto.command.StageUpdateCommand;
import com.festago.stage.dto.event.StageUpdatedEvent;
import com.festago.stage.repository.StageRepository;
Expand All @@ -24,6 +25,7 @@ public class StageUpdateService {

private final StageRepository stageRepository;
private final ArtistRepository artistRepository;
private final List<StageUpdateValidator> validators;
private final ApplicationEventPublisher eventPublisher;

public void updateStage(Long stageId, StageUpdateCommand command) {
Expand All @@ -35,6 +37,7 @@ public void updateStage(Long stageId, StageUpdateCommand command) {
.orElseThrow(() -> new NotFoundException(ErrorCode.STAGE_NOT_FOUND));
stage.changeTime(startTime, ticketOpenTime);
stage.renewArtists(artistIds);
validators.forEach(validator -> validator.validate(stage));
eventPublisher.publishEvent(new StageUpdatedEvent(stage));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.festago.stage.domain.validator;

import com.festago.stage.domain.Stage;

public interface StageDeleteValidator {

void validate(Stage stage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.festago.stage.domain.validator;

import com.festago.stage.domain.Stage;

public interface StageUpdateValidator {

void validate(Stage stage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ default Stage getOrThrow(Long stageId) {
where s.id = :id
""")
Optional<Stage> findByIdWithFetch(@Param("id") Long id);

@Query("""
select s
from Stage s
join fetch s.festival
where s.id = :id
""")
Optional<Stage> findByIdWithFetchFestival(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.festago.ticket.application.command.stage;

import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.NotFoundException;
import com.festago.stage.domain.Stage;
import com.festago.stage.repository.StageRepository;
import com.festago.ticket.domain.StageTicket;
import com.festago.ticket.domain.TicketExclusive;
import com.festago.ticket.dto.command.StageTicketCreateCommand;
import com.festago.ticket.dto.event.TicketCreatedEvent;
import com.festago.ticket.repository.StageTicketRepository;
import java.time.Clock;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class StageTicketCreateService {

private final StageTicketRepository stageTicketRepository;
private final StageRepository stageRepository;
private final ApplicationEventPublisher eventPublisher;
private final Clock clock;

public Long createStageTicket(StageTicketCreateCommand command) {
Long schoolId = command.schoolId();
Long stageId = command.stageId();
TicketExclusive ticketType = command.ticketExclusive();
StageTicket stageTicket = stageTicketRepository.findByStageIdAndTicketExclusiveWithFetch(stageId, ticketType)
.orElseGet(() -> {
Stage stage = findStage(stageId);
return stageTicketRepository.save(new StageTicket(schoolId, ticketType, stage));
});
LocalDateTime entryTime = command.entryTime();
int amount = command.amount();
stageTicket.addTicketEntryTime(schoolId, LocalDateTime.now(clock), entryTime, amount);
eventPublisher.publishEvent(new TicketCreatedEvent(stageTicket));
return stageTicket.getId();
}

private Stage findStage(Long stageId) {
return stageRepository.findByIdWithFetchFestival(stageId)
.orElseThrow(() -> new NotFoundException(ErrorCode.STAGE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.festago.ticket.application.command.stage;

import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.NotFoundException;
import com.festago.ticket.domain.StageTicket;
import com.festago.ticket.dto.command.StageTicketDeleteCommand;
import com.festago.ticket.dto.event.TicketDeletedEvent;
import com.festago.ticket.repository.StageTicketRepository;
import java.time.Clock;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class StageTicketDeleteService {

private final StageTicketRepository stageTicketRepository;
private final ApplicationEventPublisher eventPublisher;
private final Clock clock;

public void deleteStageTicket(StageTicketDeleteCommand command) {
Long ticketId = command.stageTicketId();
StageTicket stageTicket = stageTicketRepository.findByIdWithFetch(ticketId)
.orElseThrow(() -> new NotFoundException(ErrorCode.TICKET_NOT_FOUND));
Long schoolId = command.schoolId();
LocalDateTime now = LocalDateTime.now(clock);
LocalDateTime entryTime = command.entryTime();
boolean isDeleted = stageTicket.deleteTicketEntryTime(schoolId, now, entryTime);
if (stageTicket.isEmptyAmount()) {
stageTicketRepository.deleteById(ticketId);
}
if (isDeleted) {
eventPublisher.publishEvent(new TicketDeletedEvent(stageTicket));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.festago.ticket.dto.command;

import com.festago.ticket.domain.TicketExclusive;
import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record StageTicketCreateCommand(
Long schoolId,
Long stageId,
TicketExclusive ticketExclusive,
int amount,
LocalDateTime entryTime
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.festago.ticket.dto.command;

import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record StageTicketDeleteCommand(
Long schoolId,
Long stageTicketId,
LocalDateTime entryTime
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.festago.ticket.dto.event;

import com.festago.ticket.domain.NewTicket;

public record TicketCreatedEvent(
NewTicket ticket
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.festago.ticket.dto.event;

import com.festago.ticket.domain.NewTicket;

public record TicketDeletedEvent(
NewTicket ticket
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.festago.ticket.repository;

import com.festago.ticket.domain.StageTicket;
import com.festago.ticket.domain.TicketExclusive;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

public interface StageTicketRepository extends Repository<StageTicket, Long> {

StageTicket save(StageTicket stageTicket);

Optional<StageTicket> findById(Long id);

boolean existsByStageId(Long stageId);

void deleteById(Long id);

@Query("""
select st
from StageTicket st
join fetch st.stage
join fetch st.ticketEntryTimes
where st.id = :id
""")
Optional<StageTicket> findByIdWithFetch(@Param("id") Long id);

@Query("""
select st
from StageTicket st
join fetch st.stage
join fetch st.ticketEntryTimes
where st.stage.id = :stageId and st.ticketExclusive = :ticketExclusive
""")
Optional<StageTicket> findByStageIdAndTicketExclusiveWithFetch(
@Param("stageId") Long stageId,
@Param("ticketExclusive") TicketExclusive ticketExclusive
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.festago.support.fixture.FestivalFixture;
import com.festago.support.fixture.StageFixture;
import java.time.LocalDateTime;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand Down Expand Up @@ -46,7 +47,11 @@ void setUp() {
artistRepository = new MemoryArtistRepository();
festivalRepository = new MemoryFestivalRepository();
stageRepository = new MemoryStageRepository();
stageDeleteService = new StageDeleteService(stageRepository, mock(ApplicationEventPublisher.class));
stageDeleteService = new StageDeleteService(
stageRepository,
Collections.emptyList(),
mock(ApplicationEventPublisher.class)
);

테코대학교_축제 = festivalRepository.save(
FestivalFixture.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.festago.support.fixture.FestivalFixture;
import com.festago.support.fixture.StageFixture;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.LongStream;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -52,6 +53,7 @@ void setUp() {
stageUpdateService = new StageUpdateService(
stageRepository,
artistRepository,
Collections.emptyList(),
mock(ApplicationEventPublisher.class)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public List<Stage> findAllByFestivalId(Long festivalId) {
public Optional<Stage> findByIdWithFetch(Long id) {
return findById(id);
}

@Override
public Optional<Stage> findByIdWithFetchFestival(Long id) {
return findById(id);
}
}
Loading
Loading