File tree Expand file tree Collapse file tree 4 files changed +43
-1
lines changed
Expand file tree Collapse file tree 4 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -73,5 +73,15 @@ class CommentController @Inject()(
7373// }
7474// }
7575
76+ def delete (commentId : Int ): Action [AnyContent ] =
77+ authenticatedActionWithUser.async { request =>
78+ val deletedBy = request.userToken.userId
79+ commentService
80+ .deleteComment(commentId, deletedBy)
81+ .map { _ =>
82+ Ok (Json .toJson(ApiResponse [Unit ](s " Comment deleted successfully " )))
83+ }
84+ }
85+
7686
7787}
Original file line number Diff line number Diff line change @@ -25,4 +25,7 @@ class CommentRepository @Inject()(
2525 taskComments.filter(_.id === commentId).result.headOption
2626 }
2727
28+ def deleteById (commentId : Int ): DBIO [Int ] = {
29+ taskComments.filter(_.id === commentId).delete
30+ }
2831}
Original file line number Diff line number Diff line change @@ -108,6 +108,35 @@ class CommentService @Inject()(
108108// } yield
109109// }
110110
111+ def deleteComment (commentId : Int , userId : Int ): Future [Int ] = {
112+ val action = for {
113+ existingCommentOpt <- commentRepository.findById(commentId)
114+ existingComment <- existingCommentOpt match {
115+ case Some (comment) =>
116+ DBIO .successful(comment)
117+ case None =>
118+ DBIO .failed(
119+ AppException (
120+ message = s " Comment with ID $commentId does not exist. " ,
121+ statusCode = Status .NOT_FOUND
122+ )
123+ )
124+ }
125+ _ <- if (existingComment.userId != userId) {
126+ DBIO .failed(
127+ AppException (
128+ message = s " You do not have permission to delete this comment. " ,
129+ statusCode = Status .FORBIDDEN
130+ )
131+ )
132+ } else {
133+ DBIO .successful(())
134+ }
135+ deletedCount <- commentRepository.deleteById(commentId)
136+ } yield deletedCount
137+ db.run(action.transactionally)
138+ }
139+
111140
112141
113142}
Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ PUT /api/user/profile controllers.UserProfileController.updateP
7979# Task Comment routes
8080POST /api/tasks/:taskId/comments controllers.CommentController.create(taskId: Int)
8181# GET /api/tasks/:taskId/comments controllers.CommentController.getCommentsByTaskId(taskId: Int)
82- # DELETE /api/comments/:commentId controllers.CommentController.deleteComment (commentId: Int)
82+ DELETE /api/comments/:commentId controllers.CommentController.delete (commentId: Int)
8383PUT /api/comments/:commentId controllers.CommentController.update(commentId: Int)
8484
8585#Web socket
You can’t perform that action at this time.
0 commit comments