Skip to content

Commit

Permalink
[J2KB-3rd-Season#6]feat: 게시글 Like 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent d905039 commit efef233
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.devin.dev.controller.reply.ReplyOrderCondition;
import com.devin.dev.dto.post.PostDetailsDto;
import com.devin.dev.model.DefaultResponse;
import com.devin.dev.security.JwtAuthTokenProvider;
import com.devin.dev.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -52,6 +51,12 @@ public DefaultResponse<PostDetailsDto> deletePost(@PathVariable("id") Long postI

@PostMapping("/post")
public DefaultResponse<?> post(@RequestBody PostForm form, HttpServletRequest request) {
return postService.post(request, form);
return postService.post(form, request);
}

@PatchMapping("/post/{id}/like")
public DefaultResponse<?> changePostLike(@PathVariable("id") Long postId, HttpServletRequest request) {
return postService.changePostLike(postId, request);
}

}
17 changes: 16 additions & 1 deletion src/main/java/com/devin/dev/entity/post/PostLike.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.devin.dev.entity.post;

import com.devin.dev.entity.base.Created;
import com.devin.dev.entity.user.User;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
@Getter
public class PostLike extends Created {

Expand All @@ -21,4 +23,17 @@ public class PostLike extends Created {
@JoinColumn(name = "post_id")
private Post post;

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

public void changePost(Post post) {
if(this.post != null) {
this.post.getLikes().remove(this);
} else {
this.post = post;
post.getLikes().add(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.devin.dev.repository.post;

import com.devin.dev.entity.post.Post;
import com.devin.dev.entity.post.PostLike;
import com.devin.dev.entity.user.User;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

Optional<PostLike> findByPostAndUser(Post post, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public Optional<PostDetailsDto> findPostDetailsById(Long id, ReplyOrderCondition
Post result = queryFactory
.select(post)
.from(post)
.innerJoin(post.replies, reply).fetchJoin()
.innerJoin(post.user, user).fetchJoin()
.innerJoin(reply.user, user).fetchJoin()
.leftJoin(post.replies, reply).fetchJoin()
.leftJoin(post.user, user).fetchJoin()
.leftJoin(reply.user, user).fetchJoin()
.where(post.id.eq(id))
.orderBy(
replyOrder(condition)
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/devin/dev/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,60 @@ public DefaultResponse<PostDetailsDto> deletePost(Long postId) {

return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.DELETED_POST);
}

public DefaultResponse<?> changePostLike(Long postId, HttpServletRequest request) {
String token = tokenProvider.parseToken(request);
Long userId;
if (tokenProvider.validateToken(token)) {
userId = tokenProvider.getUserId(token);
} else {
return new DefaultResponse<>(StatusCode.FAIL_AUTH, ResponseMessage.NOT_FOUND_USER);
}
Optional<User> userOptional = userRepository.findById(userId);
if (userOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_USER);
}
User user = userOptional.get();

Optional<Post> postOptional = postRepository.findById(postId);
if (postOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_POST);
}
Post post = postOptional.get();

Optional<PostLike> likeOptional = postLikeRepository.findByPostAndUser(post, user);

PostLike postLike;
// 추천 유무에 따라 실행
if (likeOptional.isPresent()) {
postLike = likeOptional.get();
postLikeRepository.delete(postLike);
if (isNotSameUser(user, post.getUser())) {
// 좋아요 누른 사람 경험치 감소
user.changeExp(User.ExpChangeType.REPLY_CANCEL_LIKE);
// 답변 작성자 경험치 감소
post.getUser().changeExp(User.ExpChangeType.REPLY_NOT_BE_LIKED);
}
} else {
postLike = post.like(user, new PostLike());
if (isNotSameUser(user, post.getUser())) {
// 좋아요 누른 사람 경험치 증가
user.changeExp(User.ExpChangeType.REPLY_LIKE);
// 답변 작성자 경험치 증가
post.getUser().changeExp(User.ExpChangeType.REPLY_BE_LIKED);
}
postLikeRepository.save(postLike);
}

ReplyLikeDto replyLikeDto = new ReplyLikeDto(postLike.getId(), user.getName());

return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_LIKE_CHANGE_SUCCESS, replyLikeDto);

}

private boolean isNotSameUser(User firstUser, User secondUser) {
return !firstUser.getId().equals(secondUser.getId());
}
}


0 comments on commit efef233

Please sign in to comment.