Skip to content

Commit

Permalink
FIX : merge 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
longrunBiin committed Feb 12, 2024
2 parents 9e87f10 + 9fd89a0 commit aabb3ab
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 38 deletions.
12 changes: 10 additions & 2 deletions src/main/java/umc/IRECIPE_Server/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ public ApiResponse<?> getPostsPage(@RequestParam(required = false, value = "page
@RequestParam(required = false, value = "criteria") String criteria
)
{
return postService.getPostsPage(page, criteria);
// 현재 토큰을 사용중인 유저 고유 id 조회
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String userId = authentication.getName();

return postService.getPostsPage(page, criteria, userId);
}

@Operation(
Expand Down Expand Up @@ -183,7 +187,11 @@ public ApiResponse<?> searchPost(@RequestParam(required = false, value = "page")
@RequestParam(required = false, value = "type") String type
)
{
return postService.searchPost(keyword, type, page);
// 현재 토큰을 사용중인 유저 고유 id 조회
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String userId = authentication.getName();

return postService.searchPost(keyword, type, page, userId);
}

@Operation(
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/umc/IRECIPE_Server/converter/PostConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import umc.IRECIPE_Server.entity.Post;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PostConverter {
Expand All @@ -38,7 +39,7 @@ public static PostResponseDTO.getTempDTO toTempResponseDTO(Post post){



public static PostResponseDTO.getDTO toGetResponseDTO(Post post, Member member){
public static PostResponseDTO.getDTO toGetResponseDTO(Post post, Member member, boolean likeOrNot){
return PostResponseDTO.getDTO.builder()
.postId(post.getId())
.title(post.getTitle())
Expand All @@ -54,6 +55,7 @@ public static PostResponseDTO.getDTO toGetResponseDTO(Post post, Member member){
.writerImage(member.getProfileImage())
.createdAt(post.getCreatedAt().toLocalDate())
.reviewsCount(post.getReviewList().size())
.likeOrNot(likeOrNot)
.build();
}

Expand All @@ -63,7 +65,7 @@ public static PostResponseDTO.updateDTO toUpdateResponseDTO(Post post){
.build();
}

public static List<PostResponseDTO.getAllPostDTO> toGetAllPostDTO(Page<Post> postPage){
public static List<PostResponseDTO.getAllPostDTO> toGetAllPostDTO(Page<Post> postPage, Map<Long, Boolean> likeMap){
return postPage.stream()
.map(m -> PostResponseDTO.getAllPostDTO.builder()
.postId(m.getId())
Expand All @@ -76,6 +78,7 @@ public static List<PostResponseDTO.getAllPostDTO> toGetAllPostDTO(Page<Post> pos
.nickName(m.getMember().getNickname())
.memberImage(m.getMember().getProfileImage())
.createdAt(m.getCreatedAt().toLocalDate())
.likeOrNot(likeMap.get(m.getId()))
.build())
.collect(Collectors.toList());
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/umc/IRECIPE_Server/dto/MemberRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.NoArgsConstructor;
import umc.IRECIPE_Server.validation.annotation.ExistAllergies;
import umc.IRECIPE_Server.validation.annotation.ExistNickname;
import umc.IRECIPE_Server.validation.annotation.ExistPersonalId;

public class MemberRequest {
@Builder
Expand Down Expand Up @@ -51,6 +52,8 @@ public static class findMemberIdDto{

@Getter
@Valid
@NoArgsConstructor
@AllArgsConstructor
public static class JoinDto{
@NotBlank
String name;
Expand All @@ -67,7 +70,7 @@ public static class JoinDto{
@ExistAllergies
List<Long> allergyList;

@NotBlank
@ExistPersonalId
String personalId;
}
}
12 changes: 6 additions & 6 deletions src/main/java/umc/IRECIPE_Server/dto/request/PostRequestDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public static class newRequestDTO{
private String title;
@Schema(description = "게시글 소제목", defaultValue = "간장게장은 한국의 전통 음식 중 하나로...")
private String subhead;
@Schema(description = "음식 카테고리", defaultValue = "WESTERN/JAPANESE/CHINESE/KOREAN/UNIQUE/SIMPLE/ADVANCED")
@Schema(description = "음식 카테고리", defaultValue = "WESTERN")
private Category category;
@Schema(description = "난이도", defaultValue = "EASY/MID/DIFFICULT")
@Schema(description = "난이도", defaultValue = "EASY")
private Level level;
@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "TEMP/POST")
@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "POST")
private Status status;
@Schema(description = "게시글 내용", defaultValue = "재료 - 꽃게: 2마리 - 간장: 1컵 ...")
private String content;
Expand All @@ -46,11 +46,11 @@ public static class patchRequestDTO{
private String title;
@Schema(description = "게시글 소제목", defaultValue = "간장게장은 한국의 전통 음식 중 하나로...")
private String subhead;
@Schema(description = "음식 카테고리", defaultValue = "WESTERN/JAPANESE/CHINESE/KOREAN/UNIQUE/SIMPLE/ADVANCED")
@Schema(description = "음식 카테고리", defaultValue = "WESTERN")
private Category category;
@Schema(description = "난이도", defaultValue = "EASY/MID/DIFFICULT")
@Schema(description = "난이도", defaultValue = "EASY")
private Level level;
@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "TEMP/POST")
@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "POST")
private Status status;
@Schema(description = "게시글 내용", defaultValue = "재료 - 꽃게: 2마리 - 간장: 1컵 ...")
private String content;
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/umc/IRECIPE_Server/dto/response/PostResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public static class getTempDTO{
@Schema(description = "게시글 내용", defaultValue = "재료 - 꽃게: 2마리 - 간장: 1컵 ...")
private String content;

@Schema(description = "음식 카테고리", defaultValue = "WESTERN/JAPANESE/CHINESE/KOREAN/UNIQUE/SIMPLE/ADVANCED")
@Schema(description = "음식 카테고리", defaultValue = "WESTERN")
private Category category;

@Schema(description = "난이도", defaultValue = "EASY/MID/DIFFICULT")
@Schema(description = "난이도", defaultValue = "EASY")
private Level level;

@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "TEMP/POST")
@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "POST")
private Status status;

@Schema(description = "게시글 사진", defaultValue = "~~~~/images/test.png")
@Schema(description = "게시글 사진")
private String imageUrl;

@Schema(description = "게시글 생성일", defaultValue = "2024/02/08")
@Schema(description = "게시글 생성일")
private LocalDate createdAt;
}

Expand All @@ -81,31 +81,34 @@ public static class getDTO{
@Schema(description = "게시글 관심수", defaultValue = "3")
private int likes;

@Schema(description = "음식 카테고리", defaultValue = "WESTERN/JAPANESE/CHINESE/KOREAN/UNIQUE/SIMPLE/ADVANCED")
@Schema(description = "음식 카테고리", defaultValue = "WESTERN")
private Category category;

@Schema(description = "난이도", defaultValue = "EASY/MID/DIFFICULT")
@Schema(description = "난이도", defaultValue = "EASY")
private Level level;

@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "TEMP/POST")
@Schema(description = "게시글 상태(임시저장 여부)", defaultValue = "POST")
private Status status;

@Schema(description = "게시글 평균 별점", defaultValue = "4.3")
private Float score;

@Schema(description = "게시글 사진", defaultValue = "~~~~/images/test.png")
@Schema(description = "게시글 사진")
private String imageUrl;

@Schema(description = "작성자 닉네임", defaultValue = "Alber")
private String writerNickName;

@Schema(description = "작성자 프로필 사진", defaultValue = "~~~~~/Alber.jpg")
@Schema(description = "작성자 프로필 사진")
private String writerImage;

@Schema(description = "게시글 리뷰 갯수", defaultValue = "13")
private int reviewsCount;

@Schema(description = "게시글 생성일", defaultValue = "2024/02/08")
@Schema(description = "해당 사용자가 관심 눌렀는지", defaultValue = "false")
private boolean likeOrNot;

@Schema(description = "게시글 생성일")
private LocalDate createdAt;
}

Expand Down Expand Up @@ -147,13 +150,13 @@ public static class getAllPostDTO{
@Schema(description = "게시글 소제목", defaultValue = "간장게장은 한국의 전통 음식 중 하나로...")
private String subhead;

@Schema(description = "게시글 사진", defaultValue = "~~~~/images/test.png")
@Schema(description = "게시글 사진")
private String imageUrl;

@Schema(description = "작성자 닉네임", defaultValue = "Alber")
private String nickName;

@Schema(description = "작성자 프로필 사진", defaultValue = "~~~~~/Alber.jpg")
@Schema(description = "작성자 프로필 사진")
private String memberImage;

@Schema(description = "게시글 관심수", defaultValue = "3")
Expand All @@ -165,8 +168,11 @@ public static class getAllPostDTO{
@Schema(description = "게시글 리뷰 갯수", defaultValue = "13")
private int reviewsCount;

@Schema(description = "게시글 생성일", defaultValue = "2024/02/08")
@Schema(description = "게시글 생성일")
private LocalDate createdAt;

@Schema(description = "해당 사용자가 관심 눌렀는지", defaultValue = "false")
private boolean likeOrNot;
}

@Schema(description = "게시글 관심 응답 DTO")
Expand Down Expand Up @@ -195,7 +201,7 @@ public static class getRankedPostDTO {
@Schema(description = "게시글 제목", defaultValue = "간장게장")
private String title;

@Schema(description = "게시글 사진", defaultValue = "~~~~/images/test.png")
@Schema(description = "게시글 사진")
private String imageUrl;

@Schema(description = "게시글 관심수", defaultValue = "3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import umc.IRECIPE_Server.entity.MemberLikes;
import umc.IRECIPE_Server.entity.Post;

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

public interface MemberLikesRepository extends JpaRepository<MemberLikes, Long> {

Optional<MemberLikes> findByMemberAndPost(Member member, Post post);

Page<MemberLikes> findByMember(Member member, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findById(Long id);

Boolean existsByNickname(String nickname);
Boolean existsByPersonalId(String id);

Member findByNickname(String nickname);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/umc/IRECIPE_Server/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public Boolean findMemberByNickname(String Nickname){
return memberRepository.existsByNickname(Nickname);
}

public Optional<Member> findMember(Long id) {
return memberRepository.findById(id);
public Boolean findMemberByPersonalId(String id){
return memberRepository.existsByPersonalId(id);
}

public Member findMember(String personalId) {
Expand Down
55 changes: 45 additions & 10 deletions src/main/java/umc/IRECIPE_Server/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@

import umc.IRECIPE_Server.repository.StoredRecipeRepository;

import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.Option;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -112,7 +112,9 @@ public ApiResponse<?> getPost(Long postId, String userId){
Optional<Member> memberOptional = memberRepository.findByPersonalId(userId);
memberOptional.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

return ApiResponse.onSuccess(PostConverter.toGetResponseDTO(post, memberOptional.get()));
boolean likeOrNot = memberLikesRepository.findByMemberAndPost(memberOptional.get(), post).isPresent();

return ApiResponse.onSuccess(PostConverter.toGetResponseDTO(post, memberOptional.get(), likeOrNot));

}

Expand Down Expand Up @@ -156,12 +158,22 @@ public ApiResponse<?> deletePost(Post post){
}

// 커뮤니티 화면 조회
public ApiResponse<?> getPostsPage(int page, String criteria){
public ApiResponse<?> getPostsPage(int page, String criteria, String userId){

Pageable pageable = PageRequest.of(page, 10, Sort.by(Sort.Direction.DESC, criteria));
Page<Post> postPage = postRepository.findAllByStatus(pageable, Status.POST);

return ApiResponse.onSuccess(PostConverter.toGetAllPostDTO(postPage));
Member member = memberRepository.findByPersonalId(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

// memberLike 에서 찾으면 관심 눌렀던 게시글, 못 찾으면 관심 안 누른 게시글
Map<Long, Boolean> likeMap = new HashMap<>();
for (Post post : postPage) {
Boolean likeOrNot = memberLikesRepository.findByMemberAndPost(member, post).isPresent();
likeMap.put(post.getId(), likeOrNot);
}

return ApiResponse.onSuccess(PostConverter.toGetAllPostDTO(postPage, likeMap));
}

// 게시글에 관심 눌렀을 때
Expand Down Expand Up @@ -214,40 +226,63 @@ public ApiResponse<?> deleteLike(String userId, Long postId){
}

// 게시글 검색
public ApiResponse<?> searchPost(String keyword, String type, int page){
public ApiResponse<?> searchPost(String keyword, String type, int page, String userId){

// 멤버 찾기. null 이면 예외 처리 (NosuchElementException)
Member member = memberRepository.findByPersonalId(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

Page<Post> postPage;
Pageable pageable = PageRequest.of(page, 10);
Map<Long, Boolean> likeMap = new HashMap<>();

if (type.equals("title")){
postPage = postRepository.findByTitleContainingAndStatus(pageable, keyword, Status.POST);
if(postPage.isEmpty()){
throw new GeneralException(ErrorStatus.POST_NOT_FOUND);
}

// memberLike 에서 찾으면 관심 눌렀던 게시글, 못 찾으면 관심 안 누른 게시글
for (Post post : postPage) {
Boolean likeOrNot = memberLikesRepository.findByMemberAndPost(member, post).isPresent();
likeMap.put(post.getId(), likeOrNot);
}
}
else if(type.equals("content")){
postPage = postRepository.findByContentContainingAndStatus(pageable, keyword, Status.POST);
if(postPage.isEmpty()){
throw new GeneralException(ErrorStatus.POST_NOT_FOUND);
}

// memberLike 에서 찾으면 관심 눌렀던 게시글, 못 찾으면 관심 안 누른 게시글
for (Post post : postPage) {
Boolean likeOrNot = memberLikesRepository.findByMemberAndPost(member, post).isPresent();
likeMap.put(post.getId(), likeOrNot);
}
}
else if(type.equals("writer")){
// 멤버 찾기. null 이면 예외 처리 (NosuchElementException)
Member member = memberRepository.findByNickname(keyword);
if(member == null){
Member keyMember = memberRepository.findByNickname(keyword);
if(keyMember == null){
throw new GeneralException(ErrorStatus.MEMBER_NOT_FOUND);
}

postPage = postRepository.findByMemberAndStatus(pageable, member, Status.POST);
postPage = postRepository.findByMemberAndStatus(pageable, keyMember, Status.POST);
if(postPage.isEmpty()){
throw new GeneralException(ErrorStatus.POST_NOT_FOUND);
}

// memberLike 에서 찾으면 관심 눌렀던 게시글, 못 찾으면 관심 안 누른 게시글
for (Post post : postPage) {
Boolean likeOrNot = memberLikesRepository.findByMemberAndPost(member, post).isPresent();
likeMap.put(post.getId(), likeOrNot);
}
}
else {
throw new GeneralException(ErrorStatus.CONTENT_NOT_EXIST);
}

return ApiResponse.onSuccess(PostConverter.toGetAllPostDTO(postPage));
return ApiResponse.onSuccess(PostConverter.toGetAllPostDTO(postPage, likeMap));
}

public Page<Post> getRanking(Integer page) {
Expand Down
Loading

0 comments on commit aabb3ab

Please sign in to comment.