Skip to content

Commit

Permalink
Move comments parameters into Request Body (#1091)
Browse files Browse the repository at this point in the history
* move comments parameters into requestbody

* remove URLDecoder on comments

* update swagger docs

* update the update task comment flow for comment located in request body

* remove white space from comments to prevent empty strings
  • Loading branch information
CollinBeczak authored Dec 13, 2023
1 parent f975e18 commit 515d785
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 52 deletions.
75 changes: 42 additions & 33 deletions app/org/maproulette/framework/controller/CommentController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@

package org.maproulette.framework.controller

import java.net.URLDecoder

import javax.inject.Inject
import org.maproulette.data.ActionManager
import org.maproulette.framework.service.{CommentService, ServiceManager}
import org.maproulette.session.SessionManager
import play.api.libs.json.Json
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._

/**
Expand Down Expand Up @@ -115,56 +113,64 @@ class CommentController @Inject() (
* Adds a comment for a specific task
*
* @param taskId The id for a task
* @param comment The comment the user is leaving
* @param actionId The action if any associated with the comment
* @return Ok if successful.
*/
def add(taskId: Long, comment: String, actionId: Option[Long]): Action[AnyContent] =
Action.async { implicit request =>
def add(taskId: Long, actionId: Option[Long]): Action[JsValue] =
Action.async(parse.json) { implicit request =>
this.sessionManager.authenticatedRequest { implicit user =>
Created(
Json.toJson(
this.commentService.create(user, taskId, URLDecoder.decode(comment, "UTF-8"), actionId)
)
)
val commentResult = (request.body \ "comment").asOpt[String].map(_.trim)
commentResult match {
case Some(comment) =>
Created(
Json.toJson(
this.commentService
.create(user, taskId, comment, actionId)
)
)
}
}
}

/**
* Adds a comment for a specific challenge
*
* @param challengeId The id for a challenge
* @param comment The comment the user is leaving
* @return Ok if successful.
*/
def addChallengeComment(challengeId: Long, comment: String): Action[AnyContent] =
Action.async { implicit request =>
def addChallengeComment(challengeId: Long): Action[JsValue] =
Action.async(parse.json) { implicit request =>
this.sessionManager.authenticatedRequest { implicit user =>
Created(
Json.toJson(
this.commentService
.createChallengeComment(user, challengeId, URLDecoder.decode(comment, "UTF-8"))
)
)
val commentResult = (request.body \ "comment").asOpt[String].map(_.trim)
commentResult match {
case Some(comment) =>
Created(
Json.toJson(
this.commentService
.createChallengeComment(user, challengeId, comment)
)
)
}
}
}

/**
* Adds a comment for tasks in a bundle
*
* @param bundleId The id for the bundle
* @param comment The comment the user is leaving
* @param actionId The action if any associated with the comment
* @return Ok if successful.
*/
def addToBundleTasks(
bundleId: Long,
comment: String,
actionId: Option[Long]
): Action[AnyContent] = Action.async { implicit request =>
): Action[JsValue] = Action.async(parse.json) { implicit request =>
this.sessionManager.authenticatedRequest { implicit user =>
this.commentService.addToBundle(user, bundleId, comment, actionId)

val commentResult = (request.body \ "comment").asOpt[String].map(_.trim)
commentResult match {
case Some(comment) =>
this.commentService.addToBundle(user, bundleId, comment, actionId)
}
Ok(Json.toJson(this.serviceManager.taskBundle.getTaskBundle(user, bundleId)))
}
}
Expand All @@ -173,18 +179,21 @@ class CommentController @Inject() (
* Updates the original comment
*
* @param commentId The ID of the comment to update
* @param comment The comment to update
* @return
*/
def update(commentId: Long, comment: String): Action[AnyContent] = Action.async {
implicit request =>
this.sessionManager.authenticatedRequest { implicit user =>
Ok(
Json.toJson(
this.commentService.update(commentId, URLDecoder.decode(comment, "UTF-8"), user)
def update(commentId: Long): Action[JsValue] = Action.async(parse.json) { implicit request =>
this.sessionManager.authenticatedRequest { implicit user =>
val commentResult = (request.body \ "comment").asOpt[String].map(_.trim)
commentResult match {
case Some(comment) =>
Ok(
Json.toJson(
this.commentService
.update(commentId, comment, user)
)
)
)
}
}
}

/**
Expand Down
4 changes: 1 addition & 3 deletions app/org/maproulette/framework/service/CommentService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package org.maproulette.framework.service

import java.net.URLDecoder

import javax.inject.{Inject, Singleton}
import org.apache.commons.lang3.StringUtils
import org.maproulette.exception.{InvalidException, NotFoundException}
Expand Down Expand Up @@ -117,7 +115,7 @@ class CommentService @Inject() (

var notify = true
for (task <- tasks) {
this.create(user, task.id, URLDecoder.decode(comment, "UTF-8"), actionId, notify)
this.create(user, task.id, comment, actionId, notify)
notify = false
}
bundle
Expand Down
76 changes: 60 additions & 16 deletions conf/v2_route/comment.api
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,25 @@ GET /challengeComments/user/:id @org.maproulette.fr
# - name: id
# in: path
# description: The ID for the Task
# - name: comment
# in: query
# description: A URLEncoded comment for the Task
# - name: actionId
# in: query
# description: An optional action ID that may be associated with the comment
# requestBody:
# description: The JSON structure for the Comments body
# required: true
# content:
# application/json:
# schema:
# type: object
# properties:
# comment:
# type: string
# description: User's comment.
# example: "This is an example comment."
# required:
# - comment
###
POST /task/:id/comment @org.maproulette.framework.controller.CommentController.add(id:Long, comment:String, actionId:Option[Long])
POST /task/:id/comment @org.maproulette.framework.controller.CommentController.add(id:Long, actionId:Option[Long])
###
# tags: [ Comment ]
# summary: Adds comment to each Task in a Task Bundle
Expand All @@ -122,14 +133,25 @@ POST /task/:id/comment @org.maproulette.framework.c
# - name: id
# in: path
# description: The ID for the bundle
# - name: comment
# in: query
# description: A URLEncoded comment for the Task
# - name: actionId
# in: query
# description: An optional action ID that may be associated with the comment
# requestBody:
# description: The JSON structure for the Comments body
# required: true
# content:
# application/json:
# schema:
# type: object
# properties:
# comment:
# type: string
# description: User's comment.
# example: "This is an example comment."
# required:
# - comment
###
POST /taskBundle/:id/comment @org.maproulette.framework.controller.CommentController.addToBundleTasks(id:Long, comment:String, actionId:Option[Long])
POST /taskBundle/:id/comment @org.maproulette.framework.controller.CommentController.addToBundleTasks(id:Long, actionId:Option[Long])
###
# tags: [ Comment ]
# summary: Update comment on Task
Expand All @@ -149,11 +171,22 @@ POST /taskBundle/:id/comment @org.maproulette.framework.c
# - name: commentId
# in: path
# description: The ID of the original comment
# - name: comment
# in: query
# description: A URLEncoded comment for the Task
# requestBody:
# description: The JSON structure for the Comments body
# required: true
# content:
# application/json:
# schema:
# type: object
# properties:
# comment:
# type: string
# description: User's comment.
# example: "This is an example comment."
# required:
# - comment
###
PUT /comment/:commentId @org.maproulette.framework.controller.CommentController.update(commentId:Long, comment:String)
PUT /comment/:commentId @org.maproulette.framework.controller.CommentController.update(commentId:Long)
###
# tags: [ Comment ]
# summary: Deletes comment from Task
Expand Down Expand Up @@ -189,11 +222,22 @@ DELETE /task/:id/comment/:commentId @org.maproulette.framework.c
# - name: id
# in: path
# description: The ID for the Challenge
# - name: comment
# in: query
# description: A URLEncoded comment for the Challenge
# requestBody:
# description: The JSON structure for the Comments body
# required: true
# content:
# application/json:
# schema:
# type: object
# properties:
# comment:
# type: string
# description: User's comment.
# example: "This is an example comment."
# required:
# - comment
###
POST /challenge/:id/comment @org.maproulette.framework.controller.CommentController.addChallengeComment(id:Long, comment:String)
POST /challenge/:id/comment @org.maproulette.framework.controller.CommentController.addChallengeComment(id:Long)
###
# tags: [ Comment ]
# summary: Retrieves comments for a Challenge
Expand Down

0 comments on commit 515d785

Please sign in to comment.