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 pattern matching to its own function

Kacaii a1e80477 791eb5d3

+35 -27
+35 -27
src/app/domain/user/update_user_status.gleam
··· 29 29 id user_id: String, 30 30 ) -> wisp.Response { 31 31 use <- wisp.require_method(req, http.Put) 32 - use json_data <- wisp.require_json(req) 32 + use body <- wisp.require_json(req) 33 33 34 - case decode.run(json_data, body_decoder()) { 34 + case decode.run(body, decoder()) { 35 35 Error(err) -> web.handle_decode_error(err) 36 - Ok(is_active) -> 37 - case try_update_user_status(req:, ctx:, user_id:, is_active:) { 38 - Error(err) -> handle_error(req, err) 39 - Ok(resp) -> wisp.json_response(resp, 200) 40 - } 36 + Ok(is_active) -> handle_body(req, ctx, user_id, is_active) 37 + } 38 + } 39 + 40 + fn handle_body( 41 + req: wisp.Request, 42 + ctx: Context, 43 + user_id: String, 44 + is_active: Bool, 45 + ) -> wisp.Response { 46 + case try_update_user_status(req:, ctx:, user_id:, is_active:) { 47 + Error(err) -> handle_error(req, err) 48 + Ok(resp) -> wisp.json_response(resp, 200) 41 49 } 42 50 } 43 51 44 - ///  Updating an user status can fail 45 52 type UpdateUserStatusError { 46 53 ///  Error related to authentication / authorization 47 54 AccessControl(user.AccessControlError) 48 55 ///  User not found in the DataBase 49 - UserNotFound(String) 56 + NotFound(String) 50 57 /// 󱔼 UUID is not valid 51 58 InvalidUuid(String) 52 59 /// 󱘱 Failed to query the DataBase ··· 79 86 |> result.map_error(DataBase), 80 87 ) 81 88 82 - case list.first(returned.rows) { 83 - Error(_) -> Error(UserNotFound(user_id)) 84 - Ok(row) -> 85 - json.object([ 86 - #("id", json.string(uuid.to_string(row.id))), 87 - #("is_active", json.bool(row.is_active)), 88 - ]) 89 - |> json.to_string 90 - |> Ok 91 - } 89 + use row <- result.map( 90 + list.first(returned.rows) 91 + |> result.replace_error(NotFound(user_id)), 92 + ) 93 + 94 + [ 95 + #("id", json.string(uuid.to_string(row.id))), 96 + #("is_active", json.bool(row.is_active)), 97 + ] 98 + |> json.object 99 + |> json.to_string 92 100 } 93 101 94 102 fn handle_error(req: wisp.Request, err: UpdateUserStatusError) -> wisp.Response { 95 103 case err { 96 104 AccessControl(err) -> user.handle_access_control_error(req, err) 97 105 DataBase(err) -> web.handle_database_error(err) 98 - InvalidUuid(user_id) -> 99 - wisp.response(401) 100 - |> wisp.set_body(wisp.Text("Usuário possui UUID inválido: " <> user_id)) 101 - UserNotFound(id) -> { 102 - let resp = wisp.not_found() 106 + InvalidUuid(user_id) -> { 107 + let body = "Usuário possui UUID inválido: " <> user_id 108 + wisp.Text(body) 109 + |> wisp.set_body(wisp.response(401), _) 110 + } 111 + NotFound(id) -> { 103 112 let body = "Usuário não encontrado: " <> id 104 - 105 113 wisp.Text(body) 106 - |> wisp.set_body(resp, _) 114 + |> wisp.set_body(wisp.not_found(), _) 107 115 } 108 116 } 109 117 } 110 118 111 - fn body_decoder() -> decode.Decoder(Bool) { 119 + fn decoder() -> decode.Decoder(Bool) { 112 120 use is_active <- decode.field("status", decode.bool) 113 121 decode.success(is_active) 114 122 }