Files
boring-lang/goal-examples/webapp/router.bl
2025-10-09 17:40:54 -06:00

46 lines
1.2 KiB
Plaintext

import logging as logging;
import orm as orm;
import uuid as uuid;
import webapp.models as models;
#[derive(DeepCopy, Debug, PartialEq)]
type TodoGetParams struct {
#[validate(format="short")]
pub id: uuid.UUID;
}
#[derive(DeepCopy, Debug, PartialEq, Serialize)]
type TodoResponse struct {
#[validate(format="short")]
pub id: uuid.UUID;
#[validate(format="short")]
pub user_id: uuid.UUID;
#[validate(min_length=1, max_length=256)]
pub title: String;
#[validate(min_length=1, max_length=1000000)]
pub description: String;
pub created_at: DateTime;
pub updated_at: DateTime;
}
impl Into<TodoResponse> for models.Todo {
fn into(self: Self): TodoResponse {
return TodoResponse{..self};
}
}
type Router struct {
logger: logging::Logger;
db: orm::DB;
pub fn new(logger: logging::Logger, db: orm::DB): Router {
return Self{logger: logger, db: db};
}
pub fn get_todo(self: Self, req: http.Request[TodoGetParams, http::NoQuery, http::NoBody]): IO[Result[http::JsonResponse[TodoResponse], http::Error]] {
let id = req.params.id;
let instance = models::Todo::select().filter(models::Todo::Id::equals(id)).first(self.db)??;
return http::JsonResponse<TodoResponse>::ok(instance.into());
}
}