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

공연 등록 테스트 및 리팩토링 #3

Open
wants to merge 17 commits into
base: develop
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
@@ -0,0 +1,29 @@
package com.jummi.ticket.common.adapter.persistence;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class BaseEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime updatedAt;

@Column(columnDefinition = "boolean default false")
private boolean isDeleted;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jummi.ticket.config.jpa;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@Configuration
public class JpaConfiguration {
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.jummi.ticket.performance.adapter.persistence;

import com.jummi.ticket.performance.application.port.out.SavePerformancePort;
import com.jummi.ticket.performance.application.port.out.SavePerformanceCommand;
import com.jummi.ticket.performance.converter.PerformanceMapper;
import com.jummi.ticket.performance.domain.Performance;
import com.jummi.ticket.performance.domain.Series;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

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

@Component
@Repository
@RequiredArgsConstructor
@Transactional(readOnly = true)
class PerformanceAdapter implements SavePerformancePort {
class PerformanceAdapter implements SavePerformanceCommand {
private final PerformanceMapper mapper;
private final PerformanceRepository performanceRepository;
private final SeriesRepository seriesRepository;
Expand All @@ -24,14 +23,10 @@ class PerformanceAdapter implements SavePerformancePort {
public Long savePerformance(Performance performance, List<Series> series) {
PerformanceEntity performanceEntity = mapper.convertDomainEntityToJpaEntity(performance);
PerformanceEntity saved = performanceRepository.save(performanceEntity);
Long performanceId = saved.getPerformanceId();

List<SeriesEntity> seriesEntities = series.stream()
.map(mapper::convertDomainEntityToJpaEntity)
.collect(Collectors.toList());
seriesEntities.forEach(seriesEntity -> seriesEntity.setPerformanceId(performanceId));
List<SeriesEntity> seriesEntities = mapper.convertDomainEntitiesToJpaEntities(series, saved);
seriesRepository.saveAll(seriesEntities);

return performanceId;
return saved.getPerformanceId();
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.jummi.ticket.performance.adapter.persistence;

import com.jummi.ticket.common.adapter.persistence.BaseEntity;
import com.jummi.ticket.performance.domain.Genre;
import com.jummi.ticket.performance.domain.ReservationSite;
import lombok.*;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "performance")
@Where(clause = "is_deleted = false")
Copy link

Choose a reason for hiding this comment

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

요 부분을 이렇게 작업해두신 이유가 있을까욤???? 어떤 견해이신지 궁금하네요ㅎㅎ

@SQLDelete(sql = "UPDATE performance SET is_deleted = true WHERE performance_id = ?")
@Entity
public class PerformanceEntity {
public class PerformanceEntity extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long performanceId;
Expand All @@ -32,26 +35,12 @@ public class PerformanceEntity {

private String venue;

@ElementCollection
@CollectionTable(
name = "series",
joinColumns = @JoinColumn(name = "performance_id")
)
private List<SeriesEntity> series = new ArrayList<>();

@ElementCollection
@Column(name = "casts")
private List<String> casts = new ArrayList<>();

private int runTime;

private int minAvailableAge;

@Enumerated(EnumType.STRING)
@ElementCollection
@CollectionTable(
name = "reservation_sites",
joinColumns = @JoinColumn(name = "performance_id")
)
private Set<ReservationSiteEntity> reservationSites = new HashSet<>();
@ElementCollection(targetClass = ReservationSite.class)
@CollectionTable
private Set<ReservationSite> reservationSites;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
package com.jummi.ticket.performance.adapter.persistence;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.Setter;
import com.jummi.ticket.common.adapter.persistence.BaseEntity;
import lombok.*;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.Embeddable;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Embeddable
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class SeriesEntity {
@Table(name = "series")
@Where(clause = "is_deleted = false")
@SQLDelete(sql = "UPDATE series SET is_deleted = true WHERE series_id = ?")
@Entity
public class SeriesEntity extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long seriesId;

@Setter
private Long performanceId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "performance_id")
private PerformanceEntity performance;

private LocalDate playDate;

private LocalTime playTime;

@ElementCollection
@Column(name = "casts")
private List<String> casts;

private boolean isPerformed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.springframework.data.jpa.repository.JpaRepository;

public interface SeriesRepository extends JpaRepository<SeriesEntity, Long> {
interface SeriesRepository extends JpaRepository<SeriesEntity, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.jummi.ticket.performance.adapter.web;

import com.jummi.ticket.performance.application.port.in.RegisterPerformanceCommand;
import com.jummi.ticket.performance.application.port.in.RegisterPerformanceRequest;
import com.jummi.ticket.performance.domain.PerformanceId;
import com.jummi.ticket.performance.application.port.in.RegisterPerformanceUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -17,14 +15,14 @@
@RequestMapping("/api/performances")
@RestController
class RegisterPerformanceController {
private final RegisterPerformanceCommand registerPerformanceCommand;
private final RegisterPerformanceUseCase registerPerformanceUseCase;

@PostMapping
ResponseEntity<Void> registerPerformance(@RequestBody @Valid RegisterPerformanceRequest request) {
PerformanceId performanceId = registerPerformanceCommand.registerPerformance(request);
Long performanceId = registerPerformanceUseCase.registerPerformance(request);

return ResponseEntity.created(
URI.create("/api/performances/" + performanceId.id()))
URI.create("/api/performances/" + performanceId))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jummi.ticket.performance.application.port.in;
package com.jummi.ticket.performance.adapter.web;

import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -35,9 +35,6 @@ public class RegisterPerformanceRequest {
@NotNull
private final List<SeriesRequest> series;

@NotBlank
private final List<String> casts;

@Positive
private final int runTime;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.jummi.ticket.performance.application.port.in;
package com.jummi.ticket.performance.adapter.web;

import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

@Builder
@RequiredArgsConstructor
Expand All @@ -16,5 +18,8 @@ public class SeriesRequest {
@DateTimeFormat(pattern = "yyyyMMddHHmm")
private final LocalDateTime dateTime;

@NotBlank
Copy link

Choose a reason for hiding this comment

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

@notempty가 아니고 @notblank로도 되나요??
테스트 코드를 잘 작성해두면 요런거 테스트도 금방 해보는 장점이 있을 것 같습니당ㅎㅎ

private final List<String> casts;

private final boolean isPerformed;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jummi.ticket.performance.application.port.in;

import com.jummi.ticket.performance.adapter.web.RegisterPerformanceRequest;

public interface RegisterPerformanceUseCase {
Long registerPerformance(RegisterPerformanceRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

import java.util.List;

public interface SavePerformancePort {
public interface SavePerformanceCommand {
Long savePerformance(Performance performance, List<Series> series);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.jummi.ticket.performance.application.service;

import com.jummi.ticket.performance.application.port.in.RegisterPerformanceCommand;
import com.jummi.ticket.performance.application.port.in.RegisterPerformanceRequest;
import com.jummi.ticket.performance.application.port.out.SavePerformancePort;
import com.jummi.ticket.performance.adapter.web.RegisterPerformanceRequest;
import com.jummi.ticket.performance.application.port.in.RegisterPerformanceUseCase;
import com.jummi.ticket.performance.application.port.out.SavePerformanceCommand;
import com.jummi.ticket.performance.converter.PerformanceMapper;
import com.jummi.ticket.performance.domain.Performance;
import com.jummi.ticket.performance.domain.PerformanceId;
import com.jummi.ticket.performance.domain.Series;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -14,16 +13,15 @@

@RequiredArgsConstructor
@Service
public class RegisterPerformanceService implements RegisterPerformanceCommand {
public class RegisterPerformanceService implements RegisterPerformanceUseCase {
private final PerformanceMapper mapper;
private final SavePerformancePort savePerformancePort;
private final SavePerformanceCommand savePerformanceCommand;

@Override
public PerformanceId registerPerformance(RegisterPerformanceRequest request) {
public Long registerPerformance(RegisterPerformanceRequest request) {
Performance performance = mapper.convertRequestToDomainEntity(request);
Copy link

Choose a reason for hiding this comment

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

Performance 도메인 객체에는 List가 존재하는데, 변환 시에는 없네욤...??
이렇게 작업하신 이유가 있을까요~? 도메인 객체에서 빼던지 or 변환 시에 같이 변환해주는게 좋지 않을까 싶은데용
만약 Performance에서 List를 사용하려는 개발자는 의도치 않게 NPE를 마주할 것 같습니당

Copy link
Contributor Author

Choose a reason for hiding this comment

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

수정하겠습니다!

List<Series> series = mapper.extractSeriesDomainEntity(request);
Long performanceId = savePerformancePort.savePerformance(performance, series);

return new PerformanceId(performanceId);
return savePerformanceCommand.savePerformance(performance, series);
}
}
Loading