|
| 1 | +package controllers |
| 2 | + |
| 3 | +import dto.request.comment.CreateUpdateCommentRequest |
| 4 | +import dto.response.ApiResponse |
| 5 | +import play.api.i18n.I18nSupport.RequestWithMessagesApi |
| 6 | +import play.api.i18n.Messages |
| 7 | +import play.api.libs.json.Format.GenericFormat |
| 8 | +import play.api.libs.json.{JsValue, Json} |
| 9 | +import play.api.mvc.{Action, AnyContent, MessagesAbstractController, MessagesControllerComponents} |
| 10 | +import services.CommentService |
| 11 | +import utils.WritesExtras.unitWrites |
| 12 | +import validations.ValidationHandler |
| 13 | + |
| 14 | +import javax.inject.Inject |
| 15 | +import scala.concurrent.ExecutionContext |
| 16 | + |
| 17 | +class CommentController @Inject()( |
| 18 | + cc: MessagesControllerComponents, |
| 19 | + commentService: CommentService, |
| 20 | + authenticatedActionWithUser: AuthenticatedActionWithUser |
| 21 | + )(implicit ec: ExecutionContext) |
| 22 | + extends MessagesAbstractController(cc) |
| 23 | + with ValidationHandler { |
| 24 | + |
| 25 | + def create(taskId: Int): Action[JsValue] = |
| 26 | + authenticatedActionWithUser.async(parse.json) { request => |
| 27 | + implicit val messages: Messages = request.messages |
| 28 | + val createdBy = request.userToken.userId |
| 29 | + |
| 30 | + handleJsonValidation[CreateUpdateCommentRequest](request.body) { createCommentDto => |
| 31 | + commentService |
| 32 | + .createComment(taskId, createCommentDto, createdBy) |
| 33 | + .map { commentId => |
| 34 | + Created( |
| 35 | + Json.toJson( |
| 36 | + ApiResponse[Unit](s"Comment created successfully with ID: $commentId") |
| 37 | + ) |
| 38 | + ) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + def update(commentId: Int): Action[JsValue] = |
| 44 | + authenticatedActionWithUser.async(parse.json) { request => |
| 45 | + implicit val messages: Messages = request.messages |
| 46 | + val updatedBy = request.userToken.userId |
| 47 | + |
| 48 | + handleJsonValidation[CreateUpdateCommentRequest](request.body) { updateCommentDto => |
| 49 | + commentService |
| 50 | + .updateComment(commentId, updateCommentDto, updatedBy) |
| 51 | + .map { _ => |
| 52 | + Ok( |
| 53 | + Json.toJson( |
| 54 | + ApiResponse[Unit](s"Comment updated successfully") |
| 55 | + ) |
| 56 | + ) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 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 | + } |
| 75 | + |
| 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 | + |
| 86 | + |
| 87 | +} |
0 commit comments