mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Editing tags for multiple items
This commit is contained in:
@ -57,7 +57,12 @@ case class Column(name: String, ns: String = "", alias: String = "") {
|
||||
f ++ fr"IN (" ++ commas(values) ++ fr")"
|
||||
|
||||
def isIn[A: Put](values: NonEmptyList[A]): Fragment =
|
||||
isIn(values.map(a => sql"$a").toList)
|
||||
values.tail match {
|
||||
case Nil =>
|
||||
is(values.head)
|
||||
case _ =>
|
||||
isIn(values.map(a => sql"$a").toList)
|
||||
}
|
||||
|
||||
def isLowerIn[A: Put](values: NonEmptyList[A]): Fragment =
|
||||
fr"lower(" ++ f ++ fr") IN (" ++ commas(values.map(a => sql"$a").toList) ++ fr")"
|
||||
|
@ -132,7 +132,7 @@ object RItem {
|
||||
} yield n
|
||||
|
||||
def updateStateForCollective(
|
||||
itemId: Ident,
|
||||
itemIds: NonEmptyList[Ident],
|
||||
itemState: ItemState,
|
||||
coll: Ident
|
||||
): ConnectionIO[Int] =
|
||||
@ -140,7 +140,7 @@ object RItem {
|
||||
t <- currentTime
|
||||
n <- updateRow(
|
||||
table,
|
||||
and(id.is(itemId), cid.is(coll)),
|
||||
and(id.isIn(itemIds), cid.is(coll)),
|
||||
commas(state.setTo(itemState), updated.setTo(t))
|
||||
).update.run
|
||||
} yield n
|
||||
@ -324,4 +324,10 @@ object RItem {
|
||||
val empty: Option[Ident] = None
|
||||
updateRow(table, folder.is(folderId), folder.setTo(empty)).update.run
|
||||
}
|
||||
|
||||
def filterItemsFragment(items: NonEmptyList[Ident], coll: Ident): Fragment =
|
||||
selectSimple(Seq(id), table, and(cid.is(coll), id.isIn(items)))
|
||||
|
||||
def filterItems(items: NonEmptyList[Ident], coll: Ident): ConnectionIO[Vector[Ident]] =
|
||||
filterItemsFragment(items, coll).query[Ident].to[Vector]
|
||||
}
|
||||
|
@ -30,18 +30,17 @@ object RTagItem {
|
||||
def deleteItemTags(item: Ident): ConnectionIO[Int] =
|
||||
deleteFrom(table, itemId.is(item)).update.run
|
||||
|
||||
def deleteItemTags(items: NonEmptyList[Ident], cid: Ident): ConnectionIO[Int] = {
|
||||
val itemsFiltered =
|
||||
RItem.filterItemsFragment(items, cid)
|
||||
val sql = fr"DELETE FROM" ++ table ++ fr"WHERE" ++ itemId.isIn(itemsFiltered)
|
||||
|
||||
sql.update.run
|
||||
}
|
||||
|
||||
def deleteTag(tid: Ident): ConnectionIO[Int] =
|
||||
deleteFrom(table, tagId.is(tid)).update.run
|
||||
|
||||
def insertItemTags(item: Ident, tags: Seq[Ident]): ConnectionIO[Int] =
|
||||
for {
|
||||
tagValues <- tags.toList.traverse(id =>
|
||||
Ident.randomId[ConnectionIO].map(rid => RTagItem(rid, item, id))
|
||||
)
|
||||
tagFrag = tagValues.map(v => fr"${v.tagItemId},${v.itemId},${v.tagId}")
|
||||
ins <- insertRows(table, all, tagFrag).update.run
|
||||
} yield ins
|
||||
|
||||
def findByItem(item: Ident): ConnectionIO[Vector[RTagItem]] =
|
||||
selectSimple(all, table, itemId.is(item)).query[RTagItem].to[Vector]
|
||||
|
||||
@ -76,4 +75,12 @@ object RTagItem {
|
||||
entities.map(v => fr"${v.tagItemId},${v.itemId},${v.tagId}")
|
||||
).update.run
|
||||
} yield n
|
||||
|
||||
def appendTags(item: Ident, tags: List[Ident]): ConnectionIO[Int] =
|
||||
for {
|
||||
existing <- findByItem(item)
|
||||
toadd = tags.toSet.diff(existing.map(_.tagId).toSet)
|
||||
n <- setAllTags(item, toadd.toSeq)
|
||||
} yield n
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user