Convert RSentMail

This commit is contained in:
Eike Kettner 2020-12-10 21:12:00 +01:00
parent 5cbf0d5602
commit fe4815c737
3 changed files with 60 additions and 50 deletions

View File

@ -21,7 +21,8 @@ object QMails {
def findMail(coll: Ident, mailId: Ident): ConnectionIO[Option[(RSentMail, Ident)]] = {
val iColl = RItem.Columns.cid.prefix("i")
val mId = RSentMail.Columns.id.prefix("m")
val smail = RSentMail.as("m")
val mId = smail.id.column
val (cols, from) = partialFind
@ -31,9 +32,11 @@ object QMails {
}
def findMails(coll: Ident, itemId: Ident): ConnectionIO[Vector[(RSentMail, Ident)]] = {
val smailitem = RSentMailItem.as("t")
val smail = RSentMail.as("m")
val iColl = RItem.Columns.cid.prefix("i")
val tItem = RSentMailItem.Columns.itemId.prefix("t")
val mCreated = RSentMail.Columns.created.prefix("m")
val tItem = smailitem.itemId.column
val mCreated = smail.created.column
val (cols, from) = partialFind
@ -46,15 +49,17 @@ object QMails {
private def partialFind: (Seq[Column], Fragment) = {
val user = RUser.as("u")
val smailitem = RSentMailItem.as("t")
val smail = RSentMail.as("m")
val iId = RItem.Columns.id.prefix("i")
val tItem = RSentMailItem.Columns.itemId.prefix("t")
val tMail = RSentMailItem.Columns.sentMailId.prefix("t")
val mId = RSentMail.Columns.id.prefix("m")
val mUser = RSentMail.Columns.uid.prefix("m")
val tItem = smailitem.itemId.column
val tMail = smailitem.sentMailId.column
val mId = smail.id.column
val mUser = smail.uid.column
val cols = RSentMail.Columns.all.map(_.prefix("m")) :+ user.login.column
val from = RSentMail.table ++ fr"m INNER JOIN" ++
RSentMailItem.table ++ fr"t ON" ++ tMail.is(mId) ++
val cols = smail.all.map(_.column) :+ user.login.column
val from = Fragment.const(smail.tableName) ++ fr"m INNER JOIN" ++
Fragment.const(smailitem.tableName) ++ fr"t ON" ++ tMail.is(mId) ++
fr"INNER JOIN" ++ RItem.table ++ fr"i ON" ++ tItem.is(iId) ++
fr"INNER JOIN" ++ Fragment.const(user.tableName) ++ fr"u ON" ++ user.uid.column.is(
mUser

View File

@ -7,8 +7,8 @@ import cats.implicits._
import fs2.Stream
import docspell.common._
import docspell.store.impl.Column
import docspell.store.impl.Implicits._
import docspell.store.qb.DSL._
import docspell.store.qb._
import doobie._
import doobie.implicits._
@ -78,18 +78,19 @@ object RSentMail {
si <- OptionT.liftF(RSentMailItem[ConnectionIO](itemId, sm.id, Some(sm.created)))
} yield (sm, si)
val table = fr"sentmail"
final case class Table(alias: Option[String]) extends TableDef {
object Columns {
val id = Column("id")
val uid = Column("uid")
val messageId = Column("message_id")
val sender = Column("sender")
val connName = Column("conn_name")
val subject = Column("subject")
val recipients = Column("recipients")
val body = Column("body")
val created = Column("created")
val tableName = "sentmail"
val id = Column[Ident]("id", this)
val uid = Column[Ident]("uid", this)
val messageId = Column[String]("message_id", this)
val sender = Column[MailAddress]("sender", this)
val connName = Column[Ident]("conn_name", this)
val subject = Column[String]("subject", this)
val recipients = Column[List[MailAddress]]("recipients", this)
val body = Column[String]("body", this)
val created = Column[Timestamp]("created", this)
val all = List(
id,
@ -104,27 +105,29 @@ object RSentMail {
)
}
import Columns._
private val T = Table(None)
def as(alias: String): Table =
Table(Some(alias))
def insert(v: RSentMail): ConnectionIO[Int] =
insertRow(
table,
all,
DML.insert(
T,
T.all,
sql"${v.id},${v.uid},${v.messageId},${v.sender},${v.connName},${v.subject},${v.recipients},${v.body},${v.created}"
).update.run
)
def findByUser(userId: Ident): Stream[ConnectionIO, RSentMail] =
selectSimple(all, table, uid.is(userId)).query[RSentMail].stream
run(select(T.all), from(T), T.uid === userId).query[RSentMail].stream
def delete(mailId: Ident): ConnectionIO[Int] =
deleteFrom(table, id.is(mailId)).update.run
DML.delete(T, T.id === mailId)
def deleteByItem(item: Ident): ConnectionIO[Int] =
for {
list <- RSentMailItem.findSentMailIdsByItem(item)
n1 <- RSentMailItem.deleteAllByItem(item)
n0 <- NonEmptyList.fromList(list.toList) match {
case Some(nel) => deleteFrom(table, id.isIn(nel)).update.run
case Some(nel) => DML.delete(T, T.id.in(nel))
case None => 0.pure[ConnectionIO]
}
} yield n0 + n1

View File

@ -4,8 +4,8 @@ import cats.effect._
import cats.implicits._
import docspell.common._
import docspell.store.impl.Column
import docspell.store.impl.Implicits._
import docspell.store.qb.DSL._
import docspell.store.qb._
import doobie._
import doobie.implicits._
@ -29,13 +29,13 @@ object RSentMailItem {
now <- created.map(_.pure[F]).getOrElse(Timestamp.current[F])
} yield RSentMailItem(id, itemId, sentmailId, now)
val table = fr"sentmailitem"
final case class Table(alias: Option[String]) extends TableDef {
val tableName = "sentmailitem"
object Columns {
val id = Column("id")
val itemId = Column("item_id")
val sentMailId = Column("sentmail_id")
val created = Column("created")
val id = Column[Ident]("id", this)
val itemId = Column[Ident]("item_id", this)
val sentMailId = Column[Ident]("sentmail_id", this)
val created = Column[Timestamp]("created", this)
val all = List(
id,
@ -45,21 +45,23 @@ object RSentMailItem {
)
}
import Columns._
private val T = Table(None)
def as(alias: String): Table =
Table(Some(alias))
def insert(v: RSentMailItem): ConnectionIO[Int] =
insertRow(
table,
all,
DML.insert(
T,
T.all,
sql"${v.id},${v.itemId},${v.sentMailId},${v.created}"
).update.run
)
def deleteMail(mailId: Ident): ConnectionIO[Int] =
deleteFrom(table, sentMailId.is(mailId)).update.run
DML.delete(T, T.sentMailId === mailId)
def findSentMailIdsByItem(item: Ident): ConnectionIO[Set[Ident]] =
selectSimple(Seq(sentMailId), table, itemId.is(item)).query[Ident].to[Set]
run(select(Seq(T.sentMailId)), from(T), T.itemId === item).query[Ident].to[Set]
def deleteAllByItem(item: Ident): ConnectionIO[Int] =
deleteFrom(table, itemId.is(item)).update.run
DML.delete(T, T.itemId === item)
}