Skip to content

Commit

Permalink
feat(pollux): Implement basic logic for credential schema update. #2
Browse files Browse the repository at this point in the history
  • Loading branch information
yshyn-iohk committed Mar 16, 2023
1 parent 3a09a2a commit 0c23eeb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ object CredentialSchemaService {

final case class RepositoryError(cause: Throwable) extends Error

final case class NotFoundError(guid: UUID) extends Error
final case class NotFoundError(guid: Option[UUID] = None, id: Option[UUID] = None, message: String) extends Error

object NotFoundError {
def byGuid(guid: UUID): NotFoundError =
NotFoundError(guid = Option(guid), message = s"Credential schema record cannot be found by `guid`=$guid")
def byId(id: UUID): NotFoundError =
NotFoundError(id = Option(id), message = s"Credential schema record cannot be found by `id`=$id")
}

final case class UpdateError(id: UUID, version: String, author: String, message: String) extends Error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.iohk.atala.pollux.core.model.CredentialSchema.FilteredEntries
import io.iohk.atala.pollux.core.repository.CredentialSchemaRepository
import io.iohk.atala.pollux.core.repository.Repository.SearchQuery
import io.iohk.atala.pollux.core.service.CredentialSchemaService.Error.*
import zio.ZIO.getOrFailWith
import zio.ZIO.{fail, getOrFailWith, succeed}
import zio.{Task, URLayer, ZLayer}

import java.util.UUID
Expand All @@ -25,7 +25,7 @@ class CredentialSchemaServiceImpl(
.getByGuid(guid)
.mapError[CredentialSchemaService.Error](t => RepositoryError(t))
.flatMap(
getOrFailWith(NotFoundError(guid))(_)
getOrFailWith(NotFoundError.byGuid(guid))(_)
)
}

Expand All @@ -37,22 +37,57 @@ class CredentialSchemaServiceImpl(
getByGUID(CredentialSchema.makeGUID(author, id, version))
}

// TODO: Implement a business logic for a real schema update
override def update(in: CredentialSchema.Input): Result[CredentialSchema] = {
override def update(
id: UUID,
in: CredentialSchema.Input
): Result[CredentialSchema] = {
for {
cs <- CredentialSchema.make(in)
updated_opt <- credentialSchemaRepository
.update(cs)
cs <- CredentialSchema.make(id, in)
existingVersions <- credentialSchemaRepository
.getAllVersions(id, in.author)
.mapError[CredentialSchemaService.Error](RepositoryError.apply)
_ <- existingVersions.headOption match {
case None =>
fail(NotFoundError.byId(id))
case _ =>
succeed(cs)
}
_ <- existingVersions.find(_ > in.version) match {
case Some(higherVersion) =>
fail(
UpdateError(
id,
in.version,
in.author,
s"Higher version is found: $higherVersion"
)
)
case None =>
succeed(cs)
}
_ <- existingVersions.find(_ == in.version) match {
case Some(existingVersion) =>
fail(
UpdateError(
id,
in.version,
in.author,
s"The version already exists: $existingVersion"
)
)
case None => succeed(cs)
}
updated <- credentialSchemaRepository
.create(cs)
.mapError[CredentialSchemaService.Error](RepositoryError.apply)
updated <- getOrFailWith(NotFoundError(cs.guid))(updated_opt)
} yield updated
}
override def delete(guid: UUID): Result[CredentialSchema] = {
for {
deleted_row_opt <- credentialSchemaRepository
.delete(guid)
.mapError(RepositoryError.apply)
deleted_row <- getOrFailWith(NotFoundError(guid))(deleted_row_opt)
deleted_row <- getOrFailWith(NotFoundError.byGuid(guid))(deleted_row_opt)
} yield deleted_row
}

Expand Down

0 comments on commit 0c23eeb

Please sign in to comment.