-
Notifications
You must be signed in to change notification settings - Fork 1
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 #674 from bounswe/main
Deploying the comment feature
- Loading branch information
Showing
14 changed files
with
681 additions
and
201 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/backend/src/main/java/com/app/gamereview/controller/CommentController.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,61 @@ | ||
package com.app.gamereview.controller; | ||
|
||
import com.app.gamereview.dto.request.comment.CreateCommentRequestDto; | ||
import com.app.gamereview.dto.request.comment.EditCommentRequestDto; | ||
import com.app.gamereview.dto.request.comment.ReplyCommentRequestDto; | ||
import com.app.gamereview.model.Comment; | ||
import com.app.gamereview.model.User; | ||
import com.app.gamereview.service.CommentService; | ||
import com.app.gamereview.util.validation.annotation.AuthorizationRequired; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.Valid; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/comment") | ||
@Validated | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@Autowired | ||
public CommentController(CommentService commentService) { | ||
this.commentService = commentService; | ||
} | ||
|
||
|
||
@AuthorizationRequired | ||
@PostMapping("/create") | ||
public ResponseEntity<Comment> createComment(@Valid @RequestBody CreateCommentRequestDto comment, @RequestHeader String Authorization, HttpServletRequest request) { | ||
User user = (User) request.getAttribute("authenticatedUser"); | ||
Comment commentToCreate = commentService.createComment(comment, user); | ||
return ResponseEntity.ok(commentToCreate); | ||
} | ||
|
||
@AuthorizationRequired | ||
@PostMapping("/reply") | ||
public ResponseEntity<Comment> createReply(@Valid @RequestBody ReplyCommentRequestDto reply, @RequestHeader String Authorization, HttpServletRequest request) { | ||
User user = (User) request.getAttribute("authenticatedUser"); | ||
Comment commentToCreate = commentService.replyComment(reply, user); | ||
return ResponseEntity.ok(commentToCreate); | ||
} | ||
|
||
@AuthorizationRequired | ||
@PostMapping("/edit") | ||
public ResponseEntity<Comment> editComment(@RequestParam String id, @Valid @RequestBody EditCommentRequestDto comment, @RequestHeader String Authorization, HttpServletRequest request) { | ||
User user = (User) request.getAttribute("authenticatedUser"); | ||
Comment editedComment = commentService.editComment(id, comment, user); | ||
return ResponseEntity.ok(editedComment); | ||
} | ||
|
||
@AuthorizationRequired | ||
@DeleteMapping("/delete") | ||
public ResponseEntity<Comment> deleteComment(@RequestParam String id, @RequestHeader String Authorization, HttpServletRequest request) { | ||
User user = (User) request.getAttribute("authenticatedUser"); | ||
Comment deletedComment = commentService.deleteComment(id, user); | ||
return ResponseEntity.ok(deletedComment); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...backend/src/main/java/com/app/gamereview/dto/request/comment/CreateCommentRequestDto.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,25 @@ | ||
package com.app.gamereview.dto.request.comment; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreateCommentRequestDto { | ||
|
||
private String commentContent; | ||
|
||
@NotNull(message = "Post Id cannot be null or empty.") | ||
@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", | ||
message = "Post has invalid Id (UUID) format") | ||
private String post; | ||
|
||
|
||
// TODO annotations | ||
// TODO achievements | ||
} |
14 changes: 14 additions & 0 deletions
14
app/backend/src/main/java/com/app/gamereview/dto/request/comment/EditCommentRequestDto.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,14 @@ | ||
package com.app.gamereview.dto.request.comment; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class EditCommentRequestDto { | ||
|
||
private String commentContent; | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
app/backend/src/main/java/com/app/gamereview/dto/request/comment/ReplyCommentRequestDto.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,25 @@ | ||
package com.app.gamereview.dto.request.comment; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ReplyCommentRequestDto { | ||
|
||
private String commentContent; | ||
|
||
|
||
@NotNull(message = "Parent Comment Id cannot be null or empty.") | ||
@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", | ||
message = "Comment has invalid Id (UUID) format") | ||
private String parentComment; | ||
|
||
// TODO annotations | ||
// TODO achievements | ||
} |
35 changes: 35 additions & 0 deletions
35
...ackend/src/main/java/com/app/gamereview/dto/response/comment/CommentReplyResponseDto.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,35 @@ | ||
package com.app.gamereview.dto.response.comment; | ||
|
||
import com.app.gamereview.model.User; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CommentReplyResponseDto { | ||
|
||
private String id; | ||
|
||
private String commentContent; | ||
|
||
private User commenter; | ||
|
||
private String post; | ||
|
||
private LocalDateTime lastEditedAt; | ||
|
||
private LocalDateTime createdAt; | ||
|
||
private Boolean isEdited; | ||
|
||
private int overallVote; // overallVote = # of upvote - # of downvote | ||
|
||
private int voteCount; // voteCount = # of upvote + # of downvote | ||
|
||
// TODO reports | ||
// TODO annotations | ||
} |
38 changes: 38 additions & 0 deletions
38
...end/src/main/java/com/app/gamereview/dto/response/comment/GetPostCommentsResponseDto.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,38 @@ | ||
package com.app.gamereview.dto.response.comment; | ||
|
||
import com.app.gamereview.model.User; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class GetPostCommentsResponseDto { | ||
|
||
private String id; | ||
|
||
private String commentContent; | ||
|
||
private User commenter; | ||
|
||
private String post; | ||
|
||
private LocalDateTime lastEditedAt; | ||
|
||
private LocalDateTime createdAt; | ||
|
||
private Boolean isEdited; | ||
|
||
private int overallVote; // overallVote = # of upvote - # of downvote | ||
|
||
private int voteCount; // voteCount = # of upvote + # of downvote | ||
|
||
private ArrayList<CommentReplyResponseDto> replies; | ||
|
||
// TODO reports | ||
// TODO annotations | ||
} |
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
58 changes: 58 additions & 0 deletions
58
app/backend/src/main/java/com/app/gamereview/model/Comment.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,58 @@ | ||
package com.app.gamereview.model; | ||
|
||
import com.app.gamereview.enums.VoteChoice; | ||
import com.app.gamereview.model.common.BaseModel; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Document(collection = "Comment") | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Comment extends BaseModel { | ||
|
||
private String commentContent; | ||
|
||
private String commenter; | ||
|
||
@NotNull | ||
private String post; | ||
|
||
private String parentComment; | ||
|
||
private LocalDateTime lastEditedAt; | ||
|
||
private int overallVote; // overallVote = # of upvote - # of downvote | ||
|
||
private int voteCount; // voteCount = # of upvote + # of downvote | ||
|
||
// TODO reports | ||
// TODO annotations | ||
|
||
public void addVote(VoteChoice choice) { | ||
voteCount += 1; | ||
if (choice.name().equals("UPVOTE")) { | ||
overallVote += 1; | ||
} else if (choice.name().equals("DOWNVOTE")) { | ||
overallVote -= 1; | ||
} | ||
} | ||
|
||
public void deleteVote(VoteChoice choice) { | ||
voteCount -= 1; | ||
if (choice.name().equals("UPVOTE")) { | ||
overallVote -= 1; | ||
} else if (choice.name().equals("DOWNVOTE")) { | ||
overallVote += 1; | ||
} | ||
} | ||
|
||
|
||
} |
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
12 changes: 12 additions & 0 deletions
12
app/backend/src/main/java/com/app/gamereview/repository/CommentRepository.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,12 @@ | ||
package com.app.gamereview.repository; | ||
|
||
import com.app.gamereview.model.Comment; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface CommentRepository extends MongoRepository<Comment, String> { | ||
List<Comment> findByPost(String postId); | ||
|
||
int countByPost(String postId); | ||
} |
Oops, something went wrong.