Skip to content

Commit

Permalink
Added history
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed May 14, 2024
1 parent 3814278 commit ca17dfd
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.mbari.oni.endpoints

import org.mbari.oni.SqlServerMixin

class SqlServerHistoryEndpointsSuite extends HistoryEndpointsSuite with SqlServerMixin {

}
Original file line number Diff line number Diff line change
@@ -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)
}
)
}


1 change: 1 addition & 0 deletions oni/src/main/scala/org/mbari/oni/endpoints/Endpoints.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions oni/src/main/scala/org/mbari/oni/endpoints/HistoryEndpoints.scala
Original file line number Diff line number Diff line change
@@ -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
)
3 changes: 3 additions & 0 deletions oni/src/main/scala/org/mbari/oni/etc/circe/CirceCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca17dfd

Please sign in to comment.