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/10 #197

Merged
merged 6 commits into from
Jun 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public enum SuccessStatus implements BaseCode {
//Member
MEMBER_DELETE_SUCCESS(HttpStatus.OK, "MEMBER2001", "νšŒμ› νƒˆν‡΄ μ„±κ³΅μž…λ‹ˆλ‹€."),
MEMBER_NOTIFICATION_UPDATE(HttpStatus.OK, "MEMBER2002", "νšŒμ› μ•Œλ¦Ό λ³€κ²½ μ„±κ³΅μž…λ‹ˆλ‹€."),
MEMBER_ACCOUNT_UPDATE(HttpStatus.OK,"MEMBER2003", "νšŒμ› κ³„μ’Œ λ³€κ²½ μ„±κ³΅μž…λ‹ˆλ‹€."),
MEMBER_ACCOUNT_UPDATE(HttpStatus.OK, "MEMBER2003", "νšŒμ› κ³„μ’Œ λ³€κ²½ μ„±κ³΅μž…λ‹ˆλ‹€."),
MEMBER_ACCOUNT_DELETE(HttpStatus.OK, "MEMBER2004", "νšŒμ› κ³„μ’Œ μ‚­μ œ μ„±κ³΅μž…λ‹ˆλ‹€."),
MEMBER_ADDRESS_DELETE(HttpStatus.OK, "MEMBER2005", "νšŒμ› 배솑지 μ‚­μ œ μ„±κ³΅μž…λ‹ˆλ‹€."),
MEMBER_CUSTOM_INFO_UPDATE(HttpStatus.OK, "MEMBER2006", "νšŒμ› 맞좀 정보 λ³€κ²½ μ„±κ³΅μž…λ‹ˆλ‹€."),
Expand All @@ -32,11 +32,12 @@ public enum SuccessStatus implements BaseCode {
POST_LIKE_SUCCESS(HttpStatus.OK, "POST2006", "포슀트 μ’‹μ•„μš” μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_DELETE_LIKE_SUCCESS(HttpStatus.OK, "POST2007", "포슀트 μ’‹μ•„μš” μ·¨μ†Œ μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_COMMENT_SUCCESS(HttpStatus.OK, "POST2008", "포슀트 λŒ“κΈ€ 달기 μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK,"POST2009", "포슀트 λŒ“κΈ€ μ‚­μ œ μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK, "POST2009", "포슀트 λŒ“κΈ€ μ‚­μ œ μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_UPDATE_COMMENT_SUCCESS(HttpStatus.OK, "POST2010", "포슀트 λŒ“κΈ€ μˆ˜μ • μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_COMMENT_LIKE_SUCCESS(HttpStatus.OK, "POST2011", "포슀트 λŒ“κΈ€ μ’‹μ•„μš” μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_DELETE_COMMENT_LIKE_SUCCESS(HttpStatus.OK,"POST2012", "포슀트 λŒ“κΈ€ μ’‹μ•„μš” μ·¨μ†Œ μ„±κ³΅μž…λ‹ˆλ‹€.")
;
POST_DELETE_COMMENT_LIKE_SUCCESS(HttpStatus.OK, "POST2012", "포슀트 λŒ“κΈ€ μ’‹μ•„μš” μ·¨μ†Œ μ„±κ³΅μž…λ‹ˆλ‹€."),
POST_GET_SUCCESS(HttpStatus.OK, "POST2013", "포슀트 μ’‹μ•„μš”μˆœ 전체 쑰회 μ„±κ³΅μž…λ‹ˆλ‹€.");


private final HttpStatus httpStatus;
private final String code;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PostRepository extends JpaRepository<Post, Long> {
//Page<Post> getPostsOrderByLikes(Pageable pageable);

@Modifying(clearAutomatically = true)
@Query("UPDATE Post p SET p.likesCount = p.likesCount +1 WHERE p.id= :postId")
Expand All @@ -16,5 +17,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {
@Query("update Post p SET p.likesCount = p.likesCount -1 WHERE p.id= :postId")
void updateUnlikeCount(Long postId);


@Modifying(clearAutomatically = true)
@Query("SELECT p FROM Post p ORDER BY p.likesCount DESC")
List<Post> findAllByOrderByLikesCountDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ public interface PostCommandService {
void updateComment(Member member, Long postId, Long commentId, PostRequestDto.UpdateCommentDTO request);

void likeComment(Member member, Long commentId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,30 @@ public class PostCommandServiceImpl implements PostCommandService {
@Override
public void follow(Long followingId, Member follower) {

if(followingId.equals(follower.getId())){
if (followingId.equals(follower.getId())) {
throw new PostHandler(ErrorStatus.POST_SELF_FOLLOW);
}
Optional<Follow> check = followRepository.findByFollowingIdAndFollowerId(followingId,follower.getId());
if(!check.isEmpty()){
Optional<Follow> check = followRepository.findByFollowingIdAndFollowerId(followingId, follower.getId());
if (!check.isEmpty()) {
throw new PostHandler(ErrorStatus.POST_ALREADY_FOLLOW);
}

Member following = memberRepository.findById(followingId).orElseThrow(()->new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
Follow follow = PostConverter.toFollow(follower,following);
Member following = memberRepository.findById(followingId).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
Follow follow = PostConverter.toFollow(follower, following);
followRepository.save(follow);
}

@Override
public void deleteFollow(Long followingId, Long followerId) {

Follow follow = followRepository.findByFollowingIdAndFollowerId(followingId,followerId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_FOLLOW_NOT_FOUND));
Follow follow = followRepository.findByFollowingIdAndFollowerId(followingId, followerId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_FOLLOW_NOT_FOUND));

followRepository.delete(follow);
}

@Override
public void registerPost(Member member, String content, List<MultipartFile> multipartFileList) {
Post post = PostConverter.toPost(member,content);
Post post = PostConverter.toPost(member, content);

postRepository.save(post);
if (multipartFileList != null) {
Expand All @@ -84,7 +84,7 @@ public void registerPost(Member member, String content, List<MultipartFile> mult
}

@Override
public void updatePost(Member member,Long postId, String content, List<MultipartFile> multipartFileList) {
public void updatePost(Member member, Long postId, String content, List<MultipartFile> multipartFileList) {

Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND));
post.updatePost(content);
Expand All @@ -104,7 +104,6 @@ public void updatePost(Member member,Long postId, String content, List<Multipart
}



}


Expand All @@ -121,13 +120,13 @@ public void likePost(Member member, Long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND));
Optional<PostLike> postLike = postLikeRepository.findByMember_IdAndPost_Id(member.getId(), postId);

if(postLike.isPresent()) {
if (postLike.isPresent()) {
if (post.getLikesCount() > 0) {
postRepository.updateUnlikeCount(postId);
}

postLikeRepository.delete(postLike.get());
}else {
} else {
postRepository.updateLikeCount(post.getId());

postLikeRepository.save(PostConverter.toPostLike(member, post));
Expand All @@ -154,7 +153,7 @@ public void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO
public void updateComment(Member member, Long postId, Long commentId, PostRequestDto.UpdateCommentDTO request) {

Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_COMMENT_NOT_FOUND));
if(!comment.getMember().equals(member)){
if (!comment.getMember().equals(member)) {
throw new PostHandler(ErrorStatus.POST_COMMENT_NOT_UPDATE);
}

Expand All @@ -169,15 +168,14 @@ public void likeComment(Member member, Long commentId) {
Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_COMMENT_NOT_FOUND));
Optional<CommentLike> commentLike = commentLikeRepository.findByMember_IdAndComment_Id(member.getId(), commentId);

if(commentLike.isPresent()){
if(comment.getLikesCount() > 0){
if (commentLike.isPresent()) {
if (comment.getLikesCount() > 0) {
commentRepository.updateUnlikeCount(commentId);
}
commentLikeRepository.delete(commentLike.get());
}else {
} else {
commentRepository.updateLikeCount(commentId);
commentLikeRepository.save(PostConverter.toCommentLike(member, comment));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
import java.util.List;

public interface PostQueryService {
List<PostResponseDto.PostViewDto> getPostsOrderByLike();

List<PostResponseDto> getAllPostsSortedByLikes();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.umc.TheGoods.service.PostService;

import com.umc.TheGoods.domain.community.Post;
import com.umc.TheGoods.repository.post.PostRepository;
import com.umc.TheGoods.web.dto.post.PostResponseDto;
import lombok.RequiredArgsConstructor;
Expand All @@ -8,6 +9,7 @@
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service
Expand All @@ -18,7 +20,10 @@ public class PostQueryServiceImpl implements PostQueryService {
private final PostRepository postRepository;

@Override
public List<PostResponseDto.PostViewDto> getPostsOrderByLike() {
return null;
public List<PostResponseDto> getAllPostsSortedByLikes() {
List<Post> posts = postRepository.findAllByOrderByLikesCountDesc();
return posts.stream()
.map(PostResponseDto::new)
.collect(Collectors.toList());
}
}
65 changes: 31 additions & 34 deletions src/main/java/com/umc/TheGoods/web/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.umc.TheGoods.apiPayload.code.status.ErrorStatus;
import com.umc.TheGoods.apiPayload.code.status.SuccessStatus;
import com.umc.TheGoods.apiPayload.exception.handler.MemberHandler;
import com.umc.TheGoods.domain.member.Auth;
import com.umc.TheGoods.domain.member.Member;
import com.umc.TheGoods.service.MemberService.MemberQueryService;
import com.umc.TheGoods.service.PostService.PostCommandService;
Expand All @@ -15,7 +14,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import net.bytebuddy.build.Plugin;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -28,30 +26,37 @@
@Tag(name = "Post", description = "포슀트 κ΄€λ ¨λœ API μž…λ‹ˆλ‹€.")
public class PostController {

final PostQueryService queryService;
private final PostQueryService postQueryService;
private final MemberQueryService memberQueryService;
final PostCommandService postCommandService;

@GetMapping("/popular")
@Operation(summary = "인기 사μž₯λ‹˜ ν”Όλ“œ 전체 쑰회 API", description = "포슀트의 κΈ°λ³Έ λ””ν΄νŠΈ μ •λ ¬λ‘œ, μ’‹μ•„μš” 순으둜 λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•©λ‹ˆλ‹€. \n\n")
@GetMapping("/")
@Operation(summary = "인기 사μž₯λ‹˜ ν”Όλ“œ 전체 쑰회 API", description = "포슀트의 κΈ°λ³Έ λ””ν΄νŠΈ μ •λ ¬λ‘œ, μ’‹μ•„μš” 순으둜 λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•©λ‹ˆλ‹€.")
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성곡"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON404", description = "Fail, 이미 μ‘΄μž¬ν•˜λŠ” μ΄λ¦„μž…λ‹ˆλ‹€.")
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON404", description = "Fail, μ‹€νŒ¨")
})
public ApiResponse<PostResponseDto.PostListViewDto> posts(@RequestParam("like") boolean like) {
return ApiResponse.onSuccess((PostResponseDto.PostListViewDto) queryService.getPostsOrderByLike());
public ApiResponse<PostResponseDto.PostListViewDto> getPostByLikes() {
List<PostResponseDto> posts = postQueryService.getAllPostsSortedByLikes();
return ApiResponse.of(SuccessStatus.POST_GET_SUCCESS, new PostResponseDto.PostListViewDto(posts));
}

@GetMapping("/follow")
@GetMapping("/following/all")
@Operation(summary = "νŒ”λ‘œμž‰ 쀑인 ν”Όλ“œ 전체 쑰회 API")
public ApiResponse<PostResponseDto.PostListViewDto> following(@RequestParam("like") boolean like) {
return null;
}

@GetMapping("/{postId}")
@Operation(summary = "ν”Όλ“œ 상세 쑰회 API", description = "쑰회 ν•˜κ³ μž ν•˜λŠ” postId 값이 ν•„μš”ν•©λ‹ˆλ‹€.")
public ApiResponse<PostResponseDto.PostViewDto> post(@PathVariable Long postId) {
return null;
}

@PostMapping("/follow/{followingId}")
@Operation(summary = "νŒ”λ‘œμš° API", description = "νŒ”λ‘œμš° ν•˜λ €λŠ” μ‚¬λžŒμ˜ idλ₯Ό request둜 μ£Όμ‹œλ©΄ λ©λ‹ˆλ‹€.")
public ApiResponse<?> follow(Authentication authentication,
@PathVariable(name = "followingId") Long followingId){
@PathVariable(name = "followingId") Long followingId) {

Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
//νŒ”λ‘œμš° κΈ°λŠ₯
Expand All @@ -63,17 +68,12 @@ public ApiResponse<?> follow(Authentication authentication,
@PostMapping("/follow/{followingId}/delete")
@Operation(summary = "νŒ”λ‘œμš° μ·¨μ†Œ API", description = "νŒ”λ‘œμš° μ·¨μ†Œν•˜λ €λŠ” μ‚¬λžŒμ˜ idλ₯Ό request둜 μ£Όμ‹œλ©΄ λ©λ‹ˆλ‹€.")
public ApiResponse<?> deleteFollow(Authentication authentication,
@PathVariable(name = "followingId") Long followingId){
@PathVariable(name = "followingId") Long followingId) {

Long memberId = Long.valueOf(authentication.getName().toString());
//νŒ”λ‘œμš° μ·¨μ†Œ κΈ°λŠ₯
postCommandService.deleteFollow(followingId,memberId);
return ApiResponse.of(SuccessStatus.POST_DELETE_FOLLOW_SUCCESS,null);
}

@GetMapping("/{postId}")
public ApiResponse<PostResponseDto.PostViewDto> post(@PathVariable Long postId) {
return null;
postCommandService.deleteFollow(followingId, memberId);
return ApiResponse.of(SuccessStatus.POST_DELETE_FOLLOW_SUCCESS, null);
}

@PostMapping("/")
Expand All @@ -82,7 +82,7 @@ public ApiResponse<?> registerPost(@RequestPart(value = "content") String conten
@RequestPart(value = "postImgList", required = false) List<MultipartFile> postImgList,
Authentication authentication) {
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
postCommandService.registerPost(member,content,postImgList);
postCommandService.registerPost(member, content, postImgList);

return ApiResponse.of(SuccessStatus.POST_UPLOAD_SUCCESS, null);
}
Expand All @@ -94,58 +94,57 @@ public ApiResponse<PostResponseDto.PostStatusDto> updatePost(@PathVariable(name
@RequestPart(value = "postImgList", required = false) List<MultipartFile> postImgList,
Authentication authentication) {
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
postCommandService.updatePost(member,postId, content,postImgList);
postCommandService.updatePost(member, postId, content, postImgList);

return ApiResponse.of(SuccessStatus.POST_UPDATE_SUCCESS,null);
return ApiResponse.of(SuccessStatus.POST_UPDATE_SUCCESS, null);
}

@DeleteMapping("/{postId}")
@Operation(summary = "ν”Όλ“œ μ‚­μ œ API", description = "postIdλŠ” μˆ˜μ •ν•  ν”Όλ“œ id, content에 feed λ‚΄μš©, postImgList에 배열에 이미지λ₯Ό λ‹΄μ•„μ„œ λ„˜κ²¨μ£Όλ©΄ λ©λ‹ˆλ‹€.")
public ApiResponse<PostResponseDto.PostStatusDto> deletePost(@PathVariable(name = "postId") Long postId,
Authentication authentication) {
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
postCommandService.deletePost(member,postId);
postCommandService.deletePost(member, postId);

return ApiResponse.of(SuccessStatus.POST_DELETE_SUCCESS, null);
}

@PostMapping("/{postId}/likes")
@Operation(summary = "ν”Όλ“œ μ’‹μ•„μš” API", description = "postId: μ’‹μ•„μš” λˆ„λ₯Ό ν”Όλ“œ id, ν•œλ²ˆ λˆ„λ₯΄λ©΄ μ’‹μ•„μš” λ‘λ²ˆ λˆ„λ₯΄λ©΄ μ’‹μ•„μš” μ·¨μ†Œ")
public ApiResponse<?> likePost(@PathVariable(name = "postId") Long postId,
Authentication authentication){
Authentication authentication) {
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
postCommandService.likePost(member, postId);
return ApiResponse.of(SuccessStatus.POST_LIKE_SUCCESS,null);
return ApiResponse.of(SuccessStatus.POST_LIKE_SUCCESS, null);
}


@PostMapping("/comment/{commentId}/likes")
@Operation(summary = "λŒ“κΈ€ μ’‹μ•„μš” API", description = "commentId: μ’‹μ•„μš” λˆ„λ₯Ό λŒ“κΈ€ id, ν•œλ²ˆ λˆ„λ₯΄λ©΄ μ’‹μ•„μš” λ‘λ²ˆ λˆ„λ₯΄λ©΄ μ’‹μ•„μš” μ·¨μ†Œ")
public ApiResponse<?> likeComment(@PathVariable(name = "commentId") Long commentId,
Authentication authentication){
Authentication authentication) {
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
postCommandService.likeComment(member, commentId);
return ApiResponse.of(SuccessStatus.POST_COMMENT_LIKE_SUCCESS, null);
}



@PostMapping("/{postId}/comment")
@Operation(summary = "ν”Όλ“œ λŒ“κΈ€ 등둝 API", description = "parentId: λŒ“κΈ€μΈ 경우 null λŒ€λŒ“κΈ€μ΄λΌλ©΄ μž‘μ„±ν•˜λ €λŠ” λŒ“κΈ€ id," +
"postId: λŒ“κΈ€ 등둝할 ν”Όλ“œ, content: λŒ“κΈ€ λ‚΄μš© ")
"postId: λŒ“κΈ€ 등둝할 ν”Όλ“œ, content: λŒ“κΈ€ λ‚΄μš© ")
public ApiResponse<?> uploadComment(@PathVariable(name = "postId") Long postId,
Authentication authentication,
@RequestBody PostRequestDto.CommentDTO request){
@RequestBody PostRequestDto.CommentDTO request) {
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
postCommandService.uploadComment(member,postId, request);
postCommandService.uploadComment(member, postId, request);

return ApiResponse.of(SuccessStatus.POST_COMMENT_SUCCESS,null);
return ApiResponse.of(SuccessStatus.POST_COMMENT_SUCCESS, null);
}

@PutMapping("/{postId}/comment/{commentId}")
@Operation(summary = "ν”Όλ“œ λŒ“κΈ€ μˆ˜μ • API", description = "postId: λŒ“κΈ€ μˆ˜μ •ν•  ν”Όλ“œ, commentId : μˆ˜μ •ν•  λŒ“κΈ€, content: λŒ“κΈ€ λ‚΄μš©")
public ApiResponse<?> updateComment(@PathVariable(name ="postId") Long postId,
@PathVariable(name ="commentId") Long commentId,
public ApiResponse<?> updateComment(@PathVariable(name = "postId") Long postId,
@PathVariable(name = "commentId") Long commentId,
@RequestBody PostRequestDto.UpdateCommentDTO requet,
Authentication authentication) {
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
Expand All @@ -156,6 +155,4 @@ public ApiResponse<?> updateComment(@PathVariable(name ="postId") Long postId,
}




}
Loading
Loading