Skip to content

Commit

Permalink
remove bundleTasks endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Mar 10, 2024
1 parent ccac507 commit 96668fc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,6 @@ class TaskBundleController @Inject() (
}
}

/**
* Adds tasks to a bundle.
*
* @param id The id for the bundle
* @param taskIds List of task ids to remove
* @return Task Bundle
*/
def bundleTasks(id: Long, taskIds: List[Long]): Action[AnyContent] = Action.async {
implicit request =>
this.sessionManager.authenticatedRequest { implicit user =>
this.serviceManager.taskBundle.bundleTasks(user, id, taskIds)
Ok(Json.toJson(this.serviceManager.taskBundle.getTaskBundle(user, id)))
}
}

/**
* Delete bundle.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ trait SearchParametersMixin {
case Some(bid) =>
FilterGroup(
List(
CustomParameter(s"CAST(${Task.TABLE}.${Task.FIELD_BUNDLE_ID} AS TEXT) LIKE '${bid}%'")
CustomParameter(s"${Task.TABLE}.${Task.FIELD_BUNDLE_ID} = $bid")
)
)
case _ => FilterGroup(List())
Expand Down
137 changes: 49 additions & 88 deletions app/org/maproulette/framework/repository/TaskBundleRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ class TaskBundleRepository @Inject() (
"inList" -> taskIds
)
.executeUpdate()

primaryId.foreach { id =>
SQL"""UPDATE tasks SET is_bundle_primary = true, bundle_id = $bundleId WHERE id = $id"""
.executeUpdate()
primaryId match {
case Some(id) =>
val sqlQuery = s"""UPDATE tasks SET is_bundle_primary = true WHERE id = $id"""
SQL(sqlQuery).executeUpdate()
case None =>
// Handle the case where primaryId is None
println("primaryId is not defined")
}

val parameters = lockedTasks.map(task => Seq[NamedParameter]("taskId" -> task.id))
Expand Down Expand Up @@ -198,6 +201,14 @@ class TaskBundleRepository @Inject() (
)
.executeUpdate()

// Retrieve the status of the primary task
val primaryTaskStatus = SQL(
"""SELECT status FROM tasks WHERE id = {primaryTaskId}"""
).on(
"primaryTaskId" -> primaryTaskId
)
.as(SqlParser.scalar[Int].single)

val lockedTasks = this.withListLocking(user, Some(TaskType())) { () =>
this.taskDAL.retrieveListById(-1, 0)(tasksToAdd)
}
Expand All @@ -207,33 +218,29 @@ class TaskBundleRepository @Inject() (
} catch {
case e: Exception => this.logger.warn(e.getMessage)
}
// Update the status of all tasks in the bundle to match the status of the primary task
SQL(
"""UPDATE tasks
SET status = {primaryTaskStatus}
WHERE bundle_id = {bundleId}
"""
).on(
"primaryTaskStatus" -> primaryTaskStatus,
"bundleId" -> bundleId
)
.executeUpdate()

this.cacheManager.withOptionCaching { () =>
Some(
task.copy(bundleId = Some(bundleId), isBundlePrimary = Some(primaryTaskId == task.id))
task.copy(
bundleId = Some(bundleId),
status = Some(primaryTaskStatus),
isBundlePrimary = Some(primaryTaskId == task.id)
)
)
}
}
}

// Retrieve the status of the primary task
val primaryTaskStatus = SQL(
"""SELECT status FROM tasks WHERE id = {primaryTaskId}"""
).on(
"primaryTaskId" -> primaryTaskId
)
.as(SqlParser.scalar[Int].single)

// Update the status of all tasks in the bundle to match the status of the primary task
SQL(
"""UPDATE tasks
SET status = {primaryTaskStatus}
WHERE bundle_id = {bundleId}
"""
).on(
"primaryTaskStatus" -> primaryTaskStatus,
"bundleId" -> bundleId
)
.executeUpdate()
}
}

Expand Down Expand Up @@ -275,85 +282,39 @@ class TaskBundleRepository @Inject() (
case Some(_) =>
SQL(s"""DELETE FROM task_bundles
WHERE bundle_id = $bundleId AND task_id = ${task.id}""").executeUpdate()

this.cacheManager.withOptionCaching { () =>
Some(
task.copy(bundleId = None, status = Some(STATUS_CREATED))
)
}
if (!preventTaskIdUnlocks.contains(task.id)) {
try {
this.unlockItem(user, task)
} catch {
case e: Exception => this.logger.warn(e.getMessage)
}
} else {
SQL(
"""UPDATE tasks
}
// This is in order to pass the filters so the task is displayed as "available" in task searching and maps.
SQL(s"DELETE FROM task_review tr WHERE tr.task_id = ${task.id}").executeUpdate()

SQL(
"""UPDATE tasks
SET status = {status}
WHERE id = {taskId}
"""
).on(
"taskId" -> task.id,
"status" -> STATUS_CREATED
)
.executeUpdate()
}
).on(
"taskId" -> task.id,
"status" -> STATUS_CREATED
)
.executeUpdate()

case None => // do nothing
}
}
}
}
}

/**
* Adds tasks to a bundle.
*
* @param bundleId The id of the bundle
*/
def bundleTasks(user: User, bundleId: Long, taskIds: List[Long]): Unit = {
this.withMRConnection { implicit c =>
// Update tasks to set their bundle_id to the provided bundleId
SQL(s"""UPDATE tasks SET bundle_id = {bundleId}
WHERE bundle_id IS NULL
AND id IN ({inList})""")
.on(
"bundleId" -> bundleId,
"inList" -> taskIds
)
.executeUpdate()

// Construct the SQL query to insert task-bundle associations
val sqlQuery =
s"""INSERT INTO task_bundles (bundle_id, task_id) VALUES ($bundleId, {taskId})"""

// Construct parameters for the BatchSql operation
val parameters = taskIds.map(taskId => Seq[NamedParameter]("taskId" -> taskId))

// Execute the BatchSql operation to insert task-bundle associations
BatchSql(sqlQuery, parameters.head, parameters.tail: _*).execute()

// Retrieve the tasks associated with the given bundleId
val tasks = this.retrieveTasks(
Query.simple(
List(
BaseParameter("bundle_id", bundleId, table = Some("tb"))
)
)
)
// Lock each of the new tasks to indicate they are part of the bundle
for (task <- tasks) {
try {
this.lockItem(user, task)
} catch {
case e: Exception => this.logger.warn(e.getMessage)
}
this.cacheManager.withOptionCaching { () =>
Some(
task.copy(
bundleId = Some(bundleId)
)
)
}
}
}
}

/**
* Deletes a task bundle.
*
Expand Down
12 changes: 0 additions & 12 deletions app/org/maproulette/framework/service/TaskBundleService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,6 @@ class TaskBundleService @Inject() (
this.getTaskBundle(user, bundleId)
}

/**
* Removes tasks from a bundle.
*
* @param bundleId The id of the bundle
*/
def bundleTasks(user: User, bundleId: Long, taskIds: List[Long])(): TaskBundle = {
val bundle = this.getTaskBundle(user, bundleId)

this.repository.bundleTasks(user, bundleId, taskIds)
this.getTaskBundle(user, bundleId)
}

/**
* Removes tasks from a bundle.
*
Expand Down
26 changes: 0 additions & 26 deletions conf/v2_route/bundle.api
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,3 @@ DELETE /taskBundle/:id @org.maproulette.framework.c
# required: true
###
POST /taskBundle/:id/unbundle @org.maproulette.framework.controller.TaskBundleController.unbundleTasks(id:Long, taskIds:List[Long], preventTaskIdUnlocks:List[Long])
###
# tags: [ Bundle ]
# summary: Bundles extra tasks to Task Bundle
# description: Adds a list of tasks to a bundle of tasks
# responses:
# '200':
# description: The task bundle with the new increased set of tasks
# content:
# application/json:
# schema:
# $ref: '#/components/schemas/TaskBundle'
# '401':
# description: The user is not authorized to make this request
# '404':
# description: No Task Bundle with provided ID found
# parameters:
# - name: id
# in: path
# description: The id of the Task Bundle
# required: true
# - name: taskIds
# in: query
# description: The list of task ids to add from to bundle
# required: true
###
POST /taskBundle/:id/bundle @org.maproulette.framework.controller.TaskBundleController.bundleTasks(id:Long, taskIds:List[Long])

0 comments on commit 96668fc

Please sign in to comment.