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.

:recycle: name db queries `query_database` by default

Kacaii 1a46874a ce1fcb63

+82 -90
+2 -2
src/app/domain/admin/admin_update_user.gleam
··· 49 49 body: RequestBody, 50 50 user_id: String, 51 51 ) -> wisp.Response { 52 - case try_update_user(req, ctx, body, user_id) { 52 + case query_database(req, ctx, body, user_id) { 53 53 Ok(body) -> wisp.json_response(body, 200) 54 54 Error(err) -> handle_error(req, body, err) 55 55 } ··· 112 112 } 113 113 } 114 114 115 - fn try_update_user( 115 + fn query_database( 116 116 req: wisp.Request, 117 117 ctx: Context, 118 118 body: RequestBody,
+2 -2
src/app/domain/brigade/register_new_brigade.gleam
··· 93 93 ctx ctx: Context, 94 94 body body: RequestBody, 95 95 ) -> wisp.Response { 96 - case try_register_brigade(request, ctx, body) { 96 + case query_database(request, ctx, body) { 97 97 Error(err) -> handle_error(request, err) 98 98 Ok(body) -> wisp.json_response(body, 201) 99 99 } 100 100 } 101 101 102 - fn try_register_brigade( 102 + fn query_database( 103 103 request request: wisp.Request, 104 104 ctx ctx: Context, 105 105 body body: RequestBody,
+2 -2
src/app/domain/brigade/update_brigade_status.gleam
··· 41 41 brigade_id: String, 42 42 is_active: Bool, 43 43 ) -> wisp.Response { 44 - case try_update_status(ctx, brigade_id, is_active) { 44 + case query_database(ctx, brigade_id, is_active) { 45 45 Ok(body) -> wisp.json_response(body, 200) 46 46 Error(err) -> handle_error(err) 47 47 } ··· 64 64 } 65 65 } 66 66 67 - fn try_update_status( 67 + fn query_database( 68 68 ctx: Context, 69 69 id: String, 70 70 is_active: Bool,
+2 -2
src/app/domain/notification/get_notification_preferences.gleam
··· 30 30 ) -> wisp.Response { 31 31 use <- wisp.require_method(req, http.Get) 32 32 33 - case try_query_database(req, ctx) { 33 + case query_database(req, ctx) { 34 34 Error(err) -> handle_error(err) 35 35 Ok(body) -> wisp.json_response(body, 200) 36 36 } ··· 43 43 } 44 44 } 45 45 46 - fn try_query_database( 46 + fn query_database( 47 47 req: wisp.Request, 48 48 ctx: Context, 49 49 ) -> Result(String, GetNotificationPreferencesError) {
+2 -2
src/app/domain/notification/update_notification_preferences.gleam
··· 51 51 ctx: Context, 52 52 data: dict.Dict(category.Category, Bool), 53 53 ) -> wisp.Response { 54 - case try_update_preferences(req, ctx, data) { 54 + case query_database(req, ctx, data) { 55 55 Error(err) -> handle_error(err) 56 56 Ok(updated_values) -> 57 57 json.dict(updated_values, category.to_string_pt_br, json.bool) ··· 87 87 } 88 88 } 89 89 90 - fn try_update_preferences( 90 + fn query_database( 91 91 req: wisp.Request, 92 92 ctx: Context, 93 93 preferences: dict.Dict(category.Category, Bool),
+2 -2
src/app/domain/occurrence/delete_occurrence.gleam
··· 39 39 ) -> wisp.Response { 40 40 use <- wisp.require_method(req, http.Delete) 41 41 42 - case try_delete_occurrence(req, ctx, occurrence_id_str) { 42 + case query_database(req, ctx, occurrence_id_str) { 43 43 Ok(deleted_id) -> wisp.json_response(deleted_id, 200) 44 44 Error(err) -> handle_error(err) 45 45 } 46 46 } 47 47 48 - fn try_delete_occurrence( 48 + fn query_database( 49 49 req: wisp.Request, 50 50 ctx: Context, 51 51 id_str: String,
+4 -4
src/app/domain/occurrence/update_occurrence_status.gleam
··· 43 43 case req.method { 44 44 // Mark an occurrence as resolved 45 45 http.Post -> 46 - case try_resolve_occurrence(req, ctx, occurrence_id) { 46 + case resolve_occurrence(req, ctx, occurrence_id) { 47 47 Error(err) -> handle_error(err) 48 48 Ok(body) -> wisp.json_response(body, 200) 49 49 } 50 50 51 51 // Reopen a resolved occurrence 52 52 http.Delete -> 53 - case try_reopen_occurrence(req, ctx, occurrence_id) { 53 + case reopen_occurrence(req, ctx, occurrence_id) { 54 54 Error(err) -> handle_error(err) 55 55 Ok(body) -> wisp.json_response(body, 200) 56 56 } ··· 59 59 } 60 60 } 61 61 62 - fn try_resolve_occurrence( 62 + fn resolve_occurrence( 63 63 req: wisp.Request, 64 64 ctx: Context, 65 65 occ_id: String, ··· 111 111 ) 112 112 } 113 113 114 - fn try_reopen_occurrence( 114 + fn reopen_occurrence( 115 115 req: wisp.Request, 116 116 ctx: Context, 117 117 occ_id: String,
+2 -2
src/app/domain/user/delete_user.gleam
··· 26 26 ) -> wisp.Response { 27 27 use <- wisp.require_method(req, http.Delete) 28 28 29 - case try_delete_user(req, ctx, user_id) { 29 + case query_database(req, ctx, user_id) { 30 30 Ok(deleted_user) -> wisp.json_response(deleted_user, 200) 31 31 Error(err) -> handle_error(req, err) 32 32 } ··· 58 58 } 59 59 } 60 60 61 - fn try_delete_user( 61 + fn query_database( 62 62 req: wisp.Request, 63 63 ctx: Context, 64 64 target_id: String,
+2 -6
src/app/domain/user/get_all_user_profiles.gleam
··· 37 37 ) -> wisp.Response { 38 38 use <- wisp.require_method(req, http.Get) 39 39 40 - // 󰡦 Find available users 41 - case try_query_database(req, ctx) { 42 - // 󰨮 Handle possible errors 40 + case query_database(req, ctx) { 43 41 Error(err) -> handle_error(req, err) 44 - //  Send the data to the client 45 42 Ok(body) -> wisp.json_response(body, 200) 46 43 } 47 44 } 48 45 49 - /// 󰀖 Gathering the user list can fail 50 46 type GetAllUsersError { 51 47 ///  Errors related to user authentication / authorization 52 48 AccessControl(user.AccessControlError) ··· 61 57 } 62 58 } 63 59 64 - fn try_query_database( 60 + fn query_database( 65 61 req: wisp.Request, 66 62 ctx: Context, 67 63 ) -> Result(String, GetAllUsersError) {
+12 -14
src/app/domain/user/get_crew_members.gleam
··· 1 - //// Handler for retrieving members from the same brigade as a given user. 1 + //// Handler for retrieving members from the same brigades as a given user. 2 2 3 3 import app/domain/role 4 4 import app/domain/user/sql ··· 45 45 ) -> wisp.Response { 46 46 use <- wisp.require_method(req, http.Get) 47 47 48 - case try_query_database(ctx:, user_id:) { 48 + case query_database(ctx, user_id) { 49 49 Ok(body) -> wisp.json_response(body, 200) 50 50 Error(err) -> handle_err(err) 51 51 } ··· 58 58 DataBase(pog.QueryError) 59 59 } 60 60 61 - fn try_query_database( 61 + fn query_database( 62 62 ctx ctx: Context, 63 63 user_id user_id: String, 64 64 ) -> Result(String, GetCrewMembersError) { ··· 78 78 } 79 79 80 80 fn row_to_json(row: sql.QueryCrewMembersRow) -> json.Json { 81 - let role_name = 82 - case row.user_role { 83 - sql.Admin -> role.Admin 84 - sql.Analyst -> role.Analyst 85 - sql.Captain -> role.Captain 86 - sql.Developer -> role.Developer 87 - sql.Firefighter -> role.Firefighter 88 - sql.Sargeant -> role.Sargeant 89 - } 90 - |> role.to_string_pt_br 81 + let user_role = case row.user_role { 82 + sql.Admin -> role.Admin 83 + sql.Analyst -> role.Analyst 84 + sql.Captain -> role.Captain 85 + sql.Developer -> role.Developer 86 + sql.Firefighter -> role.Firefighter 87 + sql.Sargeant -> role.Sargeant 88 + } 91 89 92 90 json.object([ 93 91 #("id", json.string(uuid.to_string(row.id))), 94 92 #("full_name", json.string(row.full_name)), 95 - #("user_role", json.string(role_name)), 93 + #("user_role", json.string(role.to_string_pt_br(user_role))), 96 94 #("brigade_id", json.string(uuid.to_string(row.brigade_id))), 97 95 ]) 98 96 }
+2 -2
src/app/domain/user/login.gleam
··· 87 87 data data: RequestBody, 88 88 cookie_name cookie_name: String, 89 89 ) -> response.Response(wisp.Body) { 90 - case query_login_token(data:, ctx:) { 90 + case query_database(data:, ctx:) { 91 91 Error(err) -> handle_error(err) 92 92 Ok(resp) -> { 93 93 log_login(data) ··· 145 145 146 146 ///  Check if the provided password matches the one inside our database 147 147 /// Returns the user's UUID if successfull. 148 - fn query_login_token( 148 + fn query_database( 149 149 data data: RequestBody, 150 150 ctx ctx: Context, 151 151 ) -> Result(LoginToken, LoginError) {
+44 -46
src/app/domain/user/signup.gleam
··· 44 44 } 45 45 46 46 fn handle_body(req: wisp.Request, body: SignUp, ctx: Context) -> wisp.Response { 47 - case try_insert_into_database(request: req, ctx:, signup: body) { 47 + case query_database(request: req, ctx:, signup: body) { 48 + Error(err) -> handle_error(req, err) 48 49 Ok(new_user) -> { 49 50 log_signup(body) 50 51 wisp.json_response(new_user, 201) 51 52 } 52 - 53 - //  Server errors ---------------------------------------------------- 54 - Error(err) -> handle_error(req, err) 55 53 } 56 54 } 57 55 58 56 /// 󰆼 Inserts the user in the database. 59 57 /// 󱔼 Hashes the user `password` before inserting. 60 - fn try_insert_into_database( 58 + fn query_database( 61 59 request request: wisp.Request, 62 60 signup data: SignUp, 63 61 ctx ctx: Context, ··· 83 81 |> result.map_error(InvalidRole), 84 82 ) 85 83 84 + let role_enum = case user_role { 85 + role.Admin -> sql.Admin 86 + role.Analyst -> sql.Analyst 87 + role.Captain -> sql.Captain 88 + role.Developer -> sql.Developer 89 + role.Firefighter -> sql.Firefighter 90 + role.Sargeant -> sql.Sargeant 91 + } 92 + 86 93 use returned <- result.try( 87 94 sql.insert_new_user( 88 95 ctx.db, ··· 91 98 data.phone_number, 92 99 data.email, 93 100 hashed_password.encoded_hash, 94 - role_to_enum(user_role), 101 + role_enum, 95 102 ) 96 103 |> result.map_error(DataBase), 97 104 ) ··· 101 108 |> result.replace_error(MissingSignupConfirmation), 102 109 ) 103 110 104 - json.object([#("id", json.string(uuid.to_string(row.id)))]) 111 + let id = uuid.to_string(row.id) 112 + json.object([#("id", json.string(id))]) 105 113 |> json.to_string 106 114 } 107 115 ··· 122 130 use name <- form.field("nome", { 123 131 form.parse_string |> form.check_not_empty() 124 132 }) 133 + 125 134 use registration <- form.field("matricula", { 126 135 form.parse_string |> form.check_not_empty() 127 136 }) 137 + 128 138 use phone_number <- form.field("telefone", { 129 139 form.parse_phone_number |> form.check_not_empty() 130 140 }) 141 + 131 142 use email <- form.field("email", { 132 143 form.parse_email |> form.check_not_empty() 133 144 }) 145 + 134 146 use password <- form.field("senha", { 135 147 form.parse_string |> form.check_not_empty() 136 148 }) 149 + 137 150 use _ <- form.field("confirma_senha", { 138 151 form.parse_string |> form.check_confirms(password) 139 152 }) 153 + 140 154 use user_role <- form.field("cargo", { 141 155 form.parse_string |> form.check_not_empty() 142 156 }) ··· 166 180 fn handle_database_error(err: pog.QueryError) -> wisp.Response { 167 181 case err { 168 182 pog.ConstraintViolated(_, _, constraint: "user_account_registration_key") -> { 169 - let resp = wisp.response(409) 170 - let body = wisp.Text("Matrícula já cadastrada. Experimente fazer login") 171 - 172 - wisp.set_body(resp, body) 183 + "Matrícula já cadastrada. Experimente fazer login" 184 + |> wisp.Text 185 + |> wisp.set_body(wisp.response(409), _) 173 186 } 174 187 175 188 pog.ConstraintViolated(_, _, constraint: "user_account_email_key") -> { 176 - let resp = wisp.response(409) 177 - let body = 178 - wisp.Text("Email já cadastrado. Por favor, utilize um diferente") 179 - 180 - wisp.set_body(resp, body) 189 + "Email já cadastrado. Por favor, utilize um diferente" 190 + |> wisp.Text 191 + |> wisp.set_body(wisp.response(409), _) 181 192 } 182 193 183 194 pog.ConstraintViolated(_, _, constraint: "user_account_phone_key") -> { 184 - let resp = wisp.response(409) 185 - let body = 186 - wisp.Text("Telefone já cadastrado. Por favor, utilize um diferente") 187 - 188 - wisp.set_body(resp, body) 195 + "Telefone já cadastrado. Por favor, utilize um diferente" 196 + |> wisp.Text 197 + |> wisp.set_body(wisp.response(409), _) 189 198 } 190 199 191 200 err -> web.handle_database_error(err) ··· 194 203 195 204 fn handle_error(req: wisp.Request, err: SignupError) { 196 205 case err { 206 + AccessControl(err) -> user.handle_access_control_error(req, err) 207 + DataBase(err) -> handle_database_error(err) 208 + 197 209 HashError -> { 198 - let body = 199 - "Ocorreu um erro ao encriptografar a senha do usuário" 200 - |> wisp.Text 210 + "Ocorreu um erro ao encriptografar a senha do usuário" 211 + |> wisp.Text 212 + |> wisp.set_body(wisp.internal_server_error(), _) 213 + } 201 214 202 - wisp.internal_server_error() 203 - |> wisp.set_body(body) 215 + InvalidRole(unknown) -> { 216 + let body = "O novo usuário possui um cargo inválido: " <> unknown 217 + wisp.bad_request(body) 204 218 } 205 - DataBase(err) -> handle_database_error(err) 206 - InvalidRole(unknown) -> 207 - wisp.bad_request("O novo usuário possui um cargo inválido: " <> unknown) 208 - AccessControl(err) -> user.handle_access_control_error(req, err) 219 + 209 220 MissingSignupConfirmation -> 210 - wisp.internal_server_error() 211 - |> wisp.set_body(wisp.Text( 212 - "Não foi possível confirmar a inserção do novo usuário no sistema", 213 - )) 221 + "Não foi possível confirmar a inserção do novo usuário no sistema" 222 + |> wisp.Text 223 + |> wisp.set_body(wisp.internal_server_error(), _) 214 224 } 215 225 } 216 226 217 - ///  Signup can fail 218 227 type SignupError { 219 228 /// 󱔼 Hashing went wrong 220 229 HashError ··· 227 236 /// 󰡦 Database didnt return information about the new user 228 237 MissingSignupConfirmation 229 238 } 230 - 231 - fn role_to_enum(user_role: role.Role) -> sql.UserRoleEnum { 232 - case user_role { 233 - role.Admin -> sql.Admin 234 - role.Analyst -> sql.Analyst 235 - role.Captain -> sql.Captain 236 - role.Developer -> sql.Developer 237 - role.Firefighter -> sql.Firefighter 238 - role.Sargeant -> sql.Sargeant 239 - } 240 - }
+2 -2
src/app/domain/user/update_user_profile.gleam
··· 41 41 ctx: Context, 42 42 body: RequestBody, 43 43 ) -> wisp.Response { 44 - case try_update_user(req, ctx, body) { 44 + case query_database(req, ctx, body) { 45 45 Error(err) -> handle_error(err) 46 46 Ok(resp) -> wisp.json_response(resp, 200) 47 47 } ··· 77 77 } 78 78 } 79 79 80 - fn try_update_user( 80 + fn query_database( 81 81 req: wisp.Request, 82 82 ctx: Context, 83 83 body: RequestBody,
+2 -2
src/app/domain/user/update_user_status.gleam
··· 43 43 user_id: String, 44 44 is_active: Bool, 45 45 ) -> wisp.Response { 46 - case try_update_user_status(req:, ctx:, user_id:, is_active:) { 46 + case query_database(req, ctx, user_id, is_active) { 47 47 Error(err) -> handle_error(req, err) 48 48 Ok(resp) -> wisp.json_response(resp, 200) 49 49 } ··· 60 60 DataBase(pog.QueryError) 61 61 } 62 62 63 - fn try_update_user_status( 63 + fn query_database( 64 64 req req: wisp.Request, 65 65 ctx ctx: Context, 66 66 user_id user_id: String,