Skip to content

Commit 535cf2b

Browse files
committed
get comments by task id
1 parent 708025e commit 535cf2b

File tree

5 files changed

+84
-38
lines changed

5 files changed

+84
-38
lines changed

backend/app/controllers/CommentController.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ class CommentController @Inject()(
5858
}
5959
}
6060

61-
// def getCommentsByTaskId(taskId: Int): Action[AnyContent] =
62-
// authenticatedActionWithUser.async { request =>
63-
// val userId = request.userToken.userId
64-
// commentService.getCommentsByTaskId(taskId, userId).map { comments =>
65-
// Ok(
66-
// Json.toJson(
67-
// ApiResponse(
68-
// s"Comments retrieved successfully for task ID: $taskId",
69-
// comments
70-
// )
71-
// )
72-
// )
73-
// }
74-
// }
61+
def getCommentsByTaskId(taskId: Int): Action[AnyContent] =
62+
authenticatedActionWithUser.async { request =>
63+
val userId = request.userToken.userId
64+
commentService.getCommentsByTaskId(taskId, userId).map { comments =>
65+
Ok(
66+
Json.toJson(
67+
ApiResponse.success(
68+
s"Comments retrieved successfully for task ID: $taskId",
69+
comments
70+
)
71+
)
72+
)
73+
}
74+
}
7575

7676
def delete(commentId: Int): Action[AnyContent] =
7777
authenticatedActionWithUser.async { request =>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dto.response.comment
2+
3+
import dto.request.comment.CommentChunk
4+
import play.api.libs.json.{JsValue, Json, OFormat}
5+
6+
import java.time.Instant
7+
8+
case class CommentResponse(commentId: Int,
9+
userId: Int,
10+
name: String,
11+
taskId: Int,
12+
content: JsValue,
13+
createdAt: Instant,
14+
updatedAt: Instant)
15+
16+
object CommentResponse {
17+
implicit val format: OFormat[CommentResponse] = Json.format[CommentResponse]
18+
}

backend/app/repositories/CommentRepository.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package repositories
22

3+
import dto.request.comment.CommentChunk
4+
import dto.response.comment.CommentResponse
35
import models.entities.TaskComment
4-
import models.tables.TableRegistry.taskComments
6+
import models.tables.TableRegistry.{taskComments, users}
57
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
68
import slick.jdbc.JdbcProfile
79

@@ -28,4 +30,26 @@ class CommentRepository @Inject()(
2830
def deleteById(commentId: Int): DBIO[Int] = {
2931
taskComments.filter(_.id === commentId).delete
3032
}
33+
34+
def findByTaskId(taskId: Int): DBIO[Seq[CommentResponse]] = {
35+
val query = taskComments
36+
.filter(_.taskId === taskId)
37+
.join(users).on(_.userId === _.id)
38+
.sortBy { case (c, _) => c.updatedAt.desc }
39+
40+
query.result.map { rows =>
41+
rows.map { case (c, u) =>
42+
CommentResponse(
43+
commentId = c.id.get,
44+
userId = c.userId,
45+
name = u.name,
46+
taskId = taskId,
47+
content = c.content,
48+
createdAt = c.createdAt,
49+
updatedAt = c.updatedAt
50+
)
51+
}
52+
}
53+
}
54+
3155
}

backend/app/services/CommentService.scala

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package services
22

33
import dto.request.comment.CreateUpdateCommentRequest
4+
import dto.response.comment.CommentResponse
45
import exception.AppException
56
import models.entities.TaskComment
67
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
@@ -9,6 +10,7 @@ import play.api.libs.json.Json
910
import repositories.{CommentRepository, TaskRepository}
1011
import slick.jdbc.JdbcProfile
1112

13+
import java.time.Instant
1214
import javax.inject.{Inject, Singleton}
1315
import scala.concurrent.{ExecutionContext, Future}
1416

@@ -78,35 +80,37 @@ class CommentService @Inject()(
7880
DBIO.successful(())
7981
}
8082
updatedComment = existingComment.copy(
81-
content = Json.toJson(request.content)
83+
content = Json.toJson(request.content),
84+
updatedAt = Instant.now()
8285
)
8386
updateCount <- commentRepository.update(updatedComment)
8487
} yield updateCount
8588

8689
db.run(action.transactionally)
8790
}
8891

89-
// def getCommentsByTaskId(taskId: Int, userId: Int) = {
90-
// val action = for {
91-
// taskOpt <- taskRepository.findTaskAndProjectIdIfUserInProject(
92-
// taskId,
93-
// userId
94-
// )
95-
// _ <- taskOpt match {
96-
// case Some((_, _)) =>
97-
// DBIO.successful(())
98-
// case _ =>
99-
// DBIO.failed(
100-
// AppException(
101-
// message = s"Task with ID $taskId does not exist or you do not have access.",
102-
// statusCode = Status.NOT_FOUND
103-
// )
104-
// )
105-
// }
106-
//
107-
//
108-
// } yield
109-
// }
92+
def getCommentsByTaskId(taskId: Int, userId: Int): Future[Seq[CommentResponse]] = {
93+
val action = for {
94+
taskOpt <- taskRepository.findTaskAndProjectIdIfUserInProject(
95+
taskId,
96+
userId
97+
)
98+
_ <- taskOpt match {
99+
case Some((_, _)) =>
100+
DBIO.successful(())
101+
case _ =>
102+
DBIO.failed(
103+
AppException(
104+
message = s"Task with ID $taskId does not exist or you do not have access.",
105+
statusCode = Status.NOT_FOUND
106+
)
107+
)
108+
}
109+
comments <- commentRepository.findByTaskId(taskId)
110+
} yield comments
111+
112+
db.run(action.transactionally)
113+
}
110114

111115
def deleteComment(commentId: Int, userId: Int): Future[Int] = {
112116
val action = for {

backend/conf/routes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ PUT /api/user/profile controllers.UserProfileController.updateP
7878

7979
# Task Comment routes
8080
POST /api/tasks/:taskId/comments controllers.CommentController.create(taskId: Int)
81-
# GET /api/tasks/:taskId/comments controllers.CommentController.getCommentsByTaskId(taskId: Int)
81+
GET /api/tasks/:taskId/comments controllers.CommentController.getCommentsByTaskId(taskId: Int)
8282
DELETE /api/comments/:commentId controllers.CommentController.delete(commentId: Int)
8383
PUT /api/comments/:commentId controllers.CommentController.update(commentId: Int)
8484

0 commit comments

Comments
 (0)