Skip to content
Draft
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 @@ -10,7 +10,7 @@
import org.example.plzdrawing.common.page.PageResponse;
import org.example.plzdrawing.domain.content.dto.request.UpdateContentRequest;
import org.example.plzdrawing.domain.content.dto.request.UploadContentRequest;
import org.example.plzdrawing.domain.content.dto.response.LatestContentsResponse;
import org.example.plzdrawing.domain.content.dto.response.GetContentsResponse;
import org.example.plzdrawing.domain.content.dto.response.UploadContentResponse;
import org.example.plzdrawing.domain.content.facade.ContentFacade;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -66,12 +66,14 @@ public ResponseEntity<PageResponse<ContentsDto>> getContentsThumbnail(
}

@GetMapping
@Operation(summary = "최신순 콘텐츠 조회", description = "최신순으로 콘텐츠를 조회한다.")
public ResponseEntity<PageResponse<LatestContentsResponse>> getLatestContents(
@Operation(summary = "옵션별 콘텐츠 조회", description = "옵션별로 콘텐츠를 조회한다. (0이면 최신순 1이면 좋아요 누른 게시글)")
public ResponseEntity<PageResponse<GetContentsResponse>> getLatestContents(
@AuthenticationPrincipal CustomUser customUser,
@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "size", defaultValue = "10") int size
@RequestParam(name = "size", defaultValue = "10") int size,
@RequestParam(name = "option", defaultValue = "0") int option
) {
int safePage = (page < 1) ? 0 : page - 1;
return ResponseEntity.ok(contentFacade.getLatestContents(safePage, size));
return ResponseEntity.ok(contentFacade.getLatestContents(safePage, size, customUser, option));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.example.plzdrawing.domain.content.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import org.example.plzdrawing.domain.content.enums.TimeTaken;

public record ContentsDto(
@Schema(description = "콘텐츠 id", example = "1")
Long contentId,
@Schema(description = "콘텐츠 생성일자", example = "2025-12-22")
LocalDate createAt,
LocalDateTime createAt,
@Schema(description = "콘텐츠 url", example = "[https://s3~, https://s3~]")
List<String> contentUrl,
@Schema(description = "콘텐츠 해시태그", example = "[사과, 바나나]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.plzdrawing.api.member.dto.response.UploaderDto;

public record LatestContentsResponse(
public record GetContentsResponse(
@Schema(description = "업로더 정보")
UploaderDto uploaderDto,
@Schema(description = "콘텐츠 정보")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.plzdrawing.domain.content.facade;

import static org.example.plzdrawing.common.error.CommonErrorCode.CONTENT_NOT_FOUND;
import static org.example.plzdrawing.common.error.CommonErrorCode.INVALID_FIELD;

import java.util.List;
import java.util.Map;
Expand All @@ -14,7 +15,7 @@
import org.example.plzdrawing.domain.content.Content;
import org.example.plzdrawing.domain.content.dto.request.UpdateContentRequest;
import org.example.plzdrawing.domain.content.dto.request.UploadContentRequest;
import org.example.plzdrawing.domain.content.dto.response.LatestContentsResponse;
import org.example.plzdrawing.domain.content.dto.response.GetContentsResponse;
import org.example.plzdrawing.domain.content.dto.response.UploadContentResponse;
import org.example.plzdrawing.domain.content.service.ContentImageService;
import org.example.plzdrawing.domain.content.service.ContentService;
Expand Down Expand Up @@ -86,7 +87,7 @@ public PageResponse<ContentsDto> getMemberContents(Long memberId, int page, int
Long contentId = content.getId();
return new ContentsDto(
contentId,
content.getCreatedAt().toLocalDate(),
content.getCreatedAt(),
imageMap.getOrDefault(contentId, List.of()),
tagMap.getOrDefault(contentId, List.of()),
content.getExplanation(),
Expand All @@ -103,10 +104,18 @@ public PageResponse<ContentsDto> getMemberContents(Long memberId, int page, int
return PageResponse.from(dtoPage);
}

public PageResponse<LatestContentsResponse> getLatestContents(int page, int size) {
public PageResponse<GetContentsResponse> getLatestContents(int page, int size, CustomUser customUser, int option) {
PageRequest pageRequest = PageRequest.of(page, size);

Page<Content> contentPage = contentService.findLatest(pageRequest);
Page<Content> contentPage;
if(option == 0) {
contentPage = contentService.findLatest(pageRequest);
}
else if(option == 1) {
contentPage = contentService.findByMyLikeLatest(customUser.getMember(), pageRequest);
}
else {
throw new RestApiException(INVALID_FIELD.getErrorCode());
}
if (contentPage.isEmpty()) {
return PageResponse.from(Page.empty(pageRequest));
}
Expand All @@ -123,7 +132,7 @@ public PageResponse<LatestContentsResponse> getLatestContents(int page, int size
Map<Long, Long> drawingCountMap = contentService.countDrawingsByMemberIds(memberIds);
Map<Long, ReviewService.ReviewStats> reviewStatsMap = reviewService.findSellingMemberStats(memberIds);

List<LatestContentsResponse> dtoList = contents.stream()
List<GetContentsResponse> dtoList = contents.stream()
.map(content -> {
Long contentId = content.getId();
Long uploaderId = content.getMember().getId();
Expand All @@ -140,7 +149,7 @@ public PageResponse<LatestContentsResponse> getLatestContents(int page, int size

ContentsDto contentsDto = new ContentsDto(
contentId,
content.getCreatedAt().toLocalDate(),
content.getCreatedAt(),
imageMap.getOrDefault(contentId, List.of()),
tagMap.getOrDefault(contentId, List.of()),
content.getExplanation(),
Expand All @@ -149,7 +158,7 @@ public PageResponse<LatestContentsResponse> getLatestContents(int page, int size
likeMap.getOrDefault(contentId, 0L)
);

return new LatestContentsResponse(uploaderDto, contentsDto);
return new GetContentsResponse(uploaderDto, contentsDto);
})
.toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ select c.member.id as memberId, count(c) as cnt
group by c.member.id
""")
List<DrawingCountProjection> countByMemberIds(@Param("memberIds") List<Long> memberIds);

@Query("""
select distinct c
from LikeEntity l join l.content c
where l.member = :member
order by c.createdAt desc
""")
Page<Content> findByMemberLikeAndCreatedAtDesc(@Param("member")Member member, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.example.plzdrawing.domain.content.repository.ContentRepository;
import org.example.plzdrawing.domain.member.Member;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -65,4 +66,8 @@ public Map<Long, Long> countDrawingsByMemberIds(List<Long> memberIds) {
ContentRepository.DrawingCountProjection::getCnt
));
}

public Page<Content> findByMyLikeLatest(Member member, PageRequest pageRequest) {
return contentRepository.findByMemberLikeAndCreatedAtDesc(member, pageRequest);
}
}
Loading