Skip to content

Commit

Permalink
Merge pull request #38 from Parkjyun/feat/37
Browse files Browse the repository at this point in the history
[feat] 마이페이지 관련 api 생성
  • Loading branch information
Parkjyun authored Apr 4, 2024
2 parents f8e6185 + 49b0595 commit 77d67c0
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.newsnack.www.newsnackserver.common.code.success;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@RequiredArgsConstructor
public enum MemberSuccessCode implements SuccessCode{
/**
* 200 OK
**/
GET_LOGIN_USER_INFO_SUCCESS(HttpStatus.OK, "로그인 유저 정보 조회 성공"),
GET_LIKED_ARTICLES_SUCCESS(HttpStatus.OK, "좋아요한 게시글 조회 성공"),
GET_PARTICIPATED_DEBATES_SUCCESS(HttpStatus.OK, "참여한 토론들 조회 성공"),
GET_MY_COMMENTS_SUCCESS(HttpStatus.OK, "나의 댓글 목록 조회 성공");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.newsnack.www.newsnackserver.controller;

import com.newsnack.www.newsnackserver.annotation.MemberId;
import com.newsnack.www.newsnackserver.common.code.success.MemberSuccessCode;
import com.newsnack.www.newsnackserver.common.response.NewSnackResponse;
import com.newsnack.www.newsnackserver.dto.response.MemberInfoResponse;
import com.newsnack.www.newsnackserver.dto.response.MemberLikedArticleResponse;
import com.newsnack.www.newsnackserver.dto.response.MemberParticipatedDebateResponse;
import com.newsnack.www.newsnackserver.dto.response.MyPageCommentResponse;
import com.newsnack.www.newsnackserver.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/members")
@RequiredArgsConstructor
public class MemberController {

private final MemberService memberService;

@GetMapping("/me")
public NewSnackResponse<MemberInfoResponse> getLoginUserInfo(@MemberId Long memberId){
return NewSnackResponse.success(MemberSuccessCode.GET_LOGIN_USER_INFO_SUCCESS, memberService.getMemberInfo(memberId));
}

@GetMapping("/me/liked-articles")
public NewSnackResponse<List<MemberLikedArticleResponse>> getLikedArticles(@MemberId Long memberId){
return NewSnackResponse.success(MemberSuccessCode.GET_LIKED_ARTICLES_SUCCESS, memberService.getLikedArticles(memberId));
}

@GetMapping("/me/participated-debates")
public NewSnackResponse<List<MemberParticipatedDebateResponse>> getParticipatedDebates(@MemberId Long memberId){
return NewSnackResponse.success(MemberSuccessCode.GET_PARTICIPATED_DEBATES_SUCCESS, memberService.getParticipatedDebates(memberId));
}

@GetMapping("/me/comments")
public NewSnackResponse<List<MyPageCommentResponse>> getMyComments(@MemberId Long memberId) {
return NewSnackResponse.success(MemberSuccessCode.GET_MY_COMMENTS_SUCCESS, memberService.getMyComments(memberId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import com.newsnack.www.newsnackserver.domain.articleheart.model.ArticleHeart;
import com.newsnack.www.newsnackserver.domain.member.model.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface ArticleHeartJpaRepository extends JpaRepository<ArticleHeart, Long> {
Optional<ArticleHeart> findByArticleAndMember(Article article, Member member);

@Query("select ah from ArticleHeart ah join fetch ah.article where ah.member.id = :memberId order by ah.id desc")
List<ArticleHeart> findAllWithArticleByMemberIdJPQL(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.newsnack.www.newsnackserver.domain.comment.repository;

import com.newsnack.www.newsnackserver.domain.comment.model.Comment;
import com.newsnack.www.newsnackserver.dto.response.MyPageCommentResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CommentJpaRepository extends JpaRepository<Comment, Long> {

@Query("select c from Comment c join fetch c.member where c.article.id = :articleId order by c.id desc")
List<Comment> findAllWithMemberByArticleIdOrderByIdDesc(Long articleId);

@Query("select c from Comment c join fetch c.member where c.article.id = :articleId order by c.heartCount desc, c.id desc")
List<Comment> findAllWithMemberByArticleIdOrderByHeartCountDesc(Long articleId);

@Query("select distinct c from Comment c left join fetch c.commentHearts join fetch c.member where c.article.id = :articleId order by c.id desc")
List<Comment> findAllWithMemberAndCommentHeartByArticleIdOrderByIdDesc(Long articleId);

@Query("select distinct c from Comment c left join fetch c.commentHearts join fetch c.member where c.article.id = :articleId order by c.heartCount desc, c.id desc")
List<Comment> findAllWithMemberAndCommentHeartByArticleIdOrderByHeartCountDesc(Long articleId);

@Query("select new com.newsnack.www.newsnackserver.dto.response.MyPageCommentResponse(a.id, c.content, a.title) " +
"from Comment c join c.article a " +
"where c.member.id = :memberId order by c.id desc")
List<MyPageCommentResponse> findAllByMemberIdJPQL(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Optional;

public interface DebateParticipationJpaRepository extends JpaRepository<DebateParticipation, Long> {

Optional<DebateParticipation> findByDebateAndMember(Debate debate, Member member);

@Query("select dp from DebateParticipation dp join fetch dp.member where dp.debate.id = :debateId order by dp.id desc")
Expand All @@ -20,6 +21,10 @@ public interface DebateParticipationJpaRepository extends JpaRepository<DebatePa

@Query("select distinct dp from DebateParticipation dp left join fetch dp.debateParticipationHearts join fetch dp.member where dp.debate.id = :debateId order by dp.id desc")
List<DebateParticipation> findAllWithMemberAndDebateParticipationHeartByDebateIdOrderByIdDescJPQL(Long debateId);

@Query("select distinct dp from DebateParticipation dp left join fetch dp.debateParticipationHearts join fetch dp.member where dp.debate.id = :debateId order by dp.heartCount desc, dp.id desc")
List<DebateParticipation> findAllWithMemberAndDebateParticipationHeartByDebateIdOrderByHeartCountDescJPQL(Long debateId);

@Query("select dp from DebateParticipation dp join fetch dp.debate where dp.member.id = :memberId order by dp.id desc")
List<DebateParticipation> findAllWithDebateByMemberIdJPQL(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.newsnack.www.newsnackserver.dto.response;

import com.newsnack.www.newsnackserver.domain.member.model.Member;

public record MemberInfoResponse(String name) {
public static MemberInfoResponse of(Member member) {
return new MemberInfoResponse(member.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.newsnack.www.newsnackserver.dto.response;

import com.newsnack.www.newsnackserver.domain.article.model.Article;
import com.newsnack.www.newsnackserver.domain.article.model.SectionCategory;

public record MemberLikedArticleResponse(Long id, String title, String imageUrl, String createdAt, SectionCategory sectionCategory) {
public static MemberLikedArticleResponse of(Article article) {
return new MemberLikedArticleResponse(article.getId(), article.getTitle(), article.getImageUrl(), article.getCreatedAt().toString(), article.getSectionCategory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.newsnack.www.newsnackserver.dto.response;

import com.newsnack.www.newsnackserver.domain.debate.model.Debate;

public record MemberParticipatedDebateResponse(Long id, String title) {
public static MemberParticipatedDebateResponse of(Debate debate) {
return new MemberParticipatedDebateResponse(debate.getId(), debate.getTitle());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.newsnack.www.newsnackserver.dto.response;

public record MyPageCommentResponse(Long articleId, String content, String articleTitle) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.newsnack.www.newsnackserver.service;

import com.newsnack.www.newsnackserver.common.code.failure.MemberFailureCode;
import com.newsnack.www.newsnackserver.common.exception.MemberException;
import com.newsnack.www.newsnackserver.domain.article.model.Article;
import com.newsnack.www.newsnackserver.domain.articleheart.model.ArticleHeart;
import com.newsnack.www.newsnackserver.domain.articleheart.repository.ArticleHeartJpaRepository;
import com.newsnack.www.newsnackserver.domain.comment.repository.CommentJpaRepository;
import com.newsnack.www.newsnackserver.domain.debate.model.Debate;
import com.newsnack.www.newsnackserver.domain.debateparticipation.model.DebateParticipation;
import com.newsnack.www.newsnackserver.domain.debateparticipation.repository.DebateParticipationJpaRepository;
import com.newsnack.www.newsnackserver.domain.member.model.Member;
import com.newsnack.www.newsnackserver.domain.member.repository.MemberJpaRepository;
import com.newsnack.www.newsnackserver.dto.response.MemberInfoResponse;
import com.newsnack.www.newsnackserver.dto.response.MemberLikedArticleResponse;
import com.newsnack.www.newsnackserver.dto.response.MemberParticipatedDebateResponse;
import com.newsnack.www.newsnackserver.dto.response.MyPageCommentResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberService {
private final MemberJpaRepository memberJpaRepository;
private final ArticleHeartJpaRepository articleHeartJpaRepository;
private final DebateParticipationJpaRepository debateParticipationJpaRepository;
private final CommentJpaRepository commentJpaRepository;


public MemberInfoResponse getMemberInfo(Long memberId) {
Member member = memberJpaRepository.findById(memberId).orElseThrow(() -> new MemberException(MemberFailureCode.MEMBER_NOT_FOUND));
return MemberInfoResponse.of(member);
}
//todo: paging추가, dto로 받기
public List<MemberLikedArticleResponse> getLikedArticles(Long memberId) {
List<Article> articles = articleHeartJpaRepository.findAllWithArticleByMemberIdJPQL(memberId).stream().map(ArticleHeart::getArticle).toList();
return articles.stream().map(article -> MemberLikedArticleResponse.of(article)).toList();
}
//todo: paging추가, dto로 받기
public List<MemberParticipatedDebateResponse> getParticipatedDebates(Long memberId) {
List<Debate> debates = debateParticipationJpaRepository.findAllWithDebateByMemberIdJPQL(memberId).stream().map(DebateParticipation::getDebate).toList();
return debates.stream().map(debate -> MemberParticipatedDebateResponse.of(debate)).toList();
}

public List<MyPageCommentResponse> getMyComments(Long memberId) {
return commentJpaRepository.findAllByMemberIdJPQL(memberId);
}
}

0 comments on commit 77d67c0

Please sign in to comment.