Basic management of shares

This commit is contained in:
eikek
2021-10-02 15:16:02 +02:00
parent de1baf725f
commit c7d587bea4
27 changed files with 1551 additions and 20 deletions

View File

@ -0,0 +1,13 @@
CREATE TABLE "item_share" (
"id" varchar(254) not null primary key,
"cid" varchar(254) not null,
"name" varchar(254),
"query" varchar(2000) not null,
"enabled" boolean not null,
"pass" varchar(254),
"publish_at" timestamp not null,
"publish_until" timestamp not null,
"views" int not null,
"last_access" timestamp,
foreign key ("cid") references "collective"("cid") on delete cascade
)

View File

@ -0,0 +1,13 @@
CREATE TABLE `item_share` (
`id` varchar(254) not null primary key,
`cid` varchar(254) not null,
`name` varchar(254),
`query` varchar(2000) not null,
`enabled` boolean not null,
`pass` varchar(254),
`publish_at` timestamp not null,
`publish_until` timestamp not null,
`views` int not null,
`last_access` timestamp,
foreign key (`cid`) references `collective`(`cid`) on delete cascade
)

View File

@ -0,0 +1,13 @@
CREATE TABLE "item_share" (
"id" varchar(254) not null primary key,
"cid" varchar(254) not null,
"name" varchar(254),
"query" varchar(2000) not null,
"enabled" boolean not null,
"pass" varchar(254),
"publish_at" timestamp not null,
"publish_until" timestamp not null,
"views" int not null,
"last_access" timestamp,
foreign key ("cid") references "collective"("cid") on delete cascade
)

View File

@ -9,6 +9,7 @@ package docspell.store
import scala.concurrent.ExecutionContext
import cats.effect._
import cats.~>
import fs2._
import docspell.store.file.FileStore
@ -19,6 +20,7 @@ import doobie._
import doobie.hikari.HikariTransactor
trait Store[F[_]] {
def transform: ConnectionIO ~> F
def transact[A](prg: ConnectionIO[A]): F[A]

View File

@ -6,8 +6,10 @@
package docspell.store.impl
import cats.arrow.FunctionK
import cats.effect.Async
import cats.implicits._
import cats.~>
import docspell.store.file.FileStore
import docspell.store.migrate.FlywayMigrate
@ -22,6 +24,9 @@ final class StoreImpl[F[_]: Async](
xa: Transactor[F]
) extends Store[F] {
def transform: ConnectionIO ~> F =
FunctionK.lift(transact)
def migrate: F[Int] =
FlywayMigrate.run[F](jdbc).map(_.migrationsExecuted)

View File

@ -6,20 +6,25 @@
package docspell.store.records
import cats.data.NonEmptyList
import cats.data.{NonEmptyList, OptionT}
import docspell.common._
import docspell.query.ItemQuery
import docspell.store.qb.DSL._
import docspell.store.qb._
import doobie._
import doobie.implicits._
final case class RShare(
id: Ident,
cid: Ident,
name: Option[String],
query: ItemQuery,
enabled: Boolean,
password: Option[Password],
publishedAt: Timestamp,
publishedUntil: Timestamp,
publishAt: Timestamp,
publishUntil: Timestamp,
views: Int,
lastAccess: Option[Timestamp]
) {}
@ -31,11 +36,12 @@ object RShare {
val id = Column[Ident]("id", this)
val cid = Column[Ident]("cid", this)
val name = Column[String]("name", this)
val query = Column[ItemQuery]("query", this)
val enabled = Column[Boolean]("enabled", this)
val password = Column[Password]("password", this)
val publishedAt = Column[Timestamp]("published_at", this)
val publishedUntil = Column[Timestamp]("published_until", this)
val password = Column[Password]("pass", this)
val publishedAt = Column[Timestamp]("publish_at", this)
val publishedUntil = Column[Timestamp]("publish_until", this)
val views = Column[Int]("views", this)
val lastAccess = Column[Timestamp]("last_access", this)
@ -43,6 +49,7 @@ object RShare {
NonEmptyList.of(
id,
cid,
name,
query,
enabled,
password,
@ -56,4 +63,47 @@ object RShare {
val T: Table = Table(None)
def as(alias: String): Table = Table(Some(alias))
def insert(r: RShare): ConnectionIO[Int] =
DML.insert(
T,
T.all,
fr"${r.id},${r.cid},${r.name},${r.query},${r.enabled},${r.password},${r.publishAt},${r.publishUntil},${r.views},${r.lastAccess}"
)
def incAccess(id: Ident): ConnectionIO[Int] =
for {
curTime <- Timestamp.current[ConnectionIO]
n <- DML.update(
T,
T.id === id,
DML.set(T.views.increment(1), T.lastAccess.setTo(curTime))
)
} yield n
def updateData(r: RShare, removePassword: Boolean): ConnectionIO[Int] =
DML.update(
T,
T.id === r.id && T.cid === r.cid,
DML.set(
T.name.setTo(r.name),
T.query.setTo(r.query),
T.enabled.setTo(r.enabled),
T.publishedUntil.setTo(r.publishUntil)
) ++ (if (r.password.isDefined || removePassword)
List(T.password.setTo(r.password))
else Nil)
)
def findOne(id: Ident, cid: Ident): OptionT[ConnectionIO, RShare] =
OptionT(
Select(select(T.all), from(T), T.id === id && T.cid === cid).build
.query[RShare]
.option
)
def findAllByCollective(cid: Ident): ConnectionIO[List[RShare]] =
Select(select(T.all), from(T), T.cid === cid).build.query[RShare].to[List]
def deleteByIdAndCid(id: Ident, cid: Ident): ConnectionIO[Int] =
DML.delete(T, T.id === id && T.cid === cid)
}