From 534a628acdeebb46ca681d8741c48299e0c5327c Mon Sep 17 00:00:00 2001 From: eojin0814 Date: Thu, 23 Nov 2023 17:29:50 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/res/ContentsLikeCount.java | 54 ++++++++++++++++ .../application/dto/res/ContentsRes.java | 2 + .../dto/res/ContentsWithLikeCount.java | 24 ++++++++ .../repository/ContentsCustomRepository.java | 4 +- .../ContentsCustomRepositoryImpl.java | 61 ++++++++++++++++--- .../domain/service/ContentsService.java | 3 +- .../domain/service/ContentsServiceImpl.java | 9 ++- .../presentation/ContentsController.java | 7 +-- 8 files changed, 144 insertions(+), 20 deletions(-) create mode 100644 src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsLikeCount.java create mode 100644 src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsWithLikeCount.java diff --git a/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsLikeCount.java b/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsLikeCount.java new file mode 100644 index 0000000..405fcb0 --- /dev/null +++ b/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsLikeCount.java @@ -0,0 +1,54 @@ +package gwangjang.server.domain.contents.application.dto.res; + +import gwangjang.server.domain.contents.domain.entity.constant.ApiType; +import io.swagger.models.auth.In; +import lombok.*; + +@Getter +@Builder +@Setter +@NoArgsConstructor +public class ContentsLikeCount { + private Integer contentsId; + private String url; + private String title; + private String description; + private ApiType type; + private String issueTitle; + private String keyword; + private String pubDate; + private String topic; + private String imgUrl; + private long likeCount; // Change from int to long + private boolean userLiked; + + public ContentsLikeCount( + Integer contentsId, + String url, + String title, + String description, + ApiType type, + String issueTitle, + String keyword, + String pubDate, + String topic, + String imgUrl, + long likeCount, // Change from int to long + boolean userLiked + ) { + this.contentsId = contentsId; + this.url = url; + this.title = title; + this.description = description; + this.type = type; + this.issueTitle = issueTitle; + this.keyword = keyword; + this.pubDate = pubDate; + this.topic = topic; + this.imgUrl = imgUrl; + this.likeCount = likeCount; + this.userLiked = userLiked; + } + + // Add getters for all fields +} diff --git a/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsRes.java b/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsRes.java index 740acca..2c3b435 100644 --- a/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsRes.java +++ b/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsRes.java @@ -20,4 +20,6 @@ public class ContentsRes { private String topic; private String imgUrl; + private Long likeCount; + } diff --git a/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsWithLikeCount.java b/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsWithLikeCount.java new file mode 100644 index 0000000..ece5eca --- /dev/null +++ b/src/main/java/gwangjang/server/domain/contents/application/dto/res/ContentsWithLikeCount.java @@ -0,0 +1,24 @@ +package gwangjang.server.domain.contents.application.dto.res; + +import com.fasterxml.jackson.annotation.JsonProperty; +import gwangjang.server.domain.contents.domain.entity.Contents; +import gwangjang.server.domain.contents.domain.entity.constant.ApiType; + +public class ContentsWithLikeCount { + private Integer contents_id; + private String url; + private String title; + private String description; + ApiType type; + private String issueTitle; + private String keyword; + private String pubDate; + private String topic; + private String imgUrl; + + private Long likeCount; + + private long likeStatus; + + +} diff --git a/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepository.java b/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepository.java index 2b76164..a08e670 100644 --- a/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepository.java +++ b/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepository.java @@ -2,6 +2,8 @@ import gwangjang.server.domain.contents.application.dto.res.BubbleChartRes; import gwangjang.server.domain.contents.application.dto.res.ContentsDataRes; +import gwangjang.server.domain.contents.application.dto.res.ContentsRes; +import gwangjang.server.domain.contents.application.dto.res.ContentsWithLikeCount; import gwangjang.server.domain.contents.domain.entity.Contents; import org.springframework.data.jpa.repository.Query; @@ -9,7 +11,7 @@ public interface ContentsCustomRepository { List getContentsByIssueId(String issue); - List findAllOrderByLikeCountDesc(); + List findAllOrderByLikeCountDesc(); List findContentsByLoginId(String loginId); void updateContentsImageUrl(Integer contentsId, String newImageUrl); diff --git a/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepositoryImpl.java b/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepositoryImpl.java index a4f2919..9f26cb4 100644 --- a/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepositoryImpl.java +++ b/src/main/java/gwangjang/server/domain/contents/domain/repository/ContentsCustomRepositoryImpl.java @@ -6,10 +6,9 @@ import com.querydsl.core.types.dsl.Expressions; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.impl.JPAQueryFactory; -import gwangjang.server.domain.contents.application.dto.res.BubbleChartRes; +import gwangjang.server.domain.contents.application.dto.res.*; import gwangjang.server.domain.contents.domain.entity.constant.ApiType; import gwangjang.server.domain.like.domain.entity.QContentLike; -import gwangjang.server.domain.contents.application.dto.res.ContentsDataRes; import gwangjang.server.domain.contents.domain.entity.Contents; import gwangjang.server.domain.contents.domain.entity.QContents; import jakarta.persistence.EntityManager; @@ -47,12 +46,16 @@ public List getContentsByIssueId(String issue) { .limit(10).fetch(); } - public List findAllOrderByLikeCountDesc() { + public List findAllOrderByLikeCountDesc() { QContents contents = QContents.contents; QContentLike contentLike = QContentLike.contentLike; - List result = queryFactory - .select(contents) + List result = queryFactory + .select(Projections.constructor( + ContentsWithLikeCount.class, + contents, + contentLike.likeId.count().as("likeCount") + )) .from(contents) .leftJoin(contentLike).on(contents.contents_id.eq(contentLike.contents.contents_id)) .groupBy(contents.contents_id) @@ -93,9 +96,51 @@ public void updateContentsImageUrl(Integer contentsId, String newImageUrl) { } - - - +// public List searchContentsWithLikeCount(String keyword, ApiType type, String loginId) { +// QContents contents = QContents.contents; +// QContentLike contentLike = QContentLike.contentLike; +// +// List result = queryFactory +// .select( +// contents.contents_id, +// contents.url, +// contents.title, +// contents.description, +// contents.type, +// contents.issueTitle, +// contents.keyword, +// contents.pubDate, +// contents.topic, +// contents.imgUrl, +// contentLike.likeId.count().coalesce(0L).as("likeCount"), +// contentLike.likeId.count().coalesce(0L) +// .as("userLiked") +// ) +// .from(contents) +// .leftJoin(contentLike).on(contents.contents_id.eq(contentLike.contents.contents_id).and(contentLike.loginId.eq(loginId))) +// .where(contents.keyword.containsIgnoreCase(keyword).and(contents.type.eq(type))) +// .groupBy(contents.contents_id, contents.url, contents.title, contents.description, contents.type, contents.issueTitle, +// contents.keyword, contents.pubDate, contents.topic, contents.imgUrl) +// .fetch(); +// +// return result.stream() +// .map(tuple -> new ContentsLikeCount( +// tuple.get(contents.contents_id), +// tuple.get(contents.url), +// tuple.get(contents.title), +// tuple.get(contents.description), +// tuple.get(contents.type), +// tuple.get(contents.issueTitle), +// tuple.get(contents.keyword), +// tuple.get(contents.pubDate), +// tuple.get(contents.topic), +// tuple.get(contents.imgUrl), +// tuple.get("likeCount", Integer.class), // Change to Integer.class +// tuple.get("userLiked", Boolean.class) +// )) +// .collect(Collectors.toList()); +// +// } } diff --git a/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsService.java b/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsService.java index 7b4ef41..abccfad 100644 --- a/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsService.java +++ b/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsService.java @@ -1,5 +1,6 @@ package gwangjang.server.domain.contents.domain.service; import gwangjang.server.domain.contents.application.dto.res.ContentsRes; +import gwangjang.server.domain.contents.application.dto.res.ContentsWithLikeCount; import gwangjang.server.domain.contents.domain.entity.constant.ApiType; import gwangjang.server.global.annotation.DomainService; import reactor.core.publisher.Mono; @@ -13,7 +14,7 @@ public interface ContentsService { List getContentsTitle(String issue, ApiType type); List getKeywordAndType(String Keyword, ApiType apiType); ContentsRes getContentsById(Integer contentsId); - List getContentLikeCount(); + List getContentLikeCount(); List findContentsByLoginId(String loginId); } diff --git a/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java b/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java index 584c820..6de8d7f 100644 --- a/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java +++ b/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java @@ -1,5 +1,6 @@ package gwangjang.server.domain.contents.domain.service; +import gwangjang.server.domain.contents.application.dto.res.ContentsWithLikeCount; import gwangjang.server.domain.like.domain.repository.LikeRepository; import gwangjang.server.domain.contents.application.dto.res.ContentsRes; import gwangjang.server.domain.contents.application.mapper.ContentsMapper; @@ -181,12 +182,10 @@ public ContentsRes getContentsById(Integer contentsId) { return contentsMapper.toDto(contents); } - public List getContentLikeCount(){ - List contents = contentsRepository.findAllOrderByLikeCountDesc(); + public List getContentLikeCount(){ + List contents = contentsRepository.findAllOrderByLikeCountDesc(); - return contents.stream() - .map(contentsMapper::toDto) - .collect(Collectors.toList()); + return contents; } public List findContentsByLoginId(String loginId){ List contents = contentsRepository.findContentsByLoginId(loginId); diff --git a/src/main/java/gwangjang/server/domain/contents/presentation/ContentsController.java b/src/main/java/gwangjang/server/domain/contents/presentation/ContentsController.java index 25ffedc..dbbdd7e 100644 --- a/src/main/java/gwangjang/server/domain/contents/presentation/ContentsController.java +++ b/src/main/java/gwangjang/server/domain/contents/presentation/ContentsController.java @@ -1,9 +1,6 @@ package gwangjang.server.domain.contents.presentation; -import gwangjang.server.domain.contents.application.dto.res.BubbleChartRes; -import gwangjang.server.domain.contents.application.dto.res.BubbleFrontRes; -import gwangjang.server.domain.contents.application.dto.res.ContentsDataRes; -import gwangjang.server.domain.contents.application.dto.res.ContentsRes; +import gwangjang.server.domain.contents.application.dto.res.*; import gwangjang.server.domain.contents.application.service.ContentsSubscribeUseCase; import gwangjang.server.domain.contents.domain.entity.constant.ApiType; @@ -64,7 +61,7 @@ public ResponseEntity> getNaverContents() { return ResponseEntity.ok(SuccessResponse.create(GET_CONTENTS_SUCCESS.getMessage(),this.newsAPIService.naverAPI("test"))); } @GetMapping("/contents/like") - public ResponseEntity>> getContentLikeCount() { + public ResponseEntity>> getContentLikeCount() { return ResponseEntity.ok(SuccessResponse.create(GET_CONTENTS_SUCCESS.getMessage(),this.contentsService.getContentLikeCount())); } @PostMapping("/my-page/like") From 2a1f497516831323b2caedaa8c26bcdf1bb72d75 Mon Sep 17 00:00:00 2001 From: seungyeonnnnnni Date: Mon, 27 Nov 2023 03:45:05 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix=20:=20=EC=BB=A8=ED=85=90=EC=B8=A0=20?= =?UTF-8?q?=EB=A6=AC=ED=84=B4=EA=B0=92=20=EC=A0=9C=ED=95=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/contents/domain/service/ContentsServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java b/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java index 6de8d7f..b1acd6b 100644 --- a/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java +++ b/src/main/java/gwangjang/server/domain/contents/domain/service/ContentsServiceImpl.java @@ -163,7 +163,7 @@ public List getContents(ApiType type) { } public List getContentsTitle(String issue, ApiType type) { - List contents = contentsRepository.findByIssueTitleLikeAndType("%" + issue + "%", type); + List contents = contentsRepository.findByIssueTitleLikeAndType("%" + issue + "%", type).subList(0,20); return contents.stream() .map(contentsMapper::toDto) .collect(Collectors.toList());