mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Adopt store module to new collective table
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
Reference in New Issue
Block a user