Allow creating and updating tasks on the backend. (#2)

Create hurl tests along with the necessary api code to resolve them.

Reviewed-on: #2
Co-authored-by: Drew Galbraith <drew@tiramisu.one>
Co-committed-by: Drew Galbraith <drew@tiramisu.one>
This commit is contained in:
Drew 2025-09-20 18:49:11 +00:00 committed by Drew
parent 82d524a62f
commit d32f6be813
10 changed files with 306 additions and 28 deletions

View file

@ -0,0 +1,82 @@
mod tasks;
use axum::{
Json,
extract::rejection::{JsonRejection, PathRejection},
http::StatusCode,
response::IntoResponse,
};
// AppError
// Borrowed from example: https://github.com/tokio-rs/axum/blob/axum-v0.8.4/examples/anyhow-error-response/src/main.rs
pub enum AppError {
InternalError(anyhow::Error),
JsonExtractError(JsonRejection),
PathError(PathRejection),
NotFound,
}
#[derive(Serialize)]
pub struct ErrorJson {
error: String,
}
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
match self {
Self::InternalError(anyhow_error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong {}", anyhow_error),
)
.into_response(),
Self::JsonExtractError(rej) => (
StatusCode::UNPROCESSABLE_ENTITY,
Json(ErrorJson {
error: rej.status().to_string(),
}),
)
.into_response(),
Self::PathError(rej) => (
StatusCode::BAD_REQUEST,
Json(ErrorJson {
error: rej.status().to_string(),
}),
)
.into_response(),
Self::NotFound => (
StatusCode::NOT_FOUND,
Json(ErrorJson {
error: "Requeted Entity Not Found".to_string(),
}),
)
.into_response(),
}
}
}
impl From<JsonRejection> for AppError {
fn from(value: JsonRejection) -> Self {
Self::JsonExtractError(value)
}
}
impl From<PathRejection> for AppError {
fn from(value: PathRejection) -> Self {
Self::PathError(value)
}
}
impl From<anyhow::Error> for AppError {
fn from(err: anyhow::Error) -> Self {
Self::InternalError(err.into())
}
}
impl From<sqlx::Error> for AppError {
fn from(err: sqlx::Error) -> Self {
Self::InternalError(err.into())
}
}
use serde::Serialize;
pub use tasks::*;

View file

@ -0,0 +1,47 @@
use axum::{
Json, Router,
extract::{Path, State},
http::StatusCode,
routing::{get, post},
};
use axum_extra::extract::WithRejection;
use axum_macros::debug_handler;
use serde::Deserialize;
use sqlx::{Pool, Sqlite};
use uuid::Uuid;
use crate::models::TaskModel;
use super::AppError;
pub fn create_task_router() -> Router<Pool<Sqlite>> {
Router::new()
.route("/", post(create_task))
.route("/{task_id}", get(get_task))
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CreateTaskRequest {
title: String,
description: Option<String>,
}
#[debug_handler]
pub async fn create_task(
State(pool): State<Pool<Sqlite>>,
WithRejection(Json(input), _): WithRejection<Json<CreateTaskRequest>, AppError>,
) -> Result<(StatusCode, Json<TaskModel>), AppError> {
let model = TaskModel::insert(&pool, &input.title, input.description.as_deref()).await?;
Ok((StatusCode::CREATED, Json(model)))
}
pub async fn get_task(
State(pool): State<Pool<Sqlite>>,
WithRejection(Path(task_id), _): WithRejection<Path<Uuid>, AppError>,
) -> Result<(StatusCode, Json<TaskModel>), AppError> {
let model = TaskModel::get_by_id(&pool, task_id).await?;
Ok((StatusCode::OK, Json(model)))
}