Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/4-getMyPage] 마이페이지 조회 API #9

Merged
merged 5 commits into from
Sep 16, 2023
Merged
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 @@ -2,6 +2,8 @@

import com.ewhatever.qna.answer.entity.Answer;
import com.ewhatever.qna.post.entity.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -10,4 +12,7 @@
@Repository
public interface AnswerRepository extends JpaRepository<Answer, Long> {
List<Answer> findAllByPost(Post post);
Long countByAnswerer_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Answer> findByAnswerer_UserIdxAndPost_IsJuicyAndStatusEquals(Long userIdx, Boolean isJuicy, String status, Pageable pageable);
Long countByPost_PostIdxAndStatusEquals(Long postIdx, String status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ewhatever.qna.answer.entity.Answer;
import com.ewhatever.qna.answer.repository.AnswerRepository;
import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.enums.Role;
import com.ewhatever.qna.post.entity.Post;
import com.ewhatever.qna.post.repository.PostRepository;
import com.ewhatever.qna.user.entity.User;
Expand All @@ -25,7 +26,7 @@ public void addAnswer(Long userIdx, PostAnswerReq postAnswerReq) throws BaseExce
try {
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE);
Post post = postRepository.findById(postAnswerReq.getPostIdx()).orElseThrow(() -> new BaseException(INVALID_POST_IDX));
if (user.getRole().equals("Senior")) {//TODO: enum으로 변경
if (user.getRole().equals(Role.SINY)) {
Answer answer = Answer.builder()
.content(postAnswerReq.getAnswer())
.answerer(user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.ewhatever.qna.comment.repository;

import com.ewhatever.qna.comment.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentRepository extends JpaRepository<Comment, Long> {
Long countByWriter_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Comment> findByWriter_UserIdxAndStatusEquals(Long userIdx, String status, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum BaseResponseStatus {

// user(2400-2499)
NO_SENIOR_ROLE(false, 2400, "시니어가 아닙니다."),
INVALID_USER(false, 2401, "존재하지 않는 사용자에 대한 요청입니다."),


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ewhatever.qna.common.config;

import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.Base.BaseResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(BaseException.class)
protected BaseResponse<?> handleBaseException(BaseException e) {
return new BaseResponse<>(e.getStatus());
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/ewhatever/qna/common/enums/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ewhatever.qna.common.enums;

public enum Role {
SINY, JUNY
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
Page<Post> findAllByIsJuicy(Pageable pageable, Boolean isJuicy);
Boolean existsByCategory(Category category);
Page<Post> findByCategory(Category category, Pageable pageable);
Long countByQuestioner_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Post> findByQuestioner_UserIdxAndIsJuicyAndStatusEquals(Long userIdx, Boolean isJuicy, String status, Pageable pageable);
}
34 changes: 34 additions & 0 deletions src/main/java/com/ewhatever/qna/scrap/entity/Scrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ewhatever.qna.scrap.entity;

import com.ewhatever.qna.common.Base.BaseEntity;
import com.ewhatever.qna.post.entity.Post;
import com.ewhatever.qna.user.entity.User;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Scrap extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long scrapIdx;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_idx")
private Post post;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_idx")
private User user;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ewhatever.qna.scrap.repository;

import com.ewhatever.qna.scrap.entity.Scrap;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ScrapRepository extends JpaRepository<Scrap, Long> {
Long countByUser_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Scrap> findByUser_UserIdxAndStatusEquals(Long userIdx, String status, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
package com.ewhatever.qna.user.controller;

import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.Base.BaseResponse;
import com.ewhatever.qna.user.dto.GetCommentResponse;
import com.ewhatever.qna.user.dto.GetJunyQuestionResponse;
import com.ewhatever.qna.user.dto.GetScrapResponse;
import com.ewhatever.qna.user.dto.GetSinyAnswerResponse;
import com.ewhatever.qna.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping
@RequestMapping("/users")
@RestController
@RequiredArgsConstructor
public class UserController {

private final UserService userService;


@GetMapping
public BaseResponse<?> getProfile() throws BaseException {
return userService.getProfile();
}

@GetMapping("/juny/questions")
public BaseResponse<Page<GetJunyQuestionResponse>> getMyQuestions(@RequestParam(value = "status", defaultValue = "completed") String status,
@RequestParam(value = "requestPageNum", defaultValue = "0") int requestPageNum) throws BaseException {
return new BaseResponse<>(userService.getMyQuestions(status, requestPageNum));
}

@GetMapping("/siny/answers")
public BaseResponse<Page<GetSinyAnswerResponse>> getMyAnswers(@RequestParam(value = "status", defaultValue = "completed") String status,
@RequestParam(value = "requestPageNum", defaultValue = "0") int requestPageNum) throws BaseException {
return new BaseResponse<>(userService.getMyAnswers(status, requestPageNum));
}

@GetMapping("/comments")
public BaseResponse<Page<GetCommentResponse>> getMyComments(@RequestParam(value = "requestPageNum", defaultValue = "0") int requestPageNum) throws BaseException{
return new BaseResponse<>(userService.getMyComments(requestPageNum));
}

@GetMapping("/scrap")
public BaseResponse<Page<GetScrapResponse>> getMyScraps(@RequestParam(value = "requestPageNum", defaultValue = "0") int requestPageNum) throws BaseException{
return new BaseResponse<>(userService.getMyScraps(requestPageNum));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ewhatever.qna.user.dto;

import com.ewhatever.qna.comment.entity.Comment;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;

@Builder
@Getter
@Setter
public class GetCommentResponse {
private String content;
private LocalDate createdDate;
private Long postIdx;
private String postTitle;

//TODO : DTO 변환 메소드 위치 변경
//TODO : dto에서 LocalDateTime -> LocalDate로 변경
public static GetCommentResponse fromComment(Comment comment) {
return GetCommentResponse.builder().content(comment.getContent().substring(0, 16))
.createdDate(comment.getCreatedDate().toLocalDate())
.postIdx(comment.getPost().getPostIdx())
.postTitle(comment.getPost().getTitle()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ewhatever.qna.user.dto;

import lombok.*;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class GetJunyProfileResponse {
private String name;
private Long questionCount;
private Long commentCount;
private Long scrapCount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ewhatever.qna.user.dto;

import com.ewhatever.qna.common.enums.Category;
import com.ewhatever.qna.post.entity.Post;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static java.time.temporal.ChronoUnit.DAYS;

@Builder
@Getter
@Setter
public class GetJunyQuestionResponse {
private String title;
private LocalDateTime date;
private Category category;
private Long answerCount;
private Long daysUntilDday;

public static GetJunyQuestionResponse fromPost(Post post, Long answerCount, Boolean isJuicy) {
LocalDate targetDate = post.getCreatedDate().toLocalDate().plusDays(3);
LocalDateTime date = (isJuicy)? post.getLastModifiedDate() : post.getCreatedDate();
return GetJunyQuestionResponse.builder()
.title(post.getTitle())
.date(date)
.category(post.getCategory())
.answerCount(answerCount)
.daysUntilDday(LocalDate.now().until(targetDate, DAYS)).build();
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/ewhatever/qna/user/dto/GetScrapResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ewhatever.qna.user.dto;

import com.ewhatever.qna.common.enums.Category;
import com.ewhatever.qna.post.entity.Post;
import com.ewhatever.qna.scrap.entity.Scrap;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Builder
@Setter
@Getter
public class GetScrapResponse {
private String postTitle;
private Long postIdx;
private Category category;
private String content;
private Long commentCount;
private Long scrapCount;
private LocalDateTime createdDate;

//TODO : substring말고 잘라진 부분만 가져오게 수정?
public static GetScrapResponse fromScrap(Scrap scrap) {
Post post = scrap.getPost();
return GetScrapResponse.builder().postTitle(post.getTitle())
.postIdx(post.getPostIdx())
.category(post.getCategory())
.content(post.getContent().substring(0, 16))
.commentCount(post.getCommentCount())
.scrapCount(post.getScrapCount())
.createdDate(post.getLastModifiedDate()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ewhatever.qna.user.dto;

import com.ewhatever.qna.answer.entity.Answer;
import com.ewhatever.qna.common.enums.Category;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static java.time.temporal.ChronoUnit.DAYS;

@Builder
@Setter
@Getter
public class GetSinyAnswerResponse {
private String content;
private LocalDateTime date;
private Category category;
private Long answerCount;
private Long daysUntilDday;

public static GetSinyAnswerResponse fromAnswer(Answer answer, Long answerCount, Boolean isJuicy) {
LocalDate targetDate = answer.getPost().getCreatedDate().toLocalDate().plusDays(3);
LocalDateTime date = (isJuicy)? answer.getPost().getLastModifiedDate() : answer.getPost().getCreatedDate();
return GetSinyAnswerResponse.builder()
.content(answer.getContent().substring(0, 16))
.date(date)
.category(answer.getPost().getCategory())
.answerCount(answerCount)
.daysUntilDday(LocalDate.now().until(targetDate, DAYS)).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ewhatever.qna.user.dto;

import lombok.*;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class GetSinyProfileResponse {
private String name;
private Long answerCount;
private Long commentCount;
private Long scrapCount;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.ewhatever.qna.user.dto;

public class UserResponse {

}
11 changes: 4 additions & 7 deletions src/main/java/com/ewhatever/qna/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.ewhatever.qna.user.entity;

import com.ewhatever.qna.common.Base.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import com.ewhatever.qna.common.enums.Role;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -34,7 +32,6 @@ public class User extends BaseEntity {
private String provider;

@NotNull
@Size(max = 30)
private String role;

@Enumerated(value = EnumType.ORDINAL)//TODO : EnumType 수정
private Role role;
}
Loading