-
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 #15 from SafeNet-2024/feature/post&message-api-test
Feature/post&message api test
- Loading branch information
Showing
13 changed files
with
227 additions
and
224 deletions.
There are no files selected for viewing
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
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
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
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
88 changes: 88 additions & 0 deletions
88
src/main/java/com/SafeNet/Backend/domain/post/service/CommonPostService.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,88 @@ | ||
package com.SafeNet.Backend.domain.post.service; | ||
|
||
import com.SafeNet.Backend.domain.member.entity.Member; | ||
import com.SafeNet.Backend.domain.member.repository.MemberRepository; | ||
import com.SafeNet.Backend.domain.post.dto.PostResponseDto; | ||
import com.SafeNet.Backend.domain.post.entity.Category; | ||
import com.SafeNet.Backend.domain.post.entity.Post; | ||
import com.SafeNet.Backend.domain.post.exception.PostException; | ||
import com.SafeNet.Backend.domain.post.repository.PostRepository; | ||
import com.SafeNet.Backend.domain.postLike.repository.PostLikeRepository; | ||
import com.SafeNet.Backend.domain.post.util.PostDtoConverter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CommonPostService { | ||
private final PostRepository postRepository; | ||
private final MemberRepository memberRepository; | ||
private final PostLikeRepository postLikeRepository; | ||
|
||
public Member getMemberByEmail(String email) { | ||
return memberRepository.findByEmail(email).orElseThrow(() -> new PostException("Member not found with email: " + email, HttpStatus.NOT_FOUND)); | ||
} | ||
|
||
public List<PostResponseDto> convertPostsToDto(List<Post> posts, Long memberId) { | ||
return posts.stream().map(post -> { | ||
boolean isLikedByCurrentUser = postLikeRepository.existsByPostIdAndMemberId(post.getId(), memberId); | ||
return PostDtoConverter.convertToDto(post, isLikedByCurrentUser); | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
public List<PostResponseDto> getAllPosts(String email) { | ||
Member member = getMemberByEmail(email); | ||
Long memberId = member.getId(); | ||
List<Post> posts = postRepository.findAll(); | ||
return convertPostsToDto(posts, memberId); | ||
} | ||
|
||
public List<PostResponseDto> getPostsByMemberId(String email) { | ||
Member member = getMemberByEmail(email); | ||
Long memberId = member.getId(); | ||
List<Post> posts = postRepository.findByMember_Id(memberId); | ||
return convertPostsToDto(posts, memberId); | ||
} | ||
|
||
public List<PostResponseDto> getLikedPostsByMemberId(String email) { | ||
Member member = getMemberByEmail(email); | ||
Long memberId = member.getId(); | ||
List<Post> posts = postLikeRepository.findByMember_Id(memberId).stream() | ||
.map(postLike -> postLike.getPost()) | ||
.collect(Collectors.toList()); | ||
return convertPostsToDto(posts, memberId); | ||
} | ||
|
||
public List<PostResponseDto> searchPostsByKeyword(String keyword, String email) { | ||
Member member = getMemberByEmail(email); | ||
Long memberId = member.getId(); | ||
List<Post> posts = postRepository.findByTitleContainingOrContentsContaining(keyword, keyword); | ||
return convertPostsToDto(posts, memberId); | ||
} | ||
|
||
public List<PostResponseDto> searchPostsByCategory(Category category, String email) { | ||
Member member = getMemberByEmail(email); | ||
Long memberId = member.getId(); | ||
List<Post> posts = postRepository.findByCategory(category); | ||
return convertPostsToDto(posts, memberId); | ||
} | ||
|
||
public List<PostResponseDto> sortPostsByCreated(String email) { | ||
Member member = getMemberByEmail(email); | ||
Long memberId = member.getId(); | ||
List<Post> posts = postRepository.findAllByOrderByCreatedDesc(); | ||
return convertPostsToDto(posts, memberId); | ||
} | ||
|
||
public List<PostResponseDto> sortPostsByBuyDate(String email) { | ||
Member member = getMemberByEmail(email); | ||
Long memberId = member.getId(); | ||
List<Post> posts = postRepository.findAllByOrderByBuyDateDesc(); | ||
return convertPostsToDto(posts, memberId); | ||
} | ||
} | ||
|
41 changes: 3 additions & 38 deletions
41
src/main/java/com/SafeNet/Backend/domain/post/service/MemberItemService.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,58 +1,23 @@ | ||
package com.SafeNet.Backend.domain.post.service; | ||
|
||
import com.SafeNet.Backend.domain.member.entity.Member; | ||
import com.SafeNet.Backend.domain.member.repository.MemberRepository; | ||
import com.SafeNet.Backend.domain.post.dto.PostResponseDto; | ||
import com.SafeNet.Backend.domain.post.entity.Post; | ||
import com.SafeNet.Backend.domain.post.exception.PostException; | ||
import com.SafeNet.Backend.domain.post.repository.PostRepository; | ||
import com.SafeNet.Backend.domain.postLike.entity.PostLike; | ||
import com.SafeNet.Backend.domain.postLike.repository.PostLikeRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class MemberItemService { | ||
private final PostRepository postRepository; | ||
private final PostLikeRepository postLikeRepository; | ||
private final MemberRepository memberRepository; | ||
private final CommonPostService commonPostService; | ||
|
||
public List<PostResponseDto> getPostsByMemberId(String email) { | ||
Member member = memberRepository.findByEmail(email).orElseThrow(() -> new PostException("Member not found", HttpStatus.NOT_FOUND)); | ||
try { | ||
List<Post> posts = postRepository.findByMember_Id(member.getId()); | ||
return posts.stream().map(this::convertToDto).collect(Collectors.toList()); | ||
} catch (Exception e) { | ||
throw new PostException("Failed to retrieve posts by memberId", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
return commonPostService.getPostsByMemberId(email); | ||
} | ||
|
||
public List<PostResponseDto> getLikedPostsByMemberId(String email) { | ||
Member member = memberRepository.findByEmail(email).orElseThrow(() -> new PostException("Member not found", HttpStatus.NOT_FOUND)); | ||
try { | ||
List<PostLike> postLikes = postLikeRepository.findByMember_Id(member.getId()); | ||
return postLikes.stream().map(postLike -> this.convertToDto(postLike.getPost())).collect(Collectors.toList()); | ||
} catch (Exception e) { | ||
throw new PostException("Failed to retrieve liked posts by memberId", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
return commonPostService.getLikedPostsByMemberId(email); | ||
} | ||
|
||
private PostResponseDto convertToDto(Post post) { | ||
return PostResponseDto.builder() | ||
.postId(post.getId()) | ||
.title(post.getTitle()) | ||
.likeCount(post.getPostLikeList().size()) | ||
.productImageUrl(post.getFileList().isEmpty() ? null : post.getFileList().get(1).getFileUrl()) | ||
.count(post.getCount()) | ||
.cost(post.getCost()) | ||
.build(); | ||
} | ||
|
||
} |
Oops, something went wrong.