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

refactor: dto에서 converter 분리 #169

Merged
merged 1 commit into from
Sep 8, 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
4 changes: 3 additions & 1 deletion src/main/java/ssuPlector/controller/ProjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import jakarta.validation.Valid;

import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -41,8 +42,9 @@ public ApiResponse<ProjectDetailDTO> getProjectDetail(
public ApiResponse<ProjectListResponseDto> getProjectList(
@Valid @ModelAttribute ProjectListRequestDto requestDto,
@RequestParam(value = "page", defaultValue = "0", required = false) int page) {
Page<Project> projectList = projectService.getProjectList(requestDto, page);
return ApiResponse.onSuccess(
"프로젝트 리스트 조회 성공", projectService.getProjectList(requestDto, page));
"프로젝트 리스트 조회 성공", ProjectConverter.toProjectListDto(projectList));
}

@Operation(summary = "프로젝트 생성, 저장 API", description = "프로젝트를 생성 후 저장합니다._찬민")
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/ssuPlector/converter/ProjectConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import java.util.ArrayList;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import ssuPlector.domain.Image;
import ssuPlector.domain.Project;
import ssuPlector.domain.ProjectDeveloper;
import ssuPlector.domain.category.DevLanguage;
import ssuPlector.domain.category.DevTools;
import ssuPlector.domain.category.TechStack;
import ssuPlector.dto.request.ProjectDTO.ProjectRequestDTO;
import ssuPlector.dto.response.ProjectDTO.ProjectDetailDTO;
import ssuPlector.dto.response.ProjectDTO.ProjectListResponseDto;
import ssuPlector.dto.response.ProjectDTO.ProjectPreviewDTO;
import ssuPlector.dto.response.ProjectDTO.ProjectResponseDto;

@Component
public class ProjectConverter {
Expand Down Expand Up @@ -71,4 +75,34 @@ public static Project toProject(
.techStackList(techStacks)
.build();
}

public static ProjectResponseDto toProjectResponseDto(Project project) {
return ProjectResponseDto.builder()
.id(project.getId())
.name(project.getName())
.imagePath(
project.getImageList() == null
? null
: project.getImageList().stream()
.filter(Image::getIsMainImage)
.findFirst()
.map(Image::getImagePath)
.orElse(null))
.shortIntro(project.getShortIntro())
.category(project.getCategory().toString())
.hits(project.getHits())
.build();
}

public static ProjectListResponseDto toProjectListDto(Page<Project> projectPage) {
return ProjectListResponseDto.builder()
.totalPage(projectPage.getTotalPages())
.currentElement(projectPage.getNumberOfElements())
.totalElement(projectPage.getTotalElements())
.projectResponseDtoList(
projectPage.getContent().stream()
.map(ProjectConverter::toProjectResponseDto)
.collect(Collectors.toList()))
.build();
}
}
49 changes: 12 additions & 37 deletions src/main/java/ssuPlector/dto/response/ProjectDTO.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package ssuPlector.dto.response;

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

import org.springframework.data.domain.Page;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import ssuPlector.domain.Image;
import ssuPlector.domain.Project;
import ssuPlector.domain.category.Category;
import ssuPlector.domain.category.DevLanguage;
import ssuPlector.domain.category.DevTools;
Expand Down Expand Up @@ -53,46 +48,26 @@ public static class ProjectDetailDTO {
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ProjectListResponseDto {

private int currentElement; // 현재 페이지 아이템 개수
private int totalPage; // 전체 페이지
private long totalElement; // 전체 아이템 개수
private List<ProjectResponseDto> projectResponseDtoList;

public ProjectListResponseDto(Page<Project> projectPage) {
List<ProjectResponseDto> projectResponseDtoList =
projectPage.getContent().stream()
.map(ProjectResponseDto::new)
.collect(Collectors.toList());
this.projectResponseDtoList = projectResponseDtoList;
this.totalPage = projectPage.getTotalPages();
this.currentElement = projectPage.getNumberOfElements();
this.totalElement = projectPage.getTotalElements();
}
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ProjectResponseDto {
private Long id;
private String name;
private String imagePath;
private String shortIntro;
private String category;
private long hits;

public ProjectResponseDto(Project project) {
this.id = project.getId();
this.name = project.getName();
if (project.getImageList() == null) this.imagePath = null;
else if (project.getImageList().size() == 1)
this.imagePath = project.getImageList().get(0).getImagePath();
else
for (Image image : project.getImageList())
if (image.getIsMainImage()) this.imagePath = image.getImagePath();
this.shortIntro = project.getShortIntro();
this.category = project.getCategory().name();
this.hits = project.getHits();
}
Long id;
String name;
String imagePath;
String shortIntro;
String category;
long hits;
}
}
4 changes: 2 additions & 2 deletions src/main/java/ssuPlector/service/project/ProjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.web.multipart.MultipartFile;

import ssuPlector.domain.Project;
import ssuPlector.dto.request.ProjectDTO.ProjectListRequestDto;
import ssuPlector.dto.response.ProjectDTO.ProjectListResponseDto;

public interface ProjectService {

Expand All @@ -22,7 +22,7 @@ public interface ProjectService {

boolean existsByProjectId(Long id);

ProjectListResponseDto getProjectList(ProjectListRequestDto requestDto, int page);
Page<Project> getProjectList(ProjectListRequestDto requestDto, int page);

Long updateProject(Long projectId, ProjectUpdateRequestDTO requestDTO);
}
15 changes: 7 additions & 8 deletions src/main/java/ssuPlector/service/project/ProjectServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.stream.Collectors;

import org.apache.commons.lang3.EnumUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -29,7 +30,6 @@
import ssuPlector.domain.category.Part;
import ssuPlector.domain.category.TechStack;
import ssuPlector.dto.request.ProjectDTO.ProjectListRequestDto;
import ssuPlector.dto.response.ProjectDTO.ProjectListResponseDto;
import ssuPlector.global.exception.GlobalException;
import ssuPlector.global.response.code.GlobalErrorCode;
import ssuPlector.redis.service.ProjectHitsService;
Expand Down Expand Up @@ -77,18 +77,17 @@ public boolean existsByProjectId(Long id) {
}

@Override
public ProjectListResponseDto getProjectList(ProjectListRequestDto requestDto, int page) {
public Page<Project> getProjectList(ProjectListRequestDto requestDto, int page) {
Pageable pageable = PageRequest.of(page, 4);
String category = requestDto.getCategory();
if ((category != null && !category.isBlank())
&& !EnumUtils.isValidEnum(Category.class, category))
throw new GlobalException(GlobalErrorCode.CATEGORY_NOT_FOUND);
return new ProjectListResponseDto(
projectRepository.findProjects(
requestDto.getSearchString(),
requestDto.getCategory(),
requestDto.getSortType(),
pageable));
return projectRepository.findProjects(
requestDto.getSearchString(),
requestDto.getCategory(),
requestDto.getSortType(),
pageable);
}

@Override
Expand Down
Loading