Last active
July 22, 2022 20:32
-
-
Save gabfssilva/1d884b7d290be1ae1ae32288b0241c4e to your computer and use it in GitHub Desktop.
simple http api brainstorming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import library.* | |
import library.headers.* | |
import library.json.* | |
class OrderControllerV1(repository: OrderRepository): | |
@get( | |
description = "Fetch user by id", | |
path = "/api/v1/orders/{orderId}", | |
produces = "application/json" | |
) | |
def fetchById( | |
@pathSegment orderId: UUID | |
): Response.Ok[Order, _] | Response.NotFound[String, _] = | |
repository.fetch(id) match | |
case None => notFound("order not found") | |
case Some(v) => ok(v) | |
@post( | |
description = "Crete order", | |
path = "/api/v1/orders", | |
produces = "application/json" | |
) | |
def fetchById( | |
createOrder: CreateOrder | |
): Response.Created[Order, (Location)] = //RawHeader["Header-Name"] | |
val createdOrder = repository.create(order) | |
created() | |
.withHeader(Location(s"/api/v1/orders/${createdOrder.id}")) | |
//Just an example on how to respond headers | |
type Header1 = RawHeader["Header1"] | |
type Header2 = RawHeader["Header2"] | |
@put( | |
description = "Update order", | |
path = "/api/v1/orders/{orderId}", | |
produces = "application/json" | |
) | |
def fetchById( | |
@pathSegment orderId: UUID, | |
createOrder: CreateOrder | |
): Response.Ok[Order, (Header1, Header2)] | Response.NotFound[String, _] = | |
repository.update(orderId, order) match | |
case None => notFound("order not found") | |
case Some(updatedOrder) => | |
ok(updatedOrder) //Response.Ok[Order, _] | |
.withHeader(Header1("Just an example")) //Response.Ok[Order, (Header1)] | |
.withHeader(Header2("Another example")) //Response.Ok[Order, (Header1, Header2)] | |
end OrderControllerV1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment