Skip to content

Commit

Permalink
Deal with ResourceRef
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski committed Mar 6, 2024
1 parent 700656e commit c413896
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object ResourceResolution {
apply(
aclCheck,
resolvers,
FetchResource(xas).latest(_, _),
FetchResource(xas).fetch(_, _),
Permissions.resources.read,
excludeDeprecated
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import doobie.{Get, Put}

trait FetchResource {

def latest(ref: ResourceRef, project: ProjectRef): FetchF[Resource]
/** Fetch the referenced resource in the given project */
def fetch(ref: ResourceRef, project: ProjectRef): FetchF[Resource]

}

Expand All @@ -20,10 +21,13 @@ object FetchResource {
implicit private val getValue: Get[ResourceState] = ResourceState.serializer.getValue
implicit private val putId: Put[Iri] = ResourceState.serializer.putId

def apply(xas: Transactors): FetchResource = (ref: ResourceRef, project: ProjectRef) =>
ScopedStateGet
.latest(Resources.entityType, project, ref.iri)
.transact(xas.read)
.map(_.map(_.toResource))
def apply(xas: Transactors): FetchResource = (ref: ResourceRef, project: ProjectRef) => {
val fetch = ref match {
case ResourceRef.Latest(iri) => ScopedStateGet.latest(Resources.entityType, project, iri)
case ResourceRef.Revision(_, iri, rev) => ScopedStateGet.rev(Resources.entityType, project, iri, rev)
case ResourceRef.Tag(_, iri, tag) => ScopedStateGet.tag(Resources.entityType, project, iri, tag)
}
fetch.transact(xas.read).map(_.map(_.toResource))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ import doobie.{ConnectionIO, Get, Put}
object ScopedStateGet {

def apply[Id: Put, S: Get](tpe: EntityType, project: ProjectRef, id: Id, tag: Tag): ConnectionIO[Option[S]] =
sql"""SELECT value FROM scoped_states WHERE type = $tpe AND org = ${project.organization} AND project = ${project.project} AND id = $id AND tag = $tag"""
sql"""SELECT value FROM scoped_states WHERE type = $tpe AND org = ${project.organization} AND project = ${project.project} AND id = $id AND tag = $tag"""
.query[S]
.option

def apply[Id: Put, S: Get](tpe: EntityType, project: ProjectRef, id: Id, rev: Int): ConnectionIO[Option[S]] =
sql"""SELECT value FROM scoped_states WHERE type = $tpe AND org = ${project.organization} AND project = ${project.project} AND id = $id AND rev = $rev"""
.query[S]
.option

def latest[Id: Put, S: Get](tpe: EntityType, project: ProjectRef, id: Id): ConnectionIO[Option[S]] =
apply(tpe, project, id, Latest)

def tag[Id: Put, S: Get](tpe: EntityType, project: ProjectRef, id: Id, tag: Tag): ConnectionIO[Option[S]] =
apply(tpe, project, id, tag)

def rev[Id: Put, S: Get](tpe: EntityType, project: ProjectRef, id: Id, rev: Int): ConnectionIO[Option[S]] =
apply(tpe, project, id, rev)

}

0 comments on commit c413896

Please sign in to comment.