Skip to content

Commit

Permalink
add bulk move method (#966)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz2030 authored Aug 9, 2022
1 parent af39cba commit 1d523d8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
18 changes: 17 additions & 1 deletion app/org/maproulette/controllers/api/ChallengeController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ class ChallengeController @Inject() (
/**
* Moves a challenge from one project to another. This requires admin access on both projects
*
* @param newProjectId The new project to move the challenge too
* @param newProjectId The new project to move the challenge to
* @param challengeId The challenge that you are moving
* @return Ok with no message
*/
Expand All @@ -1453,6 +1453,22 @@ class ChallengeController @Inject() (
}
}

/**
* Moves a list of challenges from one project to another. This requires admin access on both projects
*
* @param newProjectId The new project to move the challenge to
* @return Ok with no message
*/
def moveChallenges(newProjectId: Long): Action[JsValue] = Action.async(bodyParsers.json) {
implicit request =>
sessionManager.authenticatedRequest { implicit user =>
val body = request.body;
val challengeIds = (body \ "challenges").as[List[Long]]

Ok(Json.toJson(dalManager.challenge.moveChallenges(newProjectId, challengeIds, user)))
}
}

/**
* Archives a challenge
*
Expand Down
53 changes: 52 additions & 1 deletion app/org/maproulette/models/dal/ChallengeDAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ class ChallengeDAL @Inject() (

/**
* Moves a challenge from one project to another. You are required to have admin access on both
* the current project and the project you are moving the challenge too
* the current project and the project you are moving the challenge to
*
* @param newParent The id of the new parent project
* @param challengeId The id of the challenge that you are moving
Expand Down Expand Up @@ -1419,6 +1419,57 @@ class ChallengeDAL @Inject() (
}
}

/**
* Moves a list of challenges from one project to another. You are required to have admin access on both
* the current project and the project you are moving the challenges to
*
* @param newParent The id of the new parent project
* @param challengeIds The id of the challenge that you are moving
* @param c an implicit connection
*/
def moveChallenges(newParent: Long, challengeIds: List[Long], user: User)(
implicit c: Option[Connection] = None
): List[Long] = {
this.permission.hasAdminAccess(ProjectType(), user)(newParent)
this.serviceManager.project.retrieve(newParent) match {
case Some(p) =>
if (p.isVirtual.getOrElse(false)) {
throw new InvalidException(s"Cannot move challenge into a virtual project ($newParent).")
}
case None =>
// This shouldn't happen since we already did a permission check.
throw new NotFoundException(s"No project with id $newParent found.")
}

val movedChallengeIds = ListBuffer[Long]()
for (challengeId <- challengeIds) {
implicit val id = challengeId
this.retrieveById match {
case Some(c) => this.permission.hasObjectAdminAccess(c, user)
case None => throw new NotFoundException(s"No challenge with id $id found.")
}
this.cacheManager.withUpdatingCache(Long => retrieveById) { implicit item =>
this.withMRTransaction { implicit c =>
val movedChallenge =
SQL"UPDATE challenges SET parent_id = $newParent WHERE id = $id RETURNING #${this.retrieveColumns}"
.as(this.parser.*)
.headOption

// Also update status_actions so we don't lose our history
SQL"UPDATE status_actions SET project_id = $newParent WHERE challenge_id = $id"
.execute()

movedChallengeIds += id

movedChallenge
}
}
}

movedChallengeIds.toList
}


/**
* Update the popularity score of the given challenge following completion of a task.
* Challenge popularity p is calculated with the simple formula p = (p + t) / 2 where
Expand Down
23 changes: 23 additions & 0 deletions conf/v2_route/challenge.api
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,29 @@ GET /challenges/listing @org.maproulette.controllers
POST /challenge/:id/project/:projectId @org.maproulette.controllers.api.ChallengeController.moveChallenge(projectId:Long, id:Long)
###
# tags: [ Challenge ]
# summary: Move Challenges to another Project
# produces: [ application/json ]
# description: Will move a list of challenges into another project
# responses:
# '200':
# description: list of challenge IDs successfully moved
# schema:
# type: array
# items:
# type: object
# $ref: '#/definitions/org.maproulette.framework.model.Tag'
# parameters:
# - name: projectId
# in: path
# description: The ID of the project you are moving the challenge too.
# - name: challenges
# in: body
# description: a list of challenge ids to be moved
# required: true
###
POST /challenges/project/:projectId @org.maproulette.controllers.api.ChallengeController.moveChallenges(projectId:Long)
###
# tags: [ Challenge ]
# summary: Retrieve tags for Challenge
# produces: [ application/json ]
# description: Retrieves all the Tags that have been added to the specified Challenge
Expand Down

0 comments on commit 1d523d8

Please sign in to comment.