Skip to content

Commit

Permalink
simplify bundle creation
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Jan 9, 2025
1 parent 69ca85e commit 5e0fa47
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions app/org/maproulette/framework/repository/TaskBundleRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class TaskBundleRepository @Inject() (
taskIds: List[Long],
verifyTasks: (List[Task]) => Unit
): TaskBundle = {
this.withMRTransaction { implicit c =>
// First transaction: verify tasks and create bundle
val bundleId = this.withMRTransaction { implicit c =>
val lockedTasks = this.withListLocking(user, Some(TaskType())) { () =>
this.taskDAL.retrieveListById(-1, 0)(taskIds)
}
Expand All @@ -70,39 +71,26 @@ class TaskBundleRepository @Inject() (
SQL"""INSERT INTO bundles (owner_id, name) VALUES (${user.id}, ${name})""".executeInsert()

rowId match {
case Some(bundleId: Long) =>
// Update the task object to bind it to the bundle
SQL(s"""UPDATE tasks SET bundle_id = $bundleId
WHERE id IN ({inList})""")
.on(
"inList" -> taskIds
)
.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
}

val sqlInsertTaskBundles =
s"""INSERT INTO task_bundles (task_id, bundle_id) VALUES ({taskId}, $bundleId)"""
val parameters = lockedTasks.map(task => Seq[NamedParameter]("taskId" -> task.id))
BatchSql(sqlInsertTaskBundles, parameters.head, parameters.tail: _*).execute()

for (task <- lockedTasks) {
taskRepository.cacheManager.cache.remove(task.id)
case Some(id: Long) =>
// Set primary task if specified
primaryId.foreach { pid =>
SQL"""UPDATE tasks SET is_bundle_primary = true WHERE id = $pid""".executeUpdate()
}

TaskBundle(bundleId, user.id, lockedTasks.map(task => {
task.id
}), Some(lockedTasks))

id
case None =>
throw new Exception("Bundle creation failed")
}
}

// Second transaction: add tasks to bundle
this.bundleTasks(user, bundleId, taskIds)

val lockedTasks = this.withListLocking(user, Some(TaskType())) { () =>
this.taskDAL.retrieveListById(-1, 0)(taskIds)
}

// Return the created bundle
TaskBundle(bundleId, user.id, taskIds, Some(lockedTasks))
}

/**
Expand Down

0 comments on commit 5e0fa47

Please sign in to comment.