Skip to content

Commit 708025e

Browse files
committed
delete
1 parent 9595de0 commit 708025e

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

backend/app/controllers/CommentController.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

backend/app/repositories/CommentRepository.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

backend/app/services/CommentService.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

backend/conf/routes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ PUT /api/user/profile controllers.UserProfileController.updateP
7979
# Task Comment routes
8080
POST /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)
8383
PUT /api/comments/:commentId controllers.CommentController.update(commentId: Int)
8484

8585
#Web socket

0 commit comments

Comments
 (0)