@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory
10
10
import anorm .ToParameterValue
11
11
import anorm ._ , postgresql ._
12
12
import javax .inject .{Inject , Singleton }
13
+ import org .maproulette .exception .{InvalidException }
13
14
import org .maproulette .Config
14
15
import org .maproulette .framework .psql .Query
15
16
import org .maproulette .framework .psql .filter .BaseParameter
@@ -41,38 +42,52 @@ class TaskBundleRepository @Inject() (
41
42
* @param name The name of the task bundle
42
43
* @param lockedTasks The tasks to be added to the bundle
43
44
*/
44
- def insert (
45
- user : User ,
46
- name : String ,
47
- taskIds : List [Long ],
48
- verifyTasks : (List [Task ]) => Unit
49
- ): TaskBundle = {
50
- this .withMRTransaction { implicit c =>
51
- val lockedTasks = this .withListLocking(user, Some (TaskType ())) { () =>
52
- this .taskDAL.retrieveListById(- 1 , 0 )(taskIds)
53
- }
45
+ def insert (
46
+ user : User ,
47
+ name : String ,
48
+ taskIds : List [Long ],
49
+ verifyTasks : (List [Task ]) => Unit
50
+ ): TaskBundle = {
51
+ this .withMRTransaction { implicit c =>
52
+ val lockedTasks = this .withListLocking(user, Some (TaskType ())) { () =>
53
+ this .taskDAL.retrieveListById(- 1 , 0 )(taskIds)
54
+ }
55
+
56
+
57
+ val failedTaskIds = taskIds.diff(lockedTasks.map(_.id))
58
+ if (failedTaskIds.nonEmpty) {
59
+ throw new InvalidException (s " Bundle creation failed because the following task IDs were locked: ${failedTaskIds.mkString(" , " )}" )
60
+ }
54
61
55
- verifyTasks(lockedTasks)
56
-
57
- val rowId =
58
- SQL """ INSERT INTO bundles (owner_id, name) VALUES ( ${user.id}, ${name}) """ .executeInsert()
59
- rowId match {
60
- case Some (bundleId) =>
61
- val sqlQuery =
62
- s """ INSERT INTO task_bundles (task_id, bundle_id) VALUES ({taskId}, $bundleId) """
63
- val parameters = lockedTasks.map(task => {
64
- Seq [NamedParameter ](" taskId" -> task.id)
65
- })
66
- BatchSql (sqlQuery, parameters.head, parameters.tail: _* ).execute()
67
- TaskBundle (bundleId, user.id, lockedTasks.map(task => {
68
- task.id
69
- }), Some (lockedTasks))
70
-
71
- case None =>
72
- throw new Exception (" Bundle creation failed" )
62
+ verifyTasks(lockedTasks)
63
+
64
+ for (task <- lockedTasks) {
65
+ try {
66
+ this .lockItem(user, task)
67
+ } catch {
68
+ case e : Exception => this .logger.warn(e.getMessage)
73
69
}
74
70
}
71
+
72
+ val rowId =
73
+ SQL """ INSERT INTO bundles (owner_id, name) VALUES ( ${user.id}, ${name}) """ .executeInsert()
74
+ rowId match {
75
+ case Some (bundleId) =>
76
+ val sqlQuery =
77
+ s """ INSERT INTO task_bundles (task_id, bundle_id) VALUES ({taskId}, $bundleId) """
78
+ val parameters = lockedTasks.map(task => {
79
+ Seq [NamedParameter ](" taskId" -> task.id)
80
+ })
81
+ BatchSql (sqlQuery, parameters.head, parameters.tail: _* ).execute()
82
+ TaskBundle (bundleId, user.id, lockedTasks.map(task => {
83
+ task.id
84
+ }), Some (lockedTasks))
85
+
86
+ case None =>
87
+ throw new Exception (" Bundle creation failed" )
88
+ }
75
89
}
90
+ }
76
91
77
92
/**
78
93
* Removes tasks from a bundle.
0 commit comments