Skip to content

Commit

Permalink
Improve create organizaiton response
Browse files Browse the repository at this point in the history
  • Loading branch information
markholmes committed Jun 7, 2024
1 parent c6d8881 commit 33fe3fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/app/domains/organizations.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ pub fn organization_decoder(
}

/// Decodes the JSON blob from the POST to /organizations
pub fn new_organization_decoder(json: Dynamic) -> Result(NewOrganization, Nil) {
pub fn new_organization_decoder(
json: Dynamic,
) -> Result(NewOrganization, List(DecodeError)) {
let decoder =
dynamic.decode1(NewOrganization, dynamic.field("name", dynamic.string))

json
|> decoder()
|> result.nil_error()
}

// Routers ---------------------------------------------------------------------
Expand All @@ -64,29 +65,29 @@ pub fn one(req: Request, ctx: Context, id: String) -> Response {

pub fn create(req: Request, ctx: Context) -> Response {
use json <- wisp.require_json(req)
let maybe_organization = new_organization_decoder(json)
let organization = case maybe_organization {
Ok(organization) -> create_organization(ctx.db, organization)
Error(_) -> Error(InternalError)
}

let result = {
// Decode the JSON into a NewOrganization record.
use new_organization <- try(new_organization_decoder(json))

// Save the organization to the database.
use organization <- try(create_organization(ctx.db, new_organization))

// Construct a JSON payload with the id and name of the newly created organization.
// TODO: case on the organization result, return Ok or Error
Ok(
json.to_string_builder(
case organization {
Ok(organization) -> {
let data =
json.object([
#("id", json.int(organization.id)),
#("name", json.string(organization.name)),
]),
),
)
}
])

case result {
Ok(json) -> wisp.json_response(json, 201)
Error(Nil) -> wisp.unprocessable_entity()
let response =
json.to_string_builder(
json.object([#("status", json.string("success")), #("data", data)]),
)

wisp.json_response(response, 200)
}

Error(_) -> wisp.internal_server_error()
}
}

Expand Down Expand Up @@ -142,7 +143,7 @@ pub fn show(ctx: Context, id: String) -> Response {
wisp.json_response(response, 404)
}

Error(InternalError) -> wisp.internal_server_error()
Error(_) -> wisp.internal_server_error()
}
}

Expand All @@ -151,7 +152,7 @@ pub fn show(ctx: Context, id: String) -> Response {
pub fn create_organization(
connection: Connection,
organization: NewOrganization,
) -> Result(Organization, Nil) {
) -> Result(Organization, AppErrors) {
let sql =
"
INSERT INTO organizations (name)
Expand All @@ -169,7 +170,7 @@ pub fn create_organization(

case returned {
Ok(Returned(_, [organization, ..])) -> Ok(organization)
_ -> Error(Nil)
_ -> Error(InternalError)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/app/web.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub type Context {

pub type AppErrors {
NotFound(id: String)
UniqueConstraint(entity: String)
InternalError
}

Expand Down

0 comments on commit 33fe3fd

Please sign in to comment.