Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
2.0 kB · 86 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import gleam/listimport gleam/resultimport pogimport server/segment/sqlimport youid/uuid
pub type SegmentError { /// Failed to connect to the Database DatabaseError(error: pog.QueryError) /// Something went wrong when registering a Startup FailedToRegisterSegment /// Segment was not found in the Database NotFound(id: uuid.Uuid)}
pub type Segment { Segment( /// Segment's ID id: uuid.Uuid, /// Segment's name name: String, /// A brief description description: String, )}
/// Register a new segment in the Database.////// ## Examples////// ```gleam/// let result = segment.register(context.database, name:, description:)////// case result {/// Ok(data) -> todo as "send response"/// Error(segment.NotFound) -> wisp.not_found()/// Error(_) -> wisp.internal_server_error()/// }/// ```pub fn register( database: pog.Connection, name name: String, description description: String,) -> Result(Segment, SegmentError) { use returned <- result.try( sql.register(database, name, description) |> result.map_error(DatabaseError), )
use row <- result.map( list.first(returned.rows) |> result.replace_error(FailedToRegisterSegment), )
Segment(id: row.id, name: row.name, description: row.description)}
/// Search a segment in the Database using their ID.////// ## Examples////// ```gleam/// let result = segment.get(context.database, id)////// case result {/// Ok(data) -> todo as "send response"/// Error(segment.NotFound) -> wisp.not_found()/// Error(_) -> wisp.internal_server_error()/// }/// ```pub fn get( database: pog.Connection, id: uuid.Uuid,) -> Result(Segment, SegmentError) { use returned <- result.try( sql.get(database, id) |> result.map_error(DatabaseError), )
use row <- result.map( list.first(returned.rows) |> result.replace_error(NotFound(id:)), )
Segment(id: row.id, name: row.name, description: row.description)}