From a117905c99c7c031e70c2b09c7de06293436015d Mon Sep 17 00:00:00 2001 From: CollinBeczak Date: Sun, 29 Dec 2024 18:58:55 -0600 Subject: [PATCH] make bundling data more usefule --- .../controllers/api/TaskController.scala | 42 +++++++++++-------- conf/v2_route/task.api | 7 +--- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/org/maproulette/controllers/api/TaskController.scala b/app/org/maproulette/controllers/api/TaskController.scala index e3fd083e..9d28ea1b 100644 --- a/app/org/maproulette/controllers/api/TaskController.scala +++ b/app/org/maproulette/controllers/api/TaskController.scala @@ -378,20 +378,26 @@ class TaskController @Inject() ( def lockTaskBundleByIds(taskIds: List[Long]): Action[AnyContent] = Action.async { implicit request => this.sessionManager.authenticatedRequest { implicit user => - val results = taskIds.map { taskId => - this.dal.retrieveById(taskId) match { - case Some(task) => - try { - this.dal.lockItem(user, task) - Some(taskId) - } catch { - case _: LockedException => None - } - case None => None - } - }.flatten + val (lockedTasks, failedTasks) = taskIds.foldLeft((List.empty[Task], List.empty[Long])) { + case ((locked, failed), taskId) => + this.dal.retrieveById(taskId) match { + case Some(task) => + try { + this.dal.lockItem(user, task) + (task :: locked, failed) + } catch { + case _: LockedException => (locked, taskId :: failed) + } + case None => (locked, failed) + } + } - Ok(Json.toJson(results)) + if (failedTasks.nonEmpty) { + lockedTasks.foreach(task => this.dal.unlockItem(user, task)) + Ok(Json.toJson(failedTasks, false)) + } else { + Ok(Json.toJson(lockedTasks, true)) + } } } @@ -404,7 +410,7 @@ class TaskController @Inject() ( def unlockTaskBundleByIds(taskIds: List[Long]): Action[AnyContent] = Action.async { implicit request => this.sessionManager.authenticatedRequest { implicit user => - val results = taskIds.map { taskId => + val tasks = taskIds.flatMap { taskId => this.dal.retrieveById(taskId) match { case Some(task) => try { @@ -415,9 +421,9 @@ class TaskController @Inject() ( } case None => None } - }.flatten + } - Ok(Json.toJson(results)) + Ok(Json.toJson(tasks)) } } @@ -430,7 +436,7 @@ class TaskController @Inject() ( def refreshTaskLocksByIds(taskIds: List[Long]): Action[AnyContent] = Action.async { implicit request => this.sessionManager.authenticatedRequest { implicit user => - val results = taskIds.map { taskId => + val result = taskIds.map { taskId => this.dal.retrieveById(taskId) match { case Some(task) => try { @@ -443,7 +449,7 @@ class TaskController @Inject() ( } }.flatten - Ok(Json.toJson(results)) + Ok(Json.toJson(result)) } } diff --git a/conf/v2_route/task.api b/conf/v2_route/task.api index 9d127bc1..aeafbb62 100644 --- a/conf/v2_route/task.api +++ b/conf/v2_route/task.api @@ -916,14 +916,13 @@ GET /tasks/box/:left/:bottom/:right/:top @org.maproulette.framework.c # description: Success ### GET /taskCluster @org.maproulette.framework.controller.TaskController.getTaskClusters(points:Int ?= 100) - ### # tags: [ Task ] # summary: Locks a bundle of tasks -# description: Locks the specified tasks in the bundle. +# description: Attempts to lock a set of tasks. If successful, returns the tasks that were locked. If not successful, returns the tasks that were not locked. # responses: # '200': -# description: Success message +# description: List of tasks that were locked or list of tasks that were not locked. Boolean indicates if the tasks were locked. # '401': # description: The user is not authorized to make this request # requestBody: @@ -937,7 +936,6 @@ GET /taskCluster @org.maproulette.framework.c # type: integer ### POST /task/bundle/lock @org.maproulette.controllers.api.TaskController.lockTaskBundleByIds(taskIds:List[Long]) - ### # tags: [ Task ] # summary: Unlocks a bundle of tasks @@ -958,7 +956,6 @@ POST /task/bundle/lock @org.maproulette.controllers.a # type: integer ### POST /task/bundle/unlock @org.maproulette.controllers.api.TaskController.unlockTaskBundleByIds(taskIds:List[Long]) - ### # tags: [ Task ] # summary: Refreshes locks on a bundle of tasks