diff --git a/backend/src/main/java/com/worlabel/domain/image/repository/ImageRepository.java b/backend/src/main/java/com/worlabel/domain/image/repository/ImageRepository.java index 07d576e..11e471b 100644 --- a/backend/src/main/java/com/worlabel/domain/image/repository/ImageRepository.java +++ b/backend/src/main/java/com/worlabel/domain/image/repository/ImageRepository.java @@ -23,6 +23,13 @@ public interface ImageRepository extends JpaRepository { Optional findByIdAndFolderIdAndFolderProjectId(Long imageId, Integer folderId, Integer projectId); + @Query(value = "SELECT i.* FROM project_image i " + + "JOIN folder f ON i.folder_id = f.folder_id " + + "JOIN project p ON f.project_id = p.project_id " + + "WHERE p.project_id = :projectId " + + "LIMIT 1", nativeQuery = true) + Optional findFirstImageByProjectId(@Param("projectId") Integer projectId); + @Query("SELECT i FROM Image i " + "JOIN FETCH i.folder f " + "JOIN FETCH f.project p " + diff --git a/backend/src/main/java/com/worlabel/domain/project/controller/ProjectController.java b/backend/src/main/java/com/worlabel/domain/project/controller/ProjectController.java index 1748b3c..a46f702 100644 --- a/backend/src/main/java/com/worlabel/domain/project/controller/ProjectController.java +++ b/backend/src/main/java/com/worlabel/domain/project/controller/ProjectController.java @@ -5,6 +5,7 @@ import com.worlabel.domain.project.dto.AutoModelRequest; import com.worlabel.domain.project.entity.dto.ProjectMemberResponse; import com.worlabel.domain.project.entity.dto.ProjectRequest; import com.worlabel.domain.project.entity.dto.ProjectResponse; +import com.worlabel.domain.project.entity.dto.ProjectWithThumbnailResponse; import com.worlabel.domain.project.service.ProjectService; import com.worlabel.global.annotation.CurrentUser; import com.worlabel.global.config.swagger.SwaggerApiError; @@ -52,7 +53,7 @@ public class ProjectController { @SwaggerApiSuccess(description = "전체 프로젝트를 성공적으로 조회합니다.") @SwaggerApiError({ErrorCode.SERVER_ERROR}) @GetMapping("/workspaces/{workspace_id}/projects") - public List getProjects( + public List getProjects( @PathVariable("workspace_id") Integer workspaceId, @CurrentUser final Integer memberId, @Parameter(name = "마지막 프로젝트 id", description = "마지막 프로젝트 id를 넣으면 그 아래 부터 가져옴, 넣지않으면 가장 최신", example = "1") @RequestParam(required = false) Integer lastProjectId, diff --git a/backend/src/main/java/com/worlabel/domain/project/entity/dto/ProjectWithThumbnailResponse.java b/backend/src/main/java/com/worlabel/domain/project/entity/dto/ProjectWithThumbnailResponse.java new file mode 100644 index 0000000..e96ef8d --- /dev/null +++ b/backend/src/main/java/com/worlabel/domain/project/entity/dto/ProjectWithThumbnailResponse.java @@ -0,0 +1,49 @@ +package com.worlabel.domain.project.entity.dto; + +import com.worlabel.domain.project.entity.Project; +import com.worlabel.domain.project.entity.ProjectType; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.time.LocalDateTime; + +@Schema(name = "프로젝트 디테일 응답 dto", description = "프로젝트 디테일 응답 DTO") +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class ProjectWithThumbnailResponse { + + @Schema(description = "워크스페이스 ID", example = "1") + private Integer id; + + @Schema(description = "제목", example = "project") + private String title; + + @Schema(description = "워크스페이스 id", example = "1") + private Integer workspaceId; + + @Schema(description = "프로젝트 타입", example = "classification") + private ProjectType projectType; + + @Schema(description = "생성일시", example = "2024-07-25 17:51:02") + private LocalDateTime createdAt; + + @Schema(description = "수정일시", example = "2024-07-28 17:51:02") + private LocalDateTime updatedAt; + + @Schema(description = "썸네일", example = "test.jpg") + private String thumbnail; + + public static ProjectWithThumbnailResponse from(final Project project, final String thumbnail) { + return new ProjectWithThumbnailResponse( + project.getId(), + project.getTitle(), + project.getWorkspace().getId(), + project.getProjectType(), + project.getCreatedAt(), + project.getUpdatedAt(), + thumbnail + ); + } +} diff --git a/backend/src/main/java/com/worlabel/domain/project/service/ProjectService.java b/backend/src/main/java/com/worlabel/domain/project/service/ProjectService.java index 14aa519..058b6e3 100644 --- a/backend/src/main/java/com/worlabel/domain/project/service/ProjectService.java +++ b/backend/src/main/java/com/worlabel/domain/project/service/ProjectService.java @@ -25,6 +25,7 @@ import com.worlabel.domain.project.entity.Project; import com.worlabel.domain.project.entity.dto.ProjectMemberResponse; import com.worlabel.domain.project.entity.dto.ProjectRequest; import com.worlabel.domain.project.entity.dto.ProjectResponse; +import com.worlabel.domain.project.entity.dto.ProjectWithThumbnailResponse; import com.worlabel.domain.project.repository.ProjectRepository; import com.worlabel.domain.workspace.entity.Workspace; import com.worlabel.domain.workspace.repository.WorkspaceRepository; @@ -42,6 +43,7 @@ import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; import java.util.Objects; +import java.util.Optional; @Slf4j @Service @@ -81,10 +83,10 @@ public class ProjectService { } @Transactional(readOnly = true) - public List getProjectsByWorkspaceId(final Integer workspaceId, final Integer memberId, final Integer lastProjectId, final Integer pageSize) { + public List getProjectsByWorkspaceId(final Integer workspaceId, final Integer memberId, final Integer lastProjectId, final Integer pageSize) { return projectRepository.findProjectsByWorkspaceIdAndMemberIdWithPagination(workspaceId, memberId, lastProjectId, pageSize).stream() - .map(ProjectResponse::from) - .toList(); + .map(project -> ProjectWithThumbnailResponse.from(project, getFirstImageWithProject(project))) + .toList(); } @CheckPrivilege(PrivilegeType.ADMIN) @@ -249,4 +251,13 @@ public class ProjectService { throw new CustomException(ErrorCode.PARTICIPANT_BAD_REQUEST); } } + + private String getFirstImageWithProject(final Project project) { + Optional image = imageRepository.findFirstImageByProjectId(project.getId()); + if (image.isPresent()) { + return image.get().getImagePath(); + } + + return "https://www.shoshinsha-design.com/wp-content/uploads/2020/05/noimage-760x460.png"; + } }