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

[DEV-301] Ticket CrudService 마이그레이션 #115

Merged
merged 3 commits into from
Jun 11, 2024
Merged
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
Expand Up @@ -73,11 +73,12 @@ public class Ticket {
private Integer version;

@Builder
public Ticket(long price, int stock, int remainingStock, String title, Ticketing ticketing) {
public Ticket(long price, int stock, int remainingStock, String title, String description, Ticketing ticketing) {
this.price = price;
this.stock = stock;
this.remainingStock = remainingStock;
this.title = title;
this.description = description;
setTicketing(ticketing);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tiketeer.Tiketeer.domain.ticket.service;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

Expand All @@ -10,6 +9,7 @@

import com.tiketeer.Tiketeer.domain.ticket.Ticket;
import com.tiketeer.Tiketeer.domain.ticket.repository.TicketRepository;
import com.tiketeer.Tiketeer.domain.ticket.usecase.dto.CreateTicketCommandDto;
import com.tiketeer.Tiketeer.domain.ticketing.Ticketing;
import com.tiketeer.Tiketeer.domain.ticketing.exception.TicketingNotFoundException;
import com.tiketeer.Tiketeer.domain.ticketing.repository.TicketingRepository;
Expand All @@ -32,12 +32,21 @@ public List<Ticket> listTicketByTicketingId(UUID ticketingId) {
}

@Transactional
public void createTickets(UUID ticketingId, int numOfTickets) {
var ticketing = findTicketingById(ticketingId);

ticketRepository.saveAll(Arrays.stream(new int[numOfTickets])
.mapToObj(i -> Ticket.builder().ticketing(ticketing).build())
public void createTickets(CreateTicketCommandDto dto) {
var ticketing = findTicketingById(dto.getTicketingId());

ticketRepository.saveAll(dto.getCreateTicketMetadataList().stream()
.map(meta -> Ticket.builder()
.ticketing(ticketing)
.price(meta.getPrice())
.stock(meta.getStock())
.remainingStock(meta.getRemainingStock())
.title(meta.getTitle())
.description(meta.getDescription())
.build()
)
.toList());

}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tiketeer.Tiketeer.domain.ticket.usecase.dto;

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

import lombok.Builder;
Expand All @@ -11,15 +11,12 @@
@ToString
public class CreateTicketCommandDto {
private final UUID ticketingId;
private final int numOfTickets;
private LocalDateTime commandCreatedAt = LocalDateTime.now();

private final List<CreateTicketMetadata> createTicketMetadataList;

@Builder
public CreateTicketCommandDto(UUID ticketingId, int numOfTickets, LocalDateTime commandCreatedAt) {
public CreateTicketCommandDto(UUID ticketingId, List<CreateTicketMetadata> createTicketMetadataList) {
this.ticketingId = ticketingId;
this.numOfTickets = numOfTickets;
if (commandCreatedAt != null) {
this.commandCreatedAt = commandCreatedAt;
}
this.createTicketMetadataList = createTicketMetadataList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tiketeer.Tiketeer.domain.ticket.usecase.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class CreateTicketMetadata {

private final int stock;
private final int remainingStock;
private final String title;
private final String description;
private final long price;

@Builder
public CreateTicketMetadata(int stock, int remainingStock, String title, String description, long price) {
this.stock = stock;
this.remainingStock = remainingStock;
this.title = title;
this.description = description;
this.price = price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tiketeer.Tiketeer.domain.ticket.repository.TicketRepository;
import com.tiketeer.Tiketeer.domain.ticket.service.TicketCrudService;
import com.tiketeer.Tiketeer.domain.ticketing.Ticketing;
import com.tiketeer.Tiketeer.domain.ticketing.exception.EventTimeNotValidException;
import com.tiketeer.Tiketeer.domain.ticketing.exception.ModifyForNotOwnedTicketingException;
Expand All @@ -21,15 +19,10 @@
@Transactional(readOnly = true)
public class TicketingService {
private final TicketingRepository ticketingRepository;
private final TicketCrudService ticketCrudService;
private final TicketRepository ticketRepository;

@Autowired
public TicketingService(TicketingRepository ticketingRepository, TicketCrudService ticketCrudService,
TicketRepository ticketRepository) {
public TicketingService(TicketingRepository ticketingRepository) {
this.ticketingRepository = ticketingRepository;
this.ticketCrudService = ticketCrudService;
this.ticketRepository = ticketRepository;
}

public Ticketing findById(UUID ticketingId) {
Expand Down Expand Up @@ -77,6 +70,5 @@ public void validateTicketingOwnership(Ticketing ticketing, String email) {
if (!ticketing.getMember().getEmail().equals(email)) {
throw new ModifyForNotOwnedTicketingException();
}
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.tiketeer.Tiketeer.domain.ticket.Ticket;
import com.tiketeer.Tiketeer.domain.ticket.service.TicketCrudService;
import com.tiketeer.Tiketeer.domain.ticket.usecase.dto.CreateTicketCommandDto;

@Service
@Transactional(readOnly = true)
Expand All @@ -24,7 +25,8 @@ public TicketingStockService(TicketCrudService ticketCrudService, TicketingServi
@Transactional
public void createStock(UUID ticketingId, int stock) {
var ticketing = ticketingService.findById(ticketingId);
ticketCrudService.createTickets(ticketing.getId(), stock);
//TODO 마이그레이션 반영 필요
ticketCrudService.createTickets(CreateTicketCommandDto.builder().build());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

여기 건들기 시작하면 끝도없어서 일단 보류

Copy link
Contributor

Choose a reason for hiding this comment

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

요 부분 티켓팅 생성이랑 관련 있어서 제 PR에서 작업해서 올리겠슴다

}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ void getMemberTicketingSalesSuccess() throws Exception {
.build()
);
var purchase = purchaseRepository.save(new Purchase(member));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing));

//when - then
MvcResult result = mockMvc.perform(get("/api/members/" + member.getId() + "/sale")
Expand Down Expand Up @@ -364,10 +364,10 @@ void getMemberPurchasesSuccess() throws Exception {
);
var purchase1 = purchaseRepository.save(new Purchase(member));
var purchase2 = purchaseRepository.save(new Purchase(member));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing2));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing2));

// when
var result = mockMvc.perform(get("/api/members/{memberId}/purchases", member.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ void getMemberTicketingSalesSuccess() {
.build()
);
var purchase = purchaseRepository.save(new Purchase(member));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing));

//when
var memberTicketingSale = getMemberTicketingSalesUseCase.getMemberTicketingSales(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ void getMemberPurchases() {
);
var purchase1 = purchaseRepository.save(new Purchase(member));
var purchase2 = purchaseRepository.save(new Purchase(member));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing2));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing2));

// when
var results = getMemberPurchasesUseCase.getMemberPurchases(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ void findWithTicketingByMember() {
);
var purchase1 = purchaseRepository.save(new Purchase(member));
var purchase2 = purchaseRepository.save(new Purchase(member));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 2", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 3", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 4", ticketing2));
ticketRepository.save(new Ticket(1000, 1, 1, "title 1", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 2", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 3", "description 1", ticketing1));
ticketRepository.save(new Ticket(1000, 1, 1, "title 4", "description 1", ticketing2));

// when
var results = purchaseRepository.findWithTicketingByMember(member);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.tiketeer.Tiketeer.domain.ticket.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;

import org.assertj.core.api.Assertions;
Expand All @@ -15,14 +17,13 @@
import org.springframework.transaction.annotation.Transactional;

import com.tiketeer.Tiketeer.domain.member.Member;
import com.tiketeer.Tiketeer.domain.member.repository.MemberRepository;
import com.tiketeer.Tiketeer.domain.role.repository.RoleRepository;
import com.tiketeer.Tiketeer.domain.ticket.Ticket;
import com.tiketeer.Tiketeer.domain.ticket.repository.TicketRepository;
import com.tiketeer.Tiketeer.domain.ticket.usecase.dto.CreateTicketCommandDto;
import com.tiketeer.Tiketeer.domain.ticket.usecase.dto.CreateTicketMetadata;
import com.tiketeer.Tiketeer.domain.ticketing.Ticketing;
import com.tiketeer.Tiketeer.domain.ticketing.exception.TicketingNotFoundException;
import com.tiketeer.Tiketeer.domain.ticketing.repository.TicketingRepository;
import com.tiketeer.Tiketeer.domain.ticketing.service.TicketingService;
import com.tiketeer.Tiketeer.testhelper.TestHelper;

@Import({TestHelper.class})
Expand All @@ -34,12 +35,6 @@ public class TicketCrudServiceTest {
@Autowired
private TicketCrudService ticketCrudService;
@Autowired
private TicketingService ticketingService;
@Autowired
private MemberRepository memberRepository;
@Autowired
private RoleRepository roleRepository;
@Autowired
private TicketRepository ticketRepository;
@Autowired
private TicketingRepository ticketingRepository;
Expand Down Expand Up @@ -79,11 +74,11 @@ void listTicketByTicketingSuccess() {

var mockStock = 30;

var ticketingId = createTicketingAndReturnId(member, mockStock, now.plusYears(1), now.plusYears(2),
now.plusYears(3));
var ticketing = createTicketing(member, now.plusYears(1), now.plusYears(2), now.plusYears(3));
createTickets(ticketing, 5, mockStock, mockStock, "", "", 1000);

// when
var tickets = ticketCrudService.listTicketByTicketingId(ticketingId);
var tickets = ticketCrudService.listTicketByTicketingId(ticketing.getId());

// then
Assertions.assertThat(tickets.size()).isEqualTo(mockStock);
Expand All @@ -97,13 +92,16 @@ void createTicketsFailBecauseNotExistTicketing() {

Assertions.assertThatThrownBy(() -> {
// when
ticketCrudService.createTickets(invalidTicketingId, 100);
ticketCrudService.createTickets(CreateTicketCommandDto.builder()
.ticketingId(invalidTicketingId)
.createTicketMetadataList(new ArrayList<>())
.build());
// then
}).isInstanceOf(TicketingNotFoundException.class);
}

@Test
@DisplayName("유효한 티케팅 (기존 티켓 10) > 추가 하위 티켓 생성 요청 (20) > 성공 및 총 재고 10 + 20")
@DisplayName("유효한 티케팅 (티켓 10종류 * 10장) > 추가 하위 티켓 생성 요청 (1종류 20장) > 성공 및 총 재고 10*10 + 20")
void createTicketsSuccess() {
// given
var now = LocalDateTime.now();
Expand All @@ -112,22 +110,37 @@ void createTicketsSuccess() {
var member = testHelper.createMember(mockEmail);

var mockStock = 10;
var ticketingId = createTicketingAndReturnId(member, mockStock, now.plusYears(1), now.plusYears(2),
now.plusYears(3));
var ticketing = createTicketing(member, now.plusYears(1), now.plusYears(2), now.plusYears(3));
createTickets(ticketing, 10, mockStock, mockStock, "", "", 1000);

var addTickets = 20;

// when
ticketCrudService.createTickets(ticketingId, addTickets);
ticketCrudService.createTickets(CreateTicketCommandDto.builder()
.ticketingId(ticketing.getId())
.createTicketMetadataList(Collections.singletonList(
CreateTicketMetadata.builder()
.stock(addTickets)
.remainingStock(addTickets)
.title("")
.description("")
.price(2000)
.build()
))
.build());

// then
var tickets = ticketCrudService.listTicketByTicketingId(ticketingId);
Assertions.assertThat(tickets.size()).isEqualTo(mockStock + addTickets);
var tickets = ticketCrudService.listTicketByTicketingId(ticketing.getId());
Assertions.assertThat(tickets.size()).isEqualTo(mockStock + 1);

var ticketCount = tickets.stream().mapToInt(Ticket::getStock).sum();
Assertions.assertThat(ticketCount).isEqualTo(120);
}

private UUID createTicketingAndReturnId(Member member, int stock, LocalDateTime saleStart, LocalDateTime saleEnd,
private Ticketing createTicketing(Member member, LocalDateTime saleStart,
LocalDateTime saleEnd,
LocalDateTime eventTime) {
var ticketing = ticketingRepository.save(Ticketing.builder()
return ticketingRepository.save(Ticketing.builder()
.member(member)
.title("음악회")
.location("서울 강남역 8번 출구")
Expand All @@ -136,9 +149,21 @@ private UUID createTicketingAndReturnId(Member member, int stock, LocalDateTime
.saleStart(saleStart)
.saleEnd(saleEnd)
.eventTime(eventTime).build());
ticketRepository.saveAll(Arrays.stream(new int[stock])
.mapToObj(i -> Ticket.builder().ticketing(ticketing).build())

}

private void createTickets(Ticketing ticketing, int distinct, int stock, int remainingStock, String title,
String description,
long price) {
ticketRepository.saveAll(Arrays.stream(new int[distinct])
.mapToObj(i -> Ticket.builder()
.ticketing(ticketing)
.price(price)
.title(title)
.description(description)
.stock(stock)
.remainingStock(remainingStock)
.build())
.toList());
return ticketing.getId();
}
}
Loading
Loading