Skip to content

Commit

Permalink
Merge pull request #103 from BbeumbungE/feat/refactor
Browse files Browse the repository at this point in the history
refactor: 동적 프로젝션 추가
  • Loading branch information
Fishphobiagg authored Jan 10, 2024
2 parents 9ee7c4a + d54a80d commit 2952acc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public Canvas findById(Long canvasId){
}

public Response findByProfileId(Long profileId){
List<CanvasListSummary> canvasPage = canvasRepository.findByProfileId(profileId);
List<CanvasListSummary> canvasPage = canvasRepository.findByProfileId(profileId, CanvasListSummary.class);
return Response.of(CanvasCode.GET_CANVAS_SUCCESS, canvasPage);
}

public List<CanvasListSummary> findCanvasListByProfileId(Long profileId){
return canvasRepository.findByProfileId(profileId);
return canvasRepository.findByProfileId(profileId, CanvasListSummary.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface CanvasRepository extends JpaRepository<Canvas, Long> {

@Query("SELECT new com.siliconvalley.domain.canvas.dto.CanvasDto(c.id, c.canvas) FROM Canvas c WHERE c.profileId = ?1")
List<CanvasDto> findCanvasByProfileId(Long profileId);
List<CanvasListSummary> findByProfileId(Long profileId);
// List<CanvasListSummary> findByProfileId(Long profileId);

<T> List<T> findByProfileId(Long profileId, Class<T> type);
Optional<Canvas> findCanvasByIdAndProfileId(Long canvasId, Long profileId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class Canvas {
@Column(name = "profile_id")
private Long profileId;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "subject_id", nullable = false)
private Subject subject;

@OneToOne(mappedBy = "canvas", orphanRemoval = true, cascade = CascadeType.PERSIST)
@OneToOne(mappedBy = "canvas", orphanRemoval = true, cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private Post post;

public Post buildPost(){
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/siliconvalley/domain/canvas/dto/CanvasDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CanvasDto {

private Long canvasId;
private String canvasUrl;
private Long id;
private String canvas;

@Builder
public CanvasDto(Long canvasId, String canvasUrl){
this.canvasId = canvasId;
this.canvasUrl = canvasUrl;
public CanvasDto(Long id, String canvas){
this.id = id;
this.canvas = canvas;
}
}
24 changes: 21 additions & 3 deletions src/test/java/com/siliconvalley/canvas/ProjectionTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.siliconvalley.canvas;

import com.siliconvalley.domain.canvas.dao.CanvasRepository;
import com.siliconvalley.domain.canvas.domain.Canvas;
import com.siliconvalley.domain.canvas.dto.CanvasDto;
import com.siliconvalley.domain.canvas.dto.CanvasListSummary;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,6 +14,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@Slf4j
public class ProjectionTest {

@Autowired
Expand All @@ -26,7 +29,7 @@ public class ProjectionTest {
Long profileId = 1L;

//when
List<CanvasListSummary> canvasListSummaryList = canvasRepository.findByProfileId(profileId);
List<CanvasListSummary> canvasListSummaryList = canvasRepository.findByProfileId(profileId, CanvasListSummary.class);

//then
CanvasListSummary summary = canvasListSummaryList.get(0);
Expand All @@ -48,7 +51,22 @@ public class ProjectionTest {

//then
CanvasDto canvasDto = canvasDtoList.get(0);
assertThat(canvasDto.getCanvasId()).isEqualTo(testCanvasId);
assertThat(canvasDto.getCanvasUrl()).isEqualTo(testUrl);
assertThat(canvasDto.getId()).isEqualTo(testCanvasId);
assertThat(canvasDto.getCanvas()).isEqualTo(testUrl);
}

@Test
@DisplayName("동적 프로젝션 확인")
void 동적_프로젝션_확인(){
//given
Long profileId = 1L;

//when
log.info("클래스 DTO");
List<CanvasDto> canvasDtoList = canvasRepository.findByProfileId(profileId, CanvasDto.class);
log.info("인터페이스 타입");
List<CanvasListSummary> canvasListSummaries = canvasRepository.findByProfileId(profileId, CanvasListSummary.class);
log.info("엔티티 객체 타입");
List<Canvas> canvasList = canvasRepository.findByProfileId(profileId, Canvas.class);
}
}

0 comments on commit 2952acc

Please sign in to comment.