mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 10:28:27 +00:00
Allow to change position of attachments
This commit is contained in:
@ -72,12 +72,18 @@ case class Column(name: String, ns: String = "", alias: String = "") {
|
||||
def isGt[A: Put](a: A): Fragment =
|
||||
f ++ fr"> $a"
|
||||
|
||||
def isGte[A: Put](a: A): Fragment =
|
||||
f ++ fr">= $a"
|
||||
|
||||
def isGt(c: Column): Fragment =
|
||||
f ++ fr">" ++ c.f
|
||||
|
||||
def isLt[A: Put](a: A): Fragment =
|
||||
f ++ fr"< $a"
|
||||
|
||||
def isLte[A: Put](a: A): Fragment =
|
||||
f ++ fr"<= $a"
|
||||
|
||||
def isLt(c: Column): Fragment =
|
||||
f ++ fr"<" ++ c.f
|
||||
|
||||
@ -103,4 +109,10 @@ case class Column(name: String, ns: String = "", alias: String = "") {
|
||||
|
||||
def max: Fragment =
|
||||
fr"MAX(" ++ f ++ fr")"
|
||||
|
||||
def increment[A: Put](a: A): Fragment =
|
||||
f ++ fr"=" ++ f ++ fr"+ $a"
|
||||
|
||||
def decrement[A: Put](a: A): Fragment =
|
||||
f ++ fr"=" ++ f ++ fr"- $a"
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package docspell.store.queries
|
||||
|
||||
import bitpeace.FileMeta
|
||||
import cats.implicits._
|
||||
import cats.effect.Sync
|
||||
import cats.data.OptionT
|
||||
import cats.implicits._
|
||||
import fs2.Stream
|
||||
import doobie._
|
||||
import doobie.implicits._
|
||||
@ -16,6 +17,44 @@ import org.log4s._
|
||||
object QItem {
|
||||
private[this] val logger = getLogger
|
||||
|
||||
def moveAttachmentBefore(
|
||||
itemId: Ident,
|
||||
source: Ident,
|
||||
target: Ident
|
||||
): ConnectionIO[Int] = {
|
||||
|
||||
// rs < rt
|
||||
def moveBack(rs: RAttachment, rt: RAttachment): ConnectionIO[Int] =
|
||||
for {
|
||||
n <- RAttachment.decPositions(itemId, rs.position, rt.position)
|
||||
k <- RAttachment.updatePosition(rs.id, rt.position)
|
||||
} yield n + k
|
||||
|
||||
// rs > rt
|
||||
def moveForward(rs: RAttachment, rt: RAttachment): ConnectionIO[Int] =
|
||||
for {
|
||||
n <- RAttachment.incPositions(itemId, rt.position, rs.position)
|
||||
k <- RAttachment.updatePosition(rs.id, rt.position)
|
||||
} yield n + k
|
||||
|
||||
(for {
|
||||
_ <- OptionT.liftF(
|
||||
if (source == target)
|
||||
Sync[ConnectionIO].raiseError(new Exception("Attachments are the same!"))
|
||||
else ().pure[ConnectionIO]
|
||||
)
|
||||
rs <- OptionT(RAttachment.findById(source)).filter(_.itemId == itemId)
|
||||
rt <- OptionT(RAttachment.findById(target)).filter(_.itemId == itemId)
|
||||
n <- OptionT.liftF(
|
||||
if (rs.position == rt.position || rs.position + 1 == rt.position)
|
||||
0.pure[ConnectionIO]
|
||||
else if (rs.position < rt.position) moveBack(rs, rt)
|
||||
else moveForward(rs, rt)
|
||||
)
|
||||
} yield n).getOrElse(0)
|
||||
|
||||
}
|
||||
|
||||
case class ItemData(
|
||||
item: RItem,
|
||||
corrOrg: Option[ROrganization],
|
||||
|
@ -38,6 +38,20 @@ object RAttachment {
|
||||
fr"${v.id},${v.itemId},${v.fileId.id},${v.position},${v.created},${v.name}"
|
||||
).update.run
|
||||
|
||||
def decPositions(iId: Ident, lowerBound: Int, upperBound: Int): ConnectionIO[Int] =
|
||||
updateRow(
|
||||
table,
|
||||
and(itemId.is(iId), position.isGte(lowerBound), position.isLte(upperBound)),
|
||||
position.decrement(1)
|
||||
).update.run
|
||||
|
||||
def incPositions(iId: Ident, lowerBound: Int, upperBound: Int): ConnectionIO[Int] =
|
||||
updateRow(
|
||||
table,
|
||||
and(itemId.is(iId), position.isGte(lowerBound), position.isLte(upperBound)),
|
||||
position.increment(1)
|
||||
).update.run
|
||||
|
||||
def nextPosition(id: Ident): ConnectionIO[Int] =
|
||||
for {
|
||||
max <- selectSimple(position.max, table, itemId.is(id)).query[Option[Int]].unique
|
||||
|
Reference in New Issue
Block a user