mirror of
				https://github.com/TheAnachronism/docspell.git
				synced 2025-10-31 17:50:11 +00:00 
			
		
		
		
	Fix position of merged attachments
This commit is contained in:
		| @@ -81,11 +81,12 @@ object Merge { | |||||||
|       def moveAttachments(items: NonEmptyList[Ident]): F[Int] = { |       def moveAttachments(items: NonEmptyList[Ident]): F[Int] = { | ||||||
|         val target = items.head |         val target = items.head | ||||||
|         for { |         for { | ||||||
|  |           nextPos <- store.transact(RAttachment.nextPosition(target)) | ||||||
|           attachs <- store.transact(items.tail.traverse(id => RAttachment.findByItem(id))) |           attachs <- store.transact(items.tail.traverse(id => RAttachment.findByItem(id))) | ||||||
|           attachFlat = attachs.flatMap(_.toList) |           attachFlat = attachs.flatMap(_.toList) | ||||||
|           n <- attachFlat.traverse(a => |           n <- attachFlat.zipWithIndex.traverse({ case (a, idx) => | ||||||
|             store.transact(RAttachment.updateItemId(a.id, target)) |             store.transact(RAttachment.updateItemId(a.id, target, nextPos + idx)) | ||||||
|           ) |           }) | ||||||
|         } yield n.sum |         } yield n.sum | ||||||
|       } |       } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -69,9 +69,9 @@ object RAttachment { | |||||||
|       DML.set(T.position.increment(1)) |       DML.set(T.position.increment(1)) | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|   def nextPosition(id: Ident): ConnectionIO[Int] = |   def nextPosition(itemId: Ident): ConnectionIO[Int] = | ||||||
|     for { |     for { | ||||||
|       max <- Select(max(T.position).s, from(T), T.itemId === id).build |       max <- Select(max(T.position).s, from(T), T.itemId === itemId).build | ||||||
|         .query[Option[Int]] |         .query[Option[Int]] | ||||||
|         .unique |         .unique | ||||||
|     } yield max.map(_ + 1).getOrElse(0) |     } yield max.map(_ + 1).getOrElse(0) | ||||||
| @@ -97,8 +97,15 @@ object RAttachment { | |||||||
|       DML.set(T.fileId.setTo(fId)) |       DML.set(T.fileId.setTo(fId)) | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|   def updateItemId(attachId: Ident, itemId: Ident): ConnectionIO[Int] = |   def updateItemId(attachId: Ident, itemId: Ident, pos: Int): ConnectionIO[Int] = | ||||||
|     DML.update(T, T.id === attachId, DML.set(T.itemId.setTo(itemId))) |     DML.update( | ||||||
|  |       T, | ||||||
|  |       T.id === attachId, | ||||||
|  |       DML.set( | ||||||
|  |         T.itemId.setTo(itemId), | ||||||
|  |         T.position.setTo(pos) | ||||||
|  |       ) | ||||||
|  |     ) | ||||||
|  |  | ||||||
|   def updatePosition(attachId: Ident, pos: Int): ConnectionIO[Int] = |   def updatePosition(attachId: Ident, pos: Int): ConnectionIO[Int] = | ||||||
|     DML.update(T, T.id === attachId, DML.set(T.position.setTo(pos))) |     DML.update(T, T.id === attachId, DML.set(T.position.setTo(pos))) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user