Implement get task.
This commit is contained in:
parent
2b2fc974a2
commit
10efd3de8f
4 changed files with 64 additions and 12 deletions
|
|
@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::services::AppError;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy, sqlx::Type)]
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy, sqlx::Type)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
#[sqlx(rename_all = "lowercase")]
|
#[sqlx(rename_all = "lowercase")]
|
||||||
|
|
@ -46,15 +48,19 @@ impl TaskModel {
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_id(pool: &SqlitePool, id: Uuid) -> Result<TaskModel> {
|
pub async fn get_by_id(pool: &SqlitePool, id: Uuid) -> Result<TaskModel, AppError> {
|
||||||
let result = sqlx::query_as("SELECT * FROM tasks WHERE id = $1")
|
let model = sqlx::query_as("SELECT * FROM tasks WHERE id = $1")
|
||||||
.bind(id)
|
.bind(id)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await?;
|
.await
|
||||||
Ok(result)
|
.map_err(|err| match err {
|
||||||
|
sqlx::Error::RowNotFound => AppError::NotFound,
|
||||||
|
e => anyhow::Error::from(e).into(),
|
||||||
|
})?;
|
||||||
|
Ok(model)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(self, pool: &SqlitePool) -> Result<TaskModel> {
|
pub async fn update(self, pool: &SqlitePool) -> Result<TaskModel, AppError> {
|
||||||
let now: DateTime<Utc> = Utc::now();
|
let now: DateTime<Utc> = Utc::now();
|
||||||
|
|
||||||
let _ = sqlx::query(
|
let _ = sqlx::query(
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,19 @@
|
||||||
mod tasks;
|
mod tasks;
|
||||||
|
|
||||||
use axum::{Json, extract::rejection::JsonRejection, http::StatusCode, response::IntoResponse};
|
use axum::{
|
||||||
|
Json,
|
||||||
|
extract::rejection::{JsonRejection, PathRejection},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
};
|
||||||
|
|
||||||
// AppError
|
// AppError
|
||||||
// Borrowed from example: https://github.com/tokio-rs/axum/blob/axum-v0.8.4/examples/anyhow-error-response/src/main.rs
|
// Borrowed from example: https://github.com/tokio-rs/axum/blob/axum-v0.8.4/examples/anyhow-error-response/src/main.rs
|
||||||
pub enum AppError {
|
pub enum AppError {
|
||||||
InternalError(anyhow::Error),
|
InternalError(anyhow::Error),
|
||||||
JsonExtractError(JsonRejection),
|
JsonExtractError(JsonRejection),
|
||||||
|
PathError(PathRejection),
|
||||||
|
NotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
|
@ -29,6 +36,20 @@ impl IntoResponse for AppError {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.into_response(),
|
.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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -39,11 +60,23 @@ impl From<JsonRejection> for AppError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<PathRejection> for AppError {
|
||||||
|
fn from(value: PathRejection) -> Self {
|
||||||
|
Self::PathError(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<anyhow::Error> for AppError {
|
impl From<anyhow::Error> for AppError {
|
||||||
fn from(err: anyhow::Error) -> Self {
|
fn from(err: anyhow::Error) -> Self {
|
||||||
Self::InternalError(err.into())
|
Self::InternalError(err.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<sqlx::Error> for AppError {
|
||||||
|
fn from(err: sqlx::Error) -> Self {
|
||||||
|
Self::InternalError(err.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
pub use tasks::*;
|
pub use tasks::*;
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,23 @@
|
||||||
use axum::{Json, Router, extract::State, http::StatusCode, routing::post};
|
use axum::{
|
||||||
|
Json, Router,
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
routing::{get, post},
|
||||||
|
};
|
||||||
use axum_extra::extract::WithRejection;
|
use axum_extra::extract::WithRejection;
|
||||||
use axum_macros::debug_handler;
|
use axum_macros::debug_handler;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::models::TaskModel;
|
use crate::models::TaskModel;
|
||||||
|
|
||||||
use super::AppError;
|
use super::AppError;
|
||||||
|
|
||||||
pub fn create_task_router() -> Router<Pool<Sqlite>> {
|
pub fn create_task_router() -> Router<Pool<Sqlite>> {
|
||||||
Router::new().route("/", post(create_task))
|
Router::new()
|
||||||
|
.route("/", post(create_task))
|
||||||
|
.route("/{task_id}", get(get_task))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
@ -28,3 +36,12 @@ pub async fn create_task(
|
||||||
|
|
||||||
Ok((StatusCode::CREATED, Json(model)))
|
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)))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,8 +65,6 @@ HTTP 200
|
||||||
jsonpath "$.id" == "{{task_id}}"
|
jsonpath "$.id" == "{{task_id}}"
|
||||||
jsonpath "$.title" == "Test task"
|
jsonpath "$.title" == "Test task"
|
||||||
jsonpath "$.description" == "A test task for the API"
|
jsonpath "$.description" == "A test task for the API"
|
||||||
jsonpath "$.priority" == "medium"
|
|
||||||
jsonpath "$.due_date" == "2024-12-31"
|
|
||||||
jsonpath "$.status" == "todo"
|
jsonpath "$.status" == "todo"
|
||||||
jsonpath "$.created_at" exists
|
jsonpath "$.created_at" exists
|
||||||
jsonpath "$.updated_at" exists
|
jsonpath "$.updated_at" exists
|
||||||
|
|
@ -78,10 +76,8 @@ HTTP 200
|
||||||
[Asserts]
|
[Asserts]
|
||||||
jsonpath "$.id" == "{{minimal_task_id}}"
|
jsonpath "$.id" == "{{minimal_task_id}}"
|
||||||
jsonpath "$.title" == "Minimal task"
|
jsonpath "$.title" == "Minimal task"
|
||||||
jsonpath "$.priority" == "medium"
|
|
||||||
jsonpath "$.status" == "todo"
|
jsonpath "$.status" == "todo"
|
||||||
jsonpath "$.description" == null
|
jsonpath "$.description" == null
|
||||||
jsonpath "$.due_date" == null
|
|
||||||
|
|
||||||
# Test: Get non-existent task
|
# Test: Get non-existent task
|
||||||
GET {{host}}/api/tasks/00000000-0000-0000-0000-000000000000
|
GET {{host}}/api/tasks/00000000-0000-0000-0000-000000000000
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue