-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from Parkjyun/feat/37
[feat] 마이페이지 관련 api 생성
- Loading branch information
Showing
10 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/newsnack/www/newsnackserver/common/code/success/MemberSuccessCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/newsnack/www/newsnackserver/controller/MemberController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
.../java/com/newsnack/www/newsnackserver/domain/comment/repository/CommentJpaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/newsnack/www/newsnackserver/dto/response/MemberInfoResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/newsnack/www/newsnackserver/dto/response/MemberLikedArticleResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...n/java/com/newsnack/www/newsnackserver/dto/response/MemberParticipatedDebateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/newsnack/www/newsnackserver/dto/response/MyPageCommentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/newsnack/www/newsnackserver/service/MemberService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |