wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

:art: extract another pattern match to its own function

Kacaii ea16fdf3 a1e80477

+24 -17
+24 -17
src/app/domain/brigade/update_brigade_status.gleam
··· 32 32 let decoder = body_decoder() 33 33 case decode.run(data, decoder) { 34 34 Error(err) -> web.handle_decode_error(err) 35 - Ok(is_active) -> 36 - case try_update_status(ctx, brigade_id, is_active) { 37 - Ok(body) -> wisp.json_response(body, 200) 38 - Error(err) -> handle_error(err) 39 - } 35 + Ok(is_active) -> handle_body(ctx, brigade_id, is_active) 36 + } 37 + } 38 + 39 + fn handle_body( 40 + ctx: Context, 41 + brigade_id: String, 42 + is_active: Bool, 43 + ) -> wisp.Response { 44 + case try_update_status(ctx, brigade_id, is_active) { 45 + Ok(body) -> wisp.json_response(body, 200) 46 + Error(err) -> handle_error(err) 40 47 } 41 48 } 42 49 43 - ///  Updating a brigade status can fail 44 50 type UpdateBrigadeStatusError { 45 51 /// Brigade contains invalid Uuid 46 52 InvalidUuid(String) 47 53 /// An error occurred while accessing the DataBase 48 54 DataBase(pog.QueryError) 49 55 /// Brigade not found in the DataBase 50 - BrigadeNotFound(String) 56 + NotFound(String) 51 57 } 52 58 53 59 fn handle_error(err: UpdateBrigadeStatusError) -> wisp.Response { 54 60 case err { 55 61 InvalidUuid(id) -> wisp.bad_request("Equipe possui UUID Inválido: " <> id) 56 - BrigadeNotFound(id) -> wisp.bad_request("Equipe não encontrada: " <> id) 62 + NotFound(id) -> wisp.bad_request("Equipe não encontrada: " <> id) 57 63 DataBase(err) -> web.handle_database_error(err) 58 64 } 59 65 } ··· 64 70 is_active: Bool, 65 71 ) -> Result(String, UpdateBrigadeStatusError) { 66 72 use brigade_uuid <- result.try( 67 - uuid.from_string(id) |> result.replace_error(InvalidUuid(id)), 73 + uuid.from_string(id) 74 + |> result.replace_error(InvalidUuid(id)), 68 75 ) 69 76 70 77 use returned <- result.try( ··· 74 81 75 82 use row <- result.map( 76 83 list.first(returned.rows) 77 - |> result.replace_error(BrigadeNotFound(id)), 84 + |> result.replace_error(NotFound(id)), 78 85 ) 79 86 80 - json.to_string( 81 - json.object([ 82 - #("id", json.string(uuid.to_string(row.id))), 83 - #("is_active", json.bool(row.is_active)), 84 - #("updated_at", json.float(timestamp.to_unix_seconds(row.updated_at))), 85 - ]), 86 - ) 87 + [ 88 + #("id", json.string(uuid.to_string(row.id))), 89 + #("is_active", json.bool(row.is_active)), 90 + #("updated_at", json.float(timestamp.to_unix_seconds(row.updated_at))), 91 + ] 92 + |> json.object 93 + |> json.to_string 87 94 } 88 95 89 96 fn body_decoder() -> decode.Decoder(Bool) {