Skip to content

Commit

Permalink
Merge pull request #4 from PoolC/dev
Browse files Browse the repository at this point in the history
Dev: Notification, Like, Conversation 기능 보완 및 세부 기능 추가
  • Loading branch information
jimmy0006 authored Jul 31, 2024
2 parents 30ca961 + 128f470 commit 6912726
Show file tree
Hide file tree
Showing 43 changed files with 401 additions and 386 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ compiler(`⌘,` > `Build, Execution, Deployment` > `Compiler` > `Java Compiler`
### Deploy

TODO with docker


Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@
@RequiredArgsConstructor
@RequestMapping("/comment")
public class CommentController {
private final PostService postService;
private final CommentService commentService;
private final LikeService likeService;

@PostMapping
public ResponseEntity<CommentResponse> createComment(@AuthenticationPrincipal Member member,
public ResponseEntity<Void> createComment(@AuthenticationPrincipal Member member,
@RequestBody @Valid CommentCreateRequest request) {
Post post = postService.findPostById(member, request.getPostId());
CommentCreateValues values = new CommentCreateValues(post, member, request);
Comment newComment = commentService.createComment(values);
return ResponseEntity.status(HttpStatus.CREATED).body(CommentResponse.of(newComment));
commentService.createComment(member, request);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PutMapping("/{commentId}")
public ResponseEntity<Void> updateComment(@AuthenticationPrincipal Member member,
@PathVariable Long commentId,
@RequestBody CommentUpdateRequest request) {
commentService.updateComment(member, commentId, new CommentUpdateValues(request));
commentService.updateComment(member, commentId, request);
return ResponseEntity.status(HttpStatus.OK).build();
}

Expand All @@ -54,9 +51,7 @@ public ResponseEntity<Void> deleteComment(@AuthenticationPrincipal Member member

@PostMapping("/{commentId}/like")
public ResponseEntity<Void> likeComment(@AuthenticationPrincipal Member member, @PathVariable Long commentId) {
Comment comment = commentService.findById(commentId);
if (!member.equals(comment.getMember())) likeService.like(member.getLoginID(), Subject.COMMENT, commentId);
else throw new IllegalArgumentException("본인의 댓글은 좋아할 수 없습니다.");
likeService.like(member, Subject.COMMENT, commentId);
return ResponseEntity.status(HttpStatus.OK).build();
}
}
6 changes: 1 addition & 5 deletions src/main/java/org/poolc/api/comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ public Comment(Comment parent, CommentCreateValues values) {
this.body = values.getBody();
this.isDeleted = false;
this.parent = parent;
if (parent != null) {
this.isChild = true;
} else {
this.isChild = false;
}
this.isChild = parent != null;
this.children = new ArrayList<>();
this.likeCount = 0L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

@Getter
public class CommentUpdateRequest {
private Boolean anonymous;
private String body;
private final Boolean anonymous;
private final String body;

public CommentUpdateRequest(Boolean anonymous, String body) {
this.anonymous = anonymous;
Expand Down
74 changes: 59 additions & 15 deletions src/main/java/org/poolc/api/comment/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.poolc.api.comment.service;

import org.poolc.api.comment.dto.CommentCreateRequest;
import org.poolc.api.comment.dto.CommentResponse;
import org.poolc.api.comment.dto.CommentUpdateRequest;
import org.poolc.api.post.service.PostService;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import org.poolc.api.auth.exception.UnauthorizedException;
import org.poolc.api.badge.service.BadgeConditionService;
Expand All @@ -24,51 +29,56 @@ public class CommentService {
//좋아요 수에 따라 뱃지 자동지급을 위함
private final BadgeConditionService badgeConditionService;
private final NotificationService notificationService;
public Comment createComment(CommentCreateValues values) {
Comment parent = null;
if (values.getParentId() != null && values.getParentId() != 0) parent = findById(values.getParentId());
if (parent != null && parent.getIsChild()) {
throw new IllegalArgumentException("대댓글에 대댓글을 달 수 없습니다.");
} else {
Comment comment = new Comment(parent, values);
commentRepository.save(comment);
comment.getPost().addCommentCount();
if (parent == null) notificationService.createCommentNotification(values.getMember().getLoginID(), values.getPost().getMember().getLoginID(), values.getPost().getId());
else notificationService.createRecommentNotification(values.getMember().getLoginID(), parent.getMember().getLoginID(), values.getPost().getId(), parent.getId());
return comment;
}
private final PostService postService;

@Transactional
public void createComment(Member member, CommentCreateRequest request) {
Post post = postService.findById(member, request.getPostId());
CommentCreateValues values = new CommentCreateValues(post, member, request);

Comment parent = findParentComment(values.getParentId());
validateParentComment(parent);

Comment comment = saveComment(values, parent);
sendNotifications(values, parent);
}

@Transactional(readOnly = true)
public List<Comment> findCommentsByPost(Post post) {
return commentRepository.findAllByPost(post).stream()
.sorted(Comparator.comparing(Comment::getCreatedAt))
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<Comment> findCommentsByParent(Long parentId) {
Comment parent = commentRepository.findById(parentId)
.orElseThrow(() -> new NoSuchElementException("No comment found with given comment id."));
return commentRepository.findAllByParent(parent);
}

@Transactional(readOnly = true)
public Comment findById(Long commentId) {
return commentRepository.findById(commentId)
.orElseThrow(() -> new NoSuchElementException("No comment found with given comment id."));
}

@Transactional
public void deleteComment(Member member, Long commentId) {
Comment comment = findById(commentId);
checkWriterOrAdmin(member, comment);
comment.setIsDeleted();
comment.getPost().deductCommentCount();
}

public void updateComment(Member member, Long commentId, CommentUpdateValues commentUpdateValues) {
@Transactional
public void updateComment(Member member, Long commentId, CommentUpdateRequest request) {
Comment comment = findById(commentId);
checkWriter(member, comment);
comment.updateComment(commentUpdateValues);
comment.updateComment(new CommentUpdateValues(request));
}

@Transactional
public void likeComment(Member member, Long commentId) {
Comment comment = findById(commentId);
checkNotWriter(member, comment);
Expand All @@ -78,6 +88,7 @@ public void likeComment(Member member, Long commentId) {
}
}

@Transactional
public void dislikeComment(Member member, Long commentId) {
Comment comment = findById(commentId);
checkNotWriter(member, comment);
Expand All @@ -87,6 +98,39 @@ public void dislikeComment(Member member, Long commentId) {
}
}

// TODO
private Comment findParentComment(Long parentId) {
if (parentId != null && parentId != 0) {
return findById(parentId);
}
return null;
}

private void validateParentComment(Comment parent) {
if (parent != null && parent.getIsChild()) {
throw new IllegalArgumentException("대댓글에 대댓글을 달 수 없습니다.");
}
}

private Comment saveComment(CommentCreateValues values, Comment parent) {
Comment comment = new Comment(parent, values);
commentRepository.save(comment);
comment.getPost().addCommentCount();
return comment;
}

private void sendNotifications(CommentCreateValues values, Comment parent) {
String commenterID = values.getMember().getLoginID();
String postWriterID = values.getPost().getMember().getLoginID();

if (parent == null && !commenterID.equals(postWriterID)) {
notificationService.createCommentNotification(commenterID, postWriterID, values.getPost().getId());
} else if (parent != null && !parent.getMember().equals(values.getMember())) {
String parentCommenterID = parent.getMember().getLoginID();
notificationService.createRecommentNotification(commenterID, parentCommenterID, values.getPost().getId(), parent.getId());
}
}

private void checkWriterOrAdmin(Member member, Comment comment) {
if (!comment.getMember().equals(member) && !member.isAdmin()) throw new UnauthorizedException("접근할 수 없습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.poolc.api.comment.vo;

import lombok.Getter;
import org.poolc.api.comment.domain.Comment;
import org.poolc.api.comment.dto.CommentCreateRequest;
import org.poolc.api.member.domain.Member;
import org.poolc.api.post.domain.Post;
Expand All @@ -21,4 +20,4 @@ public CommentCreateValues(Post post, Member member, CommentCreateRequest reques
this.body = request.getBody();
this.parentId = request.getParentId();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public CommentUpdateValues(CommentUpdateRequest commentUpdateRequest) {
this.anonymous = commentUpdateRequest.getAnonymous();
this.body = commentUpdateRequest.getBody();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.poolc.api.conversation.domain.Conversation;
import org.poolc.api.conversation.dto.ConversationCreateRequest;
import org.poolc.api.conversation.dto.ConversationResponse;
import org.poolc.api.conversation.service.ConversationService;
import org.poolc.api.member.domain.Member;
import org.poolc.api.message.dto.MessageResponse;
Expand All @@ -23,46 +24,33 @@ public class ConversationController {
private final ConversationService conversationService;
private final MessageService messageService;

// get all conversations
@GetMapping("/all")
public ResponseEntity<List<ConversationResponse>> getAllConversations(@AuthenticationPrincipal Member member) {
return ResponseEntity.status(HttpStatus.OK).body(conversationService.findAllConversationsForLoginID(member.getLoginID()));
}

// create conversation
@PostMapping("/new")
public ResponseEntity<String> createConversation(@AuthenticationPrincipal Member member,
public ResponseEntity<ConversationResponse> createConversation(@AuthenticationPrincipal Member member,
@RequestBody ConversationCreateRequest request) {
String conversationId = conversationService.createConversation(
conversationService.convertToConversationCreateValues(request)
);
return ResponseEntity.status(HttpStatus.CREATED).body(conversationId);
ConversationResponse response = conversationService.createConversation(member.getLoginID(), request);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

// get conversation
@GetMapping("/{conversationId}")
public ResponseEntity<List<MessageResponse>> viewConversation(@AuthenticationPrincipal Member member,
@PathVariable String conversationId) {

Conversation conversation = conversationService.findConversationById(conversationId, member.getLoginID());
List<MessageResponse> responses= messageService.findMessagesByConversationId(member, conversationId)
.stream()
.map(MessageResponse::of)
.collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.OK).body(responses);
return ResponseEntity.status(HttpStatus.OK).body(messageService.findMessageResponsesByConversationId(member, conversationId));
}

// delete conversation
@DeleteMapping("/{conversationId}")
public ResponseEntity<Void> deleteConversation(@AuthenticationPrincipal Member member,
@PathVariable String conversationId) {
conversationService.deleteConversation(conversationId, member.getLoginID());

messageService.findMessagesByConversationId(member, conversationId)
.stream()
.map(message -> {
if (message.getConversation().getStarterLoginID().equals(member.getLoginID())) {
messageService.deleteMessageByStarter(member, message.getId());
} else {
messageService.deleteMessageByOther(member, message.getId());
}
return null;
});

conversationService.deleteConversation(member.getLoginID(), conversationId);
return ResponseEntity.status(HttpStatus.OK).build();
}
}
25 changes: 11 additions & 14 deletions src/main/java/org/poolc/api/conversation/domain/Conversation.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.poolc.api.conversation.domain;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.poolc.api.common.domain.TimestampEntity;
import org.poolc.api.conversation.vo.ConversationCreateValues;

import javax.persistence.*;
import org.poolc.api.message.domain.Message;

@Entity
@Getter
@Table(name = "conversations")
@Table(name = "conversation")
public class Conversation extends TimestampEntity {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
@Column(name = "conversation_id", columnDefinition = "CHAR(32)")
@Column(name = "id", columnDefinition = "CHAR(32)")
private String id;

@Column(name = "starter_login_id", nullable = false)
Expand All @@ -23,12 +25,6 @@ public class Conversation extends TimestampEntity {
@Column(name = "other_login_id", nullable = false)
private String otherLoginID;

@Column(name = "starter_name", nullable = false)
private String starterName;

@Column(name = "other_name", nullable = false)
private String otherName;

@Column(name = "starter_anonymous", nullable = false)
private boolean starterAnonymous;

Expand All @@ -41,31 +37,32 @@ public class Conversation extends TimestampEntity {
@Column(name = "other_deleted")
private boolean otherDeleted = false;

@Setter
@OneToOne
private Message lastMessage;

protected Conversation() {}

public Conversation(String starterLoginID, String otherLoginID, String starterName, String otherName, boolean starterAnonymous, boolean otherAnonymous) {
public Conversation(String starterLoginID, String otherLoginID, boolean starterAnonymous, boolean otherAnonymous) {
this.starterLoginID = starterLoginID;
this.otherLoginID = otherLoginID;
this.starterName = starterName;
this.otherName = otherName;
this.starterAnonymous = starterAnonymous;
this.otherAnonymous = otherAnonymous;
}

public Conversation(ConversationCreateValues values) {
this.starterLoginID = values.getStarterLoginID();
this.otherLoginID = values.getOtherLoginID();
this.starterName = values.getStarterName();
this.otherName = values.getOtherName();
this.starterAnonymous = values.isStarterAnonymous();
this.otherAnonymous = values.isOtherAnonymous();
}

public void setSenderDeleted() {
public void setStarterDeleted() {
this.starterDeleted = true;
}

public void setReceiverDeleted() {
this.otherDeleted = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
@Getter
@Builder
public class ConversationCreateRequest {
private String starterLoginID;
private String otherLoginID;
private boolean starterAnonymous;
private boolean otherAnonymous;

protected ConversationCreateRequest() {}
public ConversationCreateRequest(String starterLoginID, String otherLoginID, boolean starterAnonymous, boolean otherAnonymous) {
this.starterLoginID = starterLoginID;
public ConversationCreateRequest(String otherLoginID, boolean starterAnonymous, boolean otherAnonymous) {
this.otherLoginID = otherLoginID;
this.starterAnonymous = starterAnonymous;
this.otherAnonymous = otherAnonymous;
Expand Down
Loading

0 comments on commit 6912726

Please sign in to comment.