diff --git a/it-sqlserver/src/test/scala/org/mbari/oni/endpoints/SqlServerHistoryEndpointsSuite.scala b/it-sqlserver/src/test/scala/org/mbari/oni/endpoints/SqlServerHistoryEndpointsSuite.scala new file mode 100644 index 0000000..9603b03 --- /dev/null +++ b/it-sqlserver/src/test/scala/org/mbari/oni/endpoints/SqlServerHistoryEndpointsSuite.scala @@ -0,0 +1,7 @@ +package org.mbari.oni.endpoints + +import org.mbari.oni.SqlServerMixin + +class SqlServerHistoryEndpointsSuite extends HistoryEndpointsSuite with SqlServerMixin { + +} diff --git a/it/src/main/scala/org/mbari/oni/endpoints/HistoryEndpointsSuite.scala b/it/src/main/scala/org/mbari/oni/endpoints/HistoryEndpointsSuite.scala new file mode 100644 index 0000000..8b9a48b --- /dev/null +++ b/it/src/main/scala/org/mbari/oni/endpoints/HistoryEndpointsSuite.scala @@ -0,0 +1,38 @@ +package org.mbari.oni.endpoints + +import org.mbari.oni.domain.ExtendedHistory +import org.mbari.oni.jpa.DataInitializer +import sttp.model.StatusCode +import org.mbari.oni.etc.circe.CirceCodecs.{*, given} + +trait HistoryEndpointsSuite extends EndpointsSuite with DataInitializer: + + lazy val endpoints: HistoryEndpoints = HistoryEndpoints(entityManagerFactory) + + test("pending") { + init(3, 5) + runGet( + endpoints.pendingEndpointImpl, + "http://test.com/v1/history/pending", + response => { + assertEquals(response.code, StatusCode.Ok) + val histories = checkResponse[Seq[ExtendedHistory]](response.body) + assert(histories.nonEmpty) + } + ) + } + + test("approved") { + init(3, 5) + runGet( + endpoints.approvedEndpointsImpl, + "http://test.com/v1/history/approved", + response => { + assertEquals(response.code, StatusCode.Ok) + val histories = checkResponse[Seq[ExtendedHistory]](response.body) + assert(histories.nonEmpty) + } + ) + } + + diff --git a/oni/src/main/scala/org/mbari/oni/endpoints/Endpoints.scala b/oni/src/main/scala/org/mbari/oni/endpoints/Endpoints.scala index c7e0def..4170b8f 100644 --- a/oni/src/main/scala/org/mbari/oni/endpoints/Endpoints.scala +++ b/oni/src/main/scala/org/mbari/oni/endpoints/Endpoints.scala @@ -42,6 +42,7 @@ trait Endpoints: val log: System.Logger = System.getLogger(getClass.getName) // --- Schemas + implicit lazy val sExtendedHistory: Schema[ExtendedHistory] = Schema.derived[ExtendedHistory] implicit lazy val sLink: Schema[Link] = Schema.derived[Link] implicit lazy val sURI: Schema[URI] = Schema.string implicit lazy val sURL: Schema[URL] = Schema.string diff --git a/oni/src/main/scala/org/mbari/oni/endpoints/HistoryEndpoints.scala b/oni/src/main/scala/org/mbari/oni/endpoints/HistoryEndpoints.scala new file mode 100644 index 0000000..b1e66f3 --- /dev/null +++ b/oni/src/main/scala/org/mbari/oni/endpoints/HistoryEndpoints.scala @@ -0,0 +1,51 @@ +package org.mbari.oni.endpoints + +import jakarta.persistence.EntityManagerFactory +import org.mbari.oni.domain.{ErrorMsg, ExtendedHistory} +import org.mbari.oni.etc.circe.CirceCodecs.given +import org.mbari.oni.services.HistoryService +import sttp.tapir.* +import sttp.tapir.Endpoint +import sttp.tapir.json.circe.* +import sttp.tapir.server.ServerEndpoint +import sttp.tapir.server.nima.Id + +class HistoryEndpoints(entityManagerFactory: EntityManagerFactory) extends Endpoints: + + private val service = HistoryService(entityManagerFactory) + private val base = "history" + private val tag = "History" + + val pendingEndpoint: Endpoint[Unit, Unit, ErrorMsg, Seq[ExtendedHistory], Any] = openEndpoint + .get + .in(base / "pending") + .out(jsonBody[Seq[ExtendedHistory]]) + .name("pending") + .description("Get all pending change requests") + .tag(tag) + + val pendingEndpointImpl: ServerEndpoint[Any, Id] = pendingEndpoint.serverLogic { + _ => handleErrors(service.findAllPending()) + } + + val approvedEndpoints: Endpoint[Unit, Unit, ErrorMsg, Seq[ExtendedHistory], Any] = openEndpoint + .get + .in(base / "approved") + .out(jsonBody[Seq[ExtendedHistory]]) + .name("approved") + .description("Get all approved change requests") + .tag(tag) + + val approvedEndpointsImpl: ServerEndpoint[Any, Id] = approvedEndpoints.serverLogic { + _ => handleErrors(service.findAllApproved()) + } + + override def all: List[Endpoint[_, _, _, _, _]] = List( + approvedEndpoints, + pendingEndpoint + ) + + override def allImpl: List[ServerEndpoint[Any, Id]] = List( + approvedEndpointsImpl, + pendingEndpointImpl + ) diff --git a/oni/src/main/scala/org/mbari/oni/etc/circe/CirceCodecs.scala b/oni/src/main/scala/org/mbari/oni/etc/circe/CirceCodecs.scala index 1b07823..09cc30f 100644 --- a/oni/src/main/scala/org/mbari/oni/etc/circe/CirceCodecs.scala +++ b/oni/src/main/scala/org/mbari/oni/etc/circe/CirceCodecs.scala @@ -56,6 +56,9 @@ object CirceCodecs: given Decoder[Concept] = deriveDecoder given Encoder[Concept] = deriveEncoder + + given Decoder[ExtendedHistory] = deriveDecoder + given Encoder[ExtendedHistory] = deriveEncoder given Decoder[Link] = deriveDecoder given Encoder[Link] = deriveEncoder