3030use OCP \Collaboration \Reference \IReferenceManager ;
3131use OCP \Comments \ICommentsManager ;
3232use OCP \EventDispatcher \IEventDispatcher ;
33+ use OCP \IDBConnection ;
3334use OCP \IRequest ;
3435use OCP \IURLGenerator ;
3536use OCP \IUserManager ;
@@ -58,6 +59,7 @@ public function __construct(
5859 private CardServiceValidator $ cardServiceValidator ,
5960 private AssignmentService $ assignmentService ,
6061 private IReferenceManager $ referenceManager ,
62+ private IDBConnection $ connection ,
6163 private ?string $ userId ,
6264 ) {
6365 }
@@ -187,7 +189,7 @@ public function findCalendarEntries(int $boardId): array {
187189 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
188190 * @throws BadrequestException
189191 */
190- public function create (string $ title , int $ stackId , string $ type , int $ order , string $ owner , string $ description = '' , $ duedate = null , $ startdate = null , ?string $ color = null ): Card {
192+ public function create (string $ title , int $ stackId , string $ type , int $ order , string $ owner , string $ description = '' , $ duedate = null , $ startdate = null , ?string $ color = null , bool $ insertAtPosition = false ): Card {
191193 $ this ->cardServiceValidator ->check (compact ('title ' , 'stackId ' , 'type ' , 'order ' , 'owner ' ));
192194
193195 $ this ->permissionService ->checkPermission ($ this ->stackMapper , $ stackId , Acl::PERMISSION_EDIT );
@@ -204,7 +206,20 @@ public function create(string $title, int $stackId, string $type, int $order, st
204206 $ card ->setDuedate ($ duedate );
205207 $ card ->setStartdate ($ startdate );
206208 $ card ->setColor ($ color );
207- $ card = $ this ->cardMapper ->insert ($ card );
209+
210+ if (!$ insertAtPosition ) {
211+ $ card = $ this ->cardMapper ->insert ($ card );
212+ } else {
213+ $ this ->connection ->beginTransaction ();
214+ try {
215+ $ card = $ this ->cardMapper ->insert ($ card );
216+ $ this ->reorderCards ($ card ->getId (), $ stackId , $ order );
217+ $ this ->connection ->commit ();
218+ } catch (\Throwable $ e ) {
219+ $ this ->connection ->rollBack ();
220+ throw $ e ;
221+ }
222+ }
208223
209224 $ this ->activityManager ->triggerEvent (ActivityManager::DECK_OBJECT_CARD , $ card , ActivityManager::SUBJECT_CARD_CREATE , [], $ card ->getOwner ());
210225 $ this ->changeHelper ->cardChanged ($ card ->getId (), false );
@@ -467,6 +482,17 @@ public function reorder(int $id, int $stackId, int $order): array {
467482 $ changes ->setAfter ($ card );
468483 $ this ->activityManager ->triggerUpdateEvents (ActivityManager::DECK_OBJECT_CARD , $ changes , ActivityManager::SUBJECT_CARD_UPDATE );
469484
485+ $ result = $ this ->reorderCards ($ id , $ stackId , $ order );
486+ $ this ->changeHelper ->cardChanged ($ id , false );
487+ $ this ->eventDispatcher ->dispatchTyped (new CardUpdatedEvent ($ card , $ changes ->getBefore ()));
488+
489+ return $ result ;
490+ }
491+
492+ /**
493+ * @return list<Card>
494+ */
495+ private function reorderCards (int $ id , int $ stackId , int $ order ): array {
470496 $ cardsToReorder = $ this ->cardMapper ->findAll ($ stackId );
471497 $ result = [];
472498 $ i = 0 ;
@@ -489,8 +515,6 @@ public function reorder(int $id, int $stackId, int $order): array {
489515 $ this ->cardMapper ->update ($ cardToReorder );
490516 $ result [$ cardToReorder ->getOrder ()] = $ cardToReorder ;
491517 }
492- $ this ->changeHelper ->cardChanged ($ id , false );
493- $ this ->eventDispatcher ->dispatchTyped (new CardUpdatedEvent ($ card , $ changes ->getBefore ()));
494518
495519 return array_values ($ result );
496520 }
0 commit comments