wip: currently rewriting the project as a full stack application
tangled.org/kacaii.dev/sigo
gleam
1//// This module contains the code to run the sql queries defined in
2//// `./src/app/domain/brigade/sql`.
3//// > 🐿️ This module was generated automatically using v4.6.0 of
4//// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
5////
6
7import gleam/dynamic/decode
8import gleam/option.{type Option}
9import gleam/time/timestamp.{type Timestamp}
10import pog
11import youid/uuid.{type Uuid}
12
13/// A row you get from running the `assign_brigade_members` query
14/// defined in `./src/app/domain/brigade/sql/assign_brigade_members.sql`.
15///
16/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
17/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
18///
19pub type AssignBrigadeMembersRow {
20 AssignBrigadeMembersRow(inserted_user_id: Uuid)
21}
22
23/// Assign a list of members to a brigade
24///
25/// > 🐿️ This function was generated automatically using v4.6.0 of
26/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
27///
28pub fn assign_brigade_members(
29 db: pog.Connection,
30 arg_1: Uuid,
31 arg_2: List(Uuid),
32) -> Result(pog.Returned(AssignBrigadeMembersRow), pog.QueryError) {
33 let decoder = {
34 use inserted_user_id <- decode.field(0, uuid_decoder())
35 decode.success(AssignBrigadeMembersRow(inserted_user_id:))
36 }
37
38 "-- Assign a list of members to a brigade
39select b.inserted_user_id
40from public.assign_brigade_members($1, $2) as b;
41"
42 |> pog.query
43 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
44 |> pog.parameter(pog.array(
45 fn(value) { pog.text(uuid.to_string(value)) },
46 arg_2,
47 ))
48 |> pog.returning(decoder)
49 |> pog.execute(db)
50}
51
52/// A row you get from running the `delete_brigade_by_id` query
53/// defined in `./src/app/domain/brigade/sql/delete_brigade_by_id.sql`.
54///
55/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
56/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
57///
58pub type DeleteBrigadeByIdRow {
59 DeleteBrigadeByIdRow(id: Uuid, brigade_name: String)
60}
61
62/// Remove a brigade from the DataBase
63///
64/// > 🐿️ This function was generated automatically using v4.6.0 of
65/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
66///
67pub fn delete_brigade_by_id(
68 db: pog.Connection,
69 arg_1: Uuid,
70) -> Result(pog.Returned(DeleteBrigadeByIdRow), pog.QueryError) {
71 let decoder = {
72 use id <- decode.field(0, uuid_decoder())
73 use brigade_name <- decode.field(1, decode.string)
74 decode.success(DeleteBrigadeByIdRow(id:, brigade_name:))
75 }
76
77 "-- Remove a brigade from the DataBase
78delete from public.brigade as b
79where b.id = $1
80returning
81 b.id,
82 b.brigade_name;
83"
84 |> pog.query
85 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
86 |> pog.returning(decoder)
87 |> pog.execute(db)
88}
89
90/// A row you get from running the `insert_new_brigade` query
91/// defined in `./src/app/domain/brigade/sql/insert_new_brigade.sql`.
92///
93/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
94/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
95///
96pub type InsertNewBrigadeRow {
97 InsertNewBrigadeRow(id: Uuid, created_at: Timestamp)
98}
99
100/// Register a new brigade into the database
101///
102/// > 🐿️ This function was generated automatically using v4.6.0 of
103/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
104///
105pub fn insert_new_brigade(
106 db: pog.Connection,
107 arg_1: Uuid,
108 arg_2: String,
109 arg_3: String,
110 arg_4: Bool,
111) -> Result(pog.Returned(InsertNewBrigadeRow), pog.QueryError) {
112 let decoder = {
113 use id <- decode.field(0, uuid_decoder())
114 use created_at <- decode.field(1, pog.timestamp_decoder())
115 decode.success(InsertNewBrigadeRow(id:, created_at:))
116 }
117
118 "-- Register a new brigade into the database
119insert into public.brigade as b (
120 leader_id,
121 brigade_name,
122 vehicle_code,
123 is_active
124) values (
125 $1,
126 $2,
127 $3,
128 $4
129) returning
130 b.id,
131 b.created_at;
132"
133 |> pog.query
134 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
135 |> pog.parameter(pog.text(arg_2))
136 |> pog.parameter(pog.text(arg_3))
137 |> pog.parameter(pog.bool(arg_4))
138 |> pog.returning(decoder)
139 |> pog.execute(db)
140}
141
142/// A row you get from running the `query_all_brigades` query
143/// defined in `./src/app/domain/brigade/sql/query_all_brigades.sql`.
144///
145/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
146/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
147///
148pub type QueryAllBrigadesRow {
149 QueryAllBrigadesRow(
150 id: Uuid,
151 brigade_name: String,
152 leader_name: Option(String),
153 is_active: Bool,
154 )
155}
156
157/// Find all registered brigades
158///
159/// > 🐿️ This function was generated automatically using v4.6.0 of
160/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
161///
162pub fn query_all_brigades(
163 db: pog.Connection,
164) -> Result(pog.Returned(QueryAllBrigadesRow), pog.QueryError) {
165 let decoder = {
166 use id <- decode.field(0, uuid_decoder())
167 use brigade_name <- decode.field(1, decode.string)
168 use leader_name <- decode.field(2, decode.optional(decode.string))
169 use is_active <- decode.field(3, decode.bool)
170 decode.success(QueryAllBrigadesRow(
171 id:,
172 brigade_name:,
173 leader_name:,
174 is_active:,
175 ))
176 }
177
178 "-- Find all registered brigades
179select
180 b.id,
181 b.brigade_name,
182 u.full_name as leader_name,
183 b.is_active
184from public.brigade as b
185left join public.user_account as u
186 on b.leader_id = u.id;
187"
188 |> pog.query
189 |> pog.returning(decoder)
190 |> pog.execute(db)
191}
192
193/// A row you get from running the `query_brigade_info` query
194/// defined in `./src/app/domain/brigade/sql/query_brigade_info.sql`.
195///
196/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
197/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
198///
199pub type QueryBrigadeInfoRow {
200 QueryBrigadeInfoRow(
201 id: Uuid,
202 brigade_name: String,
203 leader_name: Uuid,
204 is_active: Bool,
205 )
206}
207
208/// Find details about a specific brigade
209///
210/// > 🐿️ This function was generated automatically using v4.6.0 of
211/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
212///
213pub fn query_brigade_info(
214 db: pog.Connection,
215 arg_1: Uuid,
216) -> Result(pog.Returned(QueryBrigadeInfoRow), pog.QueryError) {
217 let decoder = {
218 use id <- decode.field(0, uuid_decoder())
219 use brigade_name <- decode.field(1, decode.string)
220 use leader_name <- decode.field(2, uuid_decoder())
221 use is_active <- decode.field(3, decode.bool)
222 decode.success(QueryBrigadeInfoRow(
223 id:,
224 brigade_name:,
225 leader_name:,
226 is_active:,
227 ))
228 }
229
230 "-- Find details about a specific brigade
231select
232 b.id,
233 b.brigade_name,
234 u.id as leader_name,
235 b.is_active
236from public.brigade as b
237inner join public.user_account as u
238 on b.leader_id = u.id
239where b.id = $1;
240"
241 |> pog.query
242 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
243 |> pog.returning(decoder)
244 |> pog.execute(db)
245}
246
247/// A row you get from running the `query_members_id` query
248/// defined in `./src/app/domain/brigade/sql/query_members_id.sql`.
249///
250/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
251/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
252///
253pub type QueryMembersIdRow {
254 QueryMembersIdRow(id: Uuid)
255}
256
257/// Find the id of all members assigned a specific brigade
258///
259/// > 🐿️ This function was generated automatically using v4.6.0 of
260/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
261///
262pub fn query_members_id(
263 db: pog.Connection,
264 arg_1: Uuid,
265) -> Result(pog.Returned(QueryMembersIdRow), pog.QueryError) {
266 let decoder = {
267 use id <- decode.field(0, uuid_decoder())
268 decode.success(QueryMembersIdRow(id:))
269 }
270
271 "-- Find the id of all members assigned a specific brigade
272select u.id
273from public.user_account as u
274inner join public.brigade_membership as bm
275 on u.id = bm.user_id
276where bm.brigade_id = $1;
277"
278 |> pog.query
279 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
280 |> pog.returning(decoder)
281 |> pog.execute(db)
282}
283
284/// A row you get from running the `query_members_info` query
285/// defined in `./src/app/domain/brigade/sql/query_members_info.sql`.
286///
287/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
288/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
289///
290pub type QueryMembersInfoRow {
291 QueryMembersInfoRow(id: Uuid, full_name: String, user_role: UserRoleEnum)
292}
293
294/// Find all members of a brigade
295///
296/// > 🐿️ This function was generated automatically using v4.6.0 of
297/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
298///
299pub fn query_members_info(
300 db: pog.Connection,
301 arg_1: Uuid,
302) -> Result(pog.Returned(QueryMembersInfoRow), pog.QueryError) {
303 let decoder = {
304 use id <- decode.field(0, uuid_decoder())
305 use full_name <- decode.field(1, decode.string)
306 use user_role <- decode.field(2, user_role_enum_decoder())
307 decode.success(QueryMembersInfoRow(id:, full_name:, user_role:))
308 }
309
310 "-- Find all members of a brigade
311select
312 u.id,
313 u.full_name,
314 u.user_role
315from public.user_account as u
316inner join public.brigade_membership as bm
317 on u.id = bm.user_id
318where bm.brigade_id = $1;
319"
320 |> pog.query
321 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
322 |> pog.returning(decoder)
323 |> pog.execute(db)
324}
325
326/// A row you get from running the `replace_brigade_members` query
327/// defined in `./src/app/domain/brigade/sql/replace_brigade_members.sql`.
328///
329/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
330/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
331///
332pub type ReplaceBrigadeMembersRow {
333 ReplaceBrigadeMembersRow(inserted_user_id: Uuid)
334}
335
336/// Replace all brigade members
337///
338/// > 🐿️ This function was generated automatically using v4.6.0 of
339/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
340///
341pub fn replace_brigade_members(
342 db: pog.Connection,
343 arg_1: Uuid,
344 arg_2: List(Uuid),
345) -> Result(pog.Returned(ReplaceBrigadeMembersRow), pog.QueryError) {
346 let decoder = {
347 use inserted_user_id <- decode.field(0, uuid_decoder())
348 decode.success(ReplaceBrigadeMembersRow(inserted_user_id:))
349 }
350
351 "-- Replace all brigade members
352select b.inserted_user_id
353from public.replace_brigade_members($1, $2) as b;
354"
355 |> pog.query
356 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
357 |> pog.parameter(pog.array(
358 fn(value) { pog.text(uuid.to_string(value)) },
359 arg_2,
360 ))
361 |> pog.returning(decoder)
362 |> pog.execute(db)
363}
364
365/// A row you get from running the `update_brigade_status` query
366/// defined in `./src/app/domain/brigade/sql/update_brigade_status.sql`.
367///
368/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
369/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
370///
371pub type UpdateBrigadeStatusRow {
372 UpdateBrigadeStatusRow(id: Uuid, is_active: Bool, updated_at: Timestamp)
373}
374
375/// Set the brigade is_active status to ON or OFF
376///
377/// > 🐿️ This function was generated automatically using v4.6.0 of
378/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
379///
380pub fn update_brigade_status(
381 db: pog.Connection,
382 arg_1: Uuid,
383 arg_2: Bool,
384) -> Result(pog.Returned(UpdateBrigadeStatusRow), pog.QueryError) {
385 let decoder = {
386 use id <- decode.field(0, uuid_decoder())
387 use is_active <- decode.field(1, decode.bool)
388 use updated_at <- decode.field(2, pog.timestamp_decoder())
389 decode.success(UpdateBrigadeStatusRow(id:, is_active:, updated_at:))
390 }
391
392 "-- Set the brigade is_active status to ON or OFF
393update public.brigade as b
394set
395 is_active = $2,
396 updated_at = current_timestamp
397where b.id = $1
398returning
399 b.id,
400 b.is_active,
401 b.updated_at;
402"
403 |> pog.query
404 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
405 |> pog.parameter(pog.bool(arg_2))
406 |> pog.returning(decoder)
407 |> pog.execute(db)
408}
409
410// --- Enums -------------------------------------------------------------------
411
412/// Corresponds to the Postgres `user_role_enum` enum.
413///
414/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
415/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
416///
417pub type UserRoleEnum {
418 Sargeant
419 Developer
420 Captain
421 Firefighter
422 Analyst
423 Admin
424}
425
426fn user_role_enum_decoder() -> decode.Decoder(UserRoleEnum) {
427 use user_role_enum <- decode.then(decode.string)
428 case user_role_enum {
429 "sargeant" -> decode.success(Sargeant)
430 "developer" -> decode.success(Developer)
431 "captain" -> decode.success(Captain)
432 "firefighter" -> decode.success(Firefighter)
433 "analyst" -> decode.success(Analyst)
434 "admin" -> decode.success(Admin)
435 _ -> decode.failure(Sargeant, "UserRoleEnum")
436 }
437}
438
439// --- Encoding/decoding utils -------------------------------------------------
440
441/// A decoder to decode `Uuid`s coming from a Postgres query.
442///
443fn uuid_decoder() {
444 use bit_array <- decode.then(decode.bit_array)
445 case uuid.from_bit_array(bit_array) {
446 Ok(uuid) -> decode.success(uuid)
447 Error(_) -> decode.failure(uuid.v7(), "Uuid")
448 }
449}