Adopt store module to new collective table

This commit is contained in:
eikek
2022-07-05 21:17:18 +02:00
parent 35882fce84
commit 77f22bb5ea
65 changed files with 783 additions and 635 deletions

View File

@ -0,0 +1,48 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package docspell.common
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
final case class AccountInfo(
collectiveId: CollectiveId,
collective: Ident,
userId: Ident,
login: Ident
) {
def asAccountId: AccountId =
AccountId(collective, login)
def asString: String =
s"${collectiveId.value}/${collective.id}/${userId.id}/${login.id}"
}
object AccountInfo {
implicit val jsonDecoder: Decoder[AccountInfo] = deriveDecoder
implicit val jsonEncoder: Encoder[AccountInfo] = deriveEncoder
def parse(str: String): Either[String, AccountInfo] = {
val input = str.replaceAll("\\s+", "").trim
val invalid: Either[String, AccountInfo] =
Left(s"Cannot parse account info: $str")
input.split('/').toList match {
case collId :: collName :: userId :: login :: Nil =>
for {
cid <- collId.toLongOption.toRight(s"Invalid collective id: $collId")
cn <- Ident.fromString(collName)
uid <- Ident.fromString(userId)
un <- Ident.fromString(login)
} yield AccountInfo(CollectiveId(cid), cn, uid, un)
case _ =>
invalid
}
}
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package docspell.common
import io.circe.{Decoder, Encoder}
final class CollectiveId(val value: Long) extends AnyVal {
override def toString =
s"CollectiveId($value)"
}
object CollectiveId {
val unknown: CollectiveId = CollectiveId(-1)
def apply(n: Long): CollectiveId = new CollectiveId(n)
implicit val jsonEncoder: Encoder[CollectiveId] =
Encoder.encodeLong.contramap(_.value)
implicit val jsonDecoder: Decoder[CollectiveId] =
Decoder.decodeLong.map(CollectiveId.apply)
}