Allow to change position of attachments

This commit is contained in:
Eike Kettner
2020-05-24 17:29:28 +02:00
parent 6807ce4d2f
commit 3cb738568f
6 changed files with 123 additions and 1 deletions

View File

@ -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"
}

View File

@ -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],

View File

@ -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