From 6a95d4dd1bff9e43df0d4bd2351b2ec91981bd2a Mon Sep 17 00:00:00 2001 From: kwondongwook Date: Wed, 13 Dec 2023 21:18:55 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94,=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=B7=A8=EC=86=8C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 좋아요와 좋아요 취소는 엔드포인트를 분리하여 설계 - member_id, article_id 쌍에 대한 Unique 제약조건 설정 --- .../controller/InterestArticleController.java | 29 +++++++++++ .../dto/CreateInterestArticleRequest.java | 19 ++++++++ .../model/InterestArticle.java | 33 +++++++++++++ .../repository/InterestArticleRepository.java | 11 +++++ .../service/InterestArticleService.java | 48 +++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java create mode 100644 src/main/java/com/api/trip/domain/interestarticle/controller/dto/CreateInterestArticleRequest.java create mode 100644 src/main/java/com/api/trip/domain/interestarticle/model/InterestArticle.java create mode 100644 src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java create mode 100644 src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java diff --git a/src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java b/src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java new file mode 100644 index 0000000..a46bbb9 --- /dev/null +++ b/src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java @@ -0,0 +1,29 @@ +package com.api.trip.domain.interestarticle.controller; + +import com.api.trip.domain.interestarticle.controller.dto.CreateInterestArticleRequest; +import com.api.trip.domain.interestarticle.service.InterestArticleService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/interest-articles") +@RequiredArgsConstructor +public class InterestArticleController { + + private final InterestArticleService interestArticleService; + + @PostMapping + public ResponseEntity createInterestArticle(@RequestBody CreateInterestArticleRequest request) { + String email = SecurityContextHolder.getContext().getAuthentication().getName(); + return ResponseEntity.ok(interestArticleService.createInterestArticle(request, email)); + } + + @DeleteMapping("/{interestArticleId}") + public ResponseEntity deleteInterestArticle(@PathVariable Long interestArticleId) { + String email = SecurityContextHolder.getContext().getAuthentication().getName(); + interestArticleService.deleteInterestArticle(interestArticleId, email); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/com/api/trip/domain/interestarticle/controller/dto/CreateInterestArticleRequest.java b/src/main/java/com/api/trip/domain/interestarticle/controller/dto/CreateInterestArticleRequest.java new file mode 100644 index 0000000..ed21e3d --- /dev/null +++ b/src/main/java/com/api/trip/domain/interestarticle/controller/dto/CreateInterestArticleRequest.java @@ -0,0 +1,19 @@ +package com.api.trip.domain.interestarticle.controller.dto; + +import com.api.trip.domain.article.model.Article; +import com.api.trip.domain.interestarticle.model.InterestArticle; +import com.api.trip.domain.member.model.Member; +import lombok.Getter; + +@Getter +public class CreateInterestArticleRequest { + + private Long articleId; + + public InterestArticle toEntity(Member member, Article article) { + return InterestArticle.builder() + .member(member) + .article(article) + .build(); + } +} diff --git a/src/main/java/com/api/trip/domain/interestarticle/model/InterestArticle.java b/src/main/java/com/api/trip/domain/interestarticle/model/InterestArticle.java new file mode 100644 index 0000000..5cd6066 --- /dev/null +++ b/src/main/java/com/api/trip/domain/interestarticle/model/InterestArticle.java @@ -0,0 +1,33 @@ +package com.api.trip.domain.interestarticle.model; + +import com.api.trip.common.auditing.entity.BaseTimeEntity; +import com.api.trip.domain.article.model.Article; +import com.api.trip.domain.member.model.Member; +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"member_id", "article_id"})}) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +public class InterestArticle extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + private Member member; + + @ManyToOne(fetch = FetchType.LAZY) + private Article article; + + @Builder + private InterestArticle(Member member, Article article) { + this.member = member; + this.article = article; + } +} diff --git a/src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java b/src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java new file mode 100644 index 0000000..04bb2ed --- /dev/null +++ b/src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java @@ -0,0 +1,11 @@ +package com.api.trip.domain.interestarticle.repository; + +import com.api.trip.domain.article.model.Article; +import com.api.trip.domain.interestarticle.model.InterestArticle; +import com.api.trip.domain.member.model.Member; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface InterestArticleRepository extends JpaRepository { + + InterestArticle findByMemberAndArticle(Member member, Article article); +} diff --git a/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java b/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java new file mode 100644 index 0000000..31aa33f --- /dev/null +++ b/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java @@ -0,0 +1,48 @@ +package com.api.trip.domain.interestarticle.service; + +import com.api.trip.domain.article.model.Article; +import com.api.trip.domain.article.repository.ArticleRepository; +import com.api.trip.domain.interestarticle.controller.dto.CreateInterestArticleRequest; +import com.api.trip.domain.interestarticle.model.InterestArticle; +import com.api.trip.domain.interestarticle.repository.InterestArticleRepository; +import com.api.trip.domain.member.model.Member; +import com.api.trip.domain.member.repository.MemberRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +@RequiredArgsConstructor +public class InterestArticleService { + + private final InterestArticleRepository interestArticleRepository; + private final ArticleRepository articleRepository; + private final MemberRepository memberRepository; + + public Long createInterestArticle(CreateInterestArticleRequest request, String email) { + Member member = memberRepository.findByEmail(email).orElseThrow(); + + Article article = articleRepository.findById(request.getArticleId()).orElseThrow(); + + InterestArticle interestArticle = interestArticleRepository.findByMemberAndArticle(member, article); + if (interestArticle != null) { + throw new RuntimeException("잘못된 요청입니다."); + } + + interestArticle = request.toEntity(member, article); + + return interestArticleRepository.save(interestArticle).getId(); + } + + public void deleteInterestArticle(Long interestArticleId, String email) { + Member member = memberRepository.findByEmail(email).orElseThrow(); + + InterestArticle interestArticle = interestArticleRepository.findById(interestArticleId).orElseThrow(); + if (interestArticle.getMember() != member) { + throw new RuntimeException("삭제 권한이 없습니다."); + } + + interestArticleRepository.delete(interestArticle); + } +} From 1dae46dd1eaf69a926a96926fdd8e40714be5cc4 Mon Sep 17 00:00:00 2001 From: kwondongwook Date: Wed, 13 Dec 2023 21:37:40 +0900 Subject: [PATCH 2/6] =?UTF-8?q?Feat:=20Article=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=EC=97=90=20likeCount(=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=88=98)=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 좋아요 또는 좋아요 취소 시 Article.likeCount를 update --- .../com/api/trip/domain/article/model/Article.java | 5 ++++- .../domain/article/repository/ArticleRepository.java | 11 +++++++++++ .../service/InterestArticleService.java | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/api/trip/domain/article/model/Article.java b/src/main/java/com/api/trip/domain/article/model/Article.java index d34e154..c81f067 100644 --- a/src/main/java/com/api/trip/domain/article/model/Article.java +++ b/src/main/java/com/api/trip/domain/article/model/Article.java @@ -28,12 +28,15 @@ public class Article extends BaseTimeEntity { private long viewCount; + private long likeCount; + @Builder - private Article(Member writer, String title, String content, long viewCount) { + private Article(Member writer, String title, String content, long viewCount, long likeCount) { this.writer = writer; this.title = title; this.content = content; this.viewCount = viewCount; + this.likeCount = likeCount; } public void modify(String title, String content) { diff --git a/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java b/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java index 68c2f4d..fb3a3a0 100644 --- a/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java +++ b/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java @@ -3,10 +3,21 @@ import com.api.trip.domain.article.model.Article; import com.api.trip.domain.member.model.Member; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; public interface ArticleRepository extends JpaRepository, ArticleRepositoryCustom { List
findAllByWriterOrderByIdDesc(Member writer); + + @Modifying + @Query("UPDATE Article a SET a.likeCount = a.likeCount + 1 WHERE a = :article") + void increaseLikeCount(@Param("article") Article article); + + @Modifying + @Query("UPDATE Article a SET a.likeCount = a.likeCount - 1 WHERE a = :article") + void decreaseLikeCount(@Param("article") Article article); } diff --git a/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java b/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java index 31aa33f..93265c1 100644 --- a/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java +++ b/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java @@ -32,6 +32,8 @@ public Long createInterestArticle(CreateInterestArticleRequest request, String e interestArticle = request.toEntity(member, article); + articleRepository.increaseLikeCount(interestArticle.getArticle()); + return interestArticleRepository.save(interestArticle).getId(); } @@ -43,6 +45,8 @@ public void deleteInterestArticle(Long interestArticleId, String email) { throw new RuntimeException("삭제 권한이 없습니다."); } + articleRepository.decreaseLikeCount(interestArticle.getArticle()); + interestArticleRepository.delete(interestArticle); } } From 2e86a15fbf7b191ed722eb35749bd9e141eb8b5b Mon Sep 17 00:00:00 2001 From: kwondongwook Date: Wed, 13 Dec 2023 21:43:14 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20API?= =?UTF-8?q?=20=EC=9D=91=EB=8B=B5=20=EA=B0=92=EC=97=90=20=EC=A2=8B=EC=95=84?= =?UTF-8?q?=EC=9A=94=20=EC=88=98=20=EB=98=90=EB=8A=94=20=EC=A2=8B=EC=95=84?= =?UTF-8?q?=EC=9A=94=20=EC=97=AC=EB=B6=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../article/controller/ArticleController.java | 3 ++- .../controller/dto/GetArticlesResponse.java | 2 ++ .../controller/dto/ReadArticleResponse.java | 5 ++++- .../domain/article/service/ArticleService.java | 14 ++++++++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/api/trip/domain/article/controller/ArticleController.java b/src/main/java/com/api/trip/domain/article/controller/ArticleController.java index b18b4c9..0bcb45d 100644 --- a/src/main/java/com/api/trip/domain/article/controller/ArticleController.java +++ b/src/main/java/com/api/trip/domain/article/controller/ArticleController.java @@ -38,7 +38,8 @@ public ResponseEntity deleteArticle(@PathVariable Long articleId) { @GetMapping("/{articleId}") public ResponseEntity readArticle(@PathVariable Long articleId) { - return ResponseEntity.ok(articleService.readArticle(articleId)); + String email = SecurityContextHolder.getContext().getAuthentication().getName(); + return ResponseEntity.ok(articleService.readArticle(articleId, email)); } @GetMapping diff --git a/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java b/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java index f1262af..c8f9f73 100644 --- a/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java +++ b/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java @@ -46,6 +46,7 @@ private static class ArticleDto { private String writerNickname; private String writerRole; private LocalDateTime createdAt; + private long likeCount; private static ArticleDto of(Article article) { Member writer = article.getWriter(); @@ -56,6 +57,7 @@ private static ArticleDto of(Article article) { .writerNickname(writer.getNickname()) .writerRole(writer.getRole().name()) .createdAt(article.getCreatedAt()) + .likeCount(article.getLikeCount()) .build(); } } diff --git a/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java b/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java index 343183a..f3ae41f 100644 --- a/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java +++ b/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java @@ -1,6 +1,7 @@ package com.api.trip.domain.article.controller.dto; import com.api.trip.domain.article.model.Article; +import com.api.trip.domain.interestarticle.model.InterestArticle; import com.api.trip.domain.member.model.Member; import lombok.Builder; import lombok.Getter; @@ -19,8 +20,9 @@ public class ReadArticleResponse { private String content; private long viewCount; private LocalDateTime createdAt; + private Long interestArticleId; - public static ReadArticleResponse of(Article article) { + public static ReadArticleResponse of(Article article, InterestArticle interestArticle) { Member writer = article.getWriter(); return builder() .articleId(article.getId()) @@ -31,6 +33,7 @@ public static ReadArticleResponse of(Article article) { .content(article.getContent()) .viewCount(article.getViewCount()) .createdAt(article.getCreatedAt()) + .interestArticleId(interestArticle != null ? interestArticle.getId() : null) .build(); } } diff --git a/src/main/java/com/api/trip/domain/article/service/ArticleService.java b/src/main/java/com/api/trip/domain/article/service/ArticleService.java index cbd32e7..b910c66 100644 --- a/src/main/java/com/api/trip/domain/article/service/ArticleService.java +++ b/src/main/java/com/api/trip/domain/article/service/ArticleService.java @@ -3,6 +3,8 @@ import com.api.trip.domain.article.controller.dto.*; import com.api.trip.domain.article.model.Article; import com.api.trip.domain.article.repository.ArticleRepository; +import com.api.trip.domain.interestarticle.model.InterestArticle; +import com.api.trip.domain.interestarticle.repository.InterestArticleRepository; import com.api.trip.domain.member.model.Member; import com.api.trip.domain.member.repository.MemberRepository; import lombok.RequiredArgsConstructor; @@ -12,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Objects; @Service @Transactional @@ -20,6 +23,7 @@ public class ArticleService { private final ArticleRepository articleRepository; private final MemberRepository memberRepository; + private final InterestArticleRepository interestArticleRepository; public Long createArticle(CreateArticleRequest request, String email) { Member member = memberRepository.findByEmail(email).orElseThrow(); @@ -51,12 +55,18 @@ public void deleteArticle(Long articleId, String email) { articleRepository.delete(article); } - public ReadArticleResponse readArticle(Long articleId) { + public ReadArticleResponse readArticle(Long articleId, String email) { Article article = articleRepository.findArticle(articleId).orElseThrow(); article.increaseViewCount(); - return ReadArticleResponse.of(article); + InterestArticle interestArticle = null; + if (!Objects.equals(email, "anonymousUser")) { + Member member = memberRepository.findByEmail(email).orElseThrow(); + interestArticle = interestArticleRepository.findByMemberAndArticle(member, article); + } + + return ReadArticleResponse.of(article, interestArticle); } @Transactional(readOnly = true) From 776a568a3cb9861fdc6d0e2fae9f5008d678419f Mon Sep 17 00:00:00 2001 From: kwondongwook Date: Wed, 13 Dec 2023 21:48:21 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Fix:=20=EB=8F=99=EC=8B=9C=EC=84=B1=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=EB=A5=BC=20=EA=B3=A0=EB=A0=A4=ED=95=B4=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=A1=B0=ED=9A=8C=EC=88=98=20?= =?UTF-8?q?=EC=A6=9D=EA=B0=80=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/api/trip/domain/article/model/Article.java | 4 ---- .../api/trip/domain/article/repository/ArticleRepository.java | 4 ++++ .../com/api/trip/domain/article/service/ArticleService.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/api/trip/domain/article/model/Article.java b/src/main/java/com/api/trip/domain/article/model/Article.java index c81f067..c017dd9 100644 --- a/src/main/java/com/api/trip/domain/article/model/Article.java +++ b/src/main/java/com/api/trip/domain/article/model/Article.java @@ -43,8 +43,4 @@ public void modify(String title, String content) { this.title = title; this.content = content; } - - public void increaseViewCount() { - this.viewCount++; - } } diff --git a/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java b/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java index fb3a3a0..5dbb977 100644 --- a/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java +++ b/src/main/java/com/api/trip/domain/article/repository/ArticleRepository.java @@ -13,6 +13,10 @@ public interface ArticleRepository extends JpaRepository, Article List
findAllByWriterOrderByIdDesc(Member writer); + @Modifying + @Query("UPDATE Article a SET a.viewCount = a.viewCount + 1 WHERE a = :article") + void increaseViewCount(@Param("article") Article article); + @Modifying @Query("UPDATE Article a SET a.likeCount = a.likeCount + 1 WHERE a = :article") void increaseLikeCount(@Param("article") Article article); diff --git a/src/main/java/com/api/trip/domain/article/service/ArticleService.java b/src/main/java/com/api/trip/domain/article/service/ArticleService.java index b910c66..24509d8 100644 --- a/src/main/java/com/api/trip/domain/article/service/ArticleService.java +++ b/src/main/java/com/api/trip/domain/article/service/ArticleService.java @@ -58,7 +58,7 @@ public void deleteArticle(Long articleId, String email) { public ReadArticleResponse readArticle(Long articleId, String email) { Article article = articleRepository.findArticle(articleId).orElseThrow(); - article.increaseViewCount(); + articleRepository.increaseViewCount(article); InterestArticle interestArticle = null; if (!Objects.equals(email, "anonymousUser")) { From ae55c333678da8f524d0d3dd5d5880be096df0b3 Mon Sep 17 00:00:00 2001 From: kwondongwook Date: Wed, 13 Dec 2023 21:52:35 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=EC=88=9C=20=EC=A0=95=EB=A0=AC=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 동적 정렬 로직 (ArticleRepositoryCustomImpl.getOrderSpecifiers) 수정 --- .../repository/ArticleRepositoryCustomImpl.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/api/trip/domain/article/repository/ArticleRepositoryCustomImpl.java b/src/main/java/com/api/trip/domain/article/repository/ArticleRepositoryCustomImpl.java index c6b8fc5..aff3711 100644 --- a/src/main/java/com/api/trip/domain/article/repository/ArticleRepositoryCustomImpl.java +++ b/src/main/java/com/api/trip/domain/article/repository/ArticleRepositoryCustomImpl.java @@ -12,6 +12,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -27,12 +28,12 @@ public ArticleRepositoryCustomImpl(EntityManager em) { } @Override - public Optional
findArticle(Long id) { + public Optional
findArticle(Long articleId) { return Optional.ofNullable( jpaQueryFactory .selectFrom(article) .innerJoin(article.writer, member).fetchJoin() - .where(article.id.eq(id)) + .where(article.id.eq(articleId)) .fetchOne() ); } @@ -43,7 +44,7 @@ public Page
findArticles(Pageable pageable, String filter) { .selectFrom(article) .innerJoin(article.writer, member).fetchJoin() .where(eqFilter(filter)) - .orderBy(getOrderSpecifier(pageable)) + .orderBy(getOrderSpecifiers(pageable)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) .fetch(); @@ -67,12 +68,14 @@ private BooleanExpression eqFilter(String filter) { return null; } - private OrderSpecifier getOrderSpecifier(Pageable pageable) { + private OrderSpecifier[] getOrderSpecifiers(Pageable pageable) { + List> orderSpecifierList = new ArrayList<>(); for (Sort.Order order : pageable.getSort()) { - if ("POPULAR".equals(order.getProperty())) { - return new OrderSpecifier<>(Order.DESC, article.viewCount); + if ("popular".equals(order.getProperty())) { + orderSpecifierList.add(new OrderSpecifier<>(Order.DESC, article.likeCount)); } } - return new OrderSpecifier<>(Order.DESC, article.id); + orderSpecifierList.add(new OrderSpecifier<>(Order.DESC, article.id)); + return orderSpecifierList.toArray(OrderSpecifier[]::new); } } From 3ee93f6427e61357bf7862077657908b9cd1fe83 Mon Sep 17 00:00:00 2001 From: kwondongwook Date: Thu, 14 Dec 2023 00:21:22 +0900 Subject: [PATCH 6/6] =?UTF-8?q?Feat:=20=EB=88=84=EB=9D=BD=EB=90=9C=20API?= =?UTF-8?q?=20=EC=9D=91=EB=8B=B5=20=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/article/controller/dto/GetArticlesResponse.java | 6 ++++-- .../domain/article/controller/dto/ReadArticleResponse.java | 6 +++++- .../domain/comment/controller/dto/GetCommentsResponse.java | 2 ++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java b/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java index c8f9f73..d4c86f6 100644 --- a/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java +++ b/src/main/java/com/api/trip/domain/article/controller/dto/GetArticlesResponse.java @@ -45,8 +45,9 @@ private static class ArticleDto { private Long writerId; private String writerNickname; private String writerRole; - private LocalDateTime createdAt; + private long viewCount; private long likeCount; + private LocalDateTime createdAt; private static ArticleDto of(Article article) { Member writer = article.getWriter(); @@ -56,8 +57,9 @@ private static ArticleDto of(Article article) { .writerId(writer.getId()) .writerNickname(writer.getNickname()) .writerRole(writer.getRole().name()) - .createdAt(article.getCreatedAt()) + .viewCount(article.getViewCount()) .likeCount(article.getLikeCount()) + .createdAt(article.getCreatedAt()) .build(); } } diff --git a/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java b/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java index f3ae41f..f853c14 100644 --- a/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java +++ b/src/main/java/com/api/trip/domain/article/controller/dto/ReadArticleResponse.java @@ -17,8 +17,10 @@ public class ReadArticleResponse { private Long writerId; private String writerNickname; private String writerRole; + private String writerProfileImg; private String content; private long viewCount; + private long likeCount; private LocalDateTime createdAt; private Long interestArticleId; @@ -29,9 +31,11 @@ public static ReadArticleResponse of(Article article, InterestArticle interestAr .title(article.getTitle()) .writerId(writer.getId()) .writerNickname(writer.getNickname()) + .writerProfileImg(writer.getProfileImg()) .writerRole(writer.getRole().name()) .content(article.getContent()) - .viewCount(article.getViewCount()) + .viewCount(article.getViewCount() + 1) + .likeCount(article.getLikeCount()) .createdAt(article.getCreatedAt()) .interestArticleId(interestArticle != null ? interestArticle.getId() : null) .build(); diff --git a/src/main/java/com/api/trip/domain/comment/controller/dto/GetCommentsResponse.java b/src/main/java/com/api/trip/domain/comment/controller/dto/GetCommentsResponse.java index 95f86d4..d615b8d 100644 --- a/src/main/java/com/api/trip/domain/comment/controller/dto/GetCommentsResponse.java +++ b/src/main/java/com/api/trip/domain/comment/controller/dto/GetCommentsResponse.java @@ -45,6 +45,7 @@ private static class CommentDto { private Long commentId; private Long writerId; private String writerNickname; + private String writerProfileImg; private Long articleId; private String content; private Long parentId; @@ -58,6 +59,7 @@ private static CommentDto of(Comment comment) { .commentId(comment.getId()) .writerId(comment.getWriter().getId()) .writerNickname(comment.getWriter().getNickname()) + .writerProfileImg(comment.getWriter().getProfileImg()) .articleId(comment.getArticle().getId()) .content(comment.getContent()) .parentId(comment.getParent() != null ? comment.getParent().getId() : null)