diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-08-02 18:32:11 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-08-02 18:32:11 -0500 |
commit | 17ea17fc753df373ca9acee46f06d3cf31ca7084 (patch) | |
tree | 8aa646c75b2931316d417951da81e6f111c7a9ac /src/api/tasks.rs | |
parent | 82cc3b8054f4b126b422d946ea694d282a41cd78 (diff) |
wiptasks
Diffstat (limited to 'src/api/tasks.rs')
-rw-r--r-- | src/api/tasks.rs | 60 |
1 files changed, 48 insertions, 12 deletions
diff --git a/src/api/tasks.rs b/src/api/tasks.rs index b8acd91..cda0ce9 100644 --- a/src/api/tasks.rs +++ b/src/api/tasks.rs @@ -1,11 +1,13 @@ +use std::{ops::Range, time::Duration}; + use axum::{ - extract::{Path, State}, + extract::{Path, Query, State}, http::StatusCode, Json, }; use axum_extra::routing::Resource; use serde::{Deserialize, Serialize}; -use sqlx::PgPool; +use sqlx::{prelude::FromRow, PgPool}; use time::OffsetDateTime; use uuid::Uuid; @@ -13,7 +15,7 @@ use crate::{auth::AccessClaims, state::AppState}; use super::error::Error; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, FromRow)] pub struct Task { pub id: Uuid, pub user_id: Option<Uuid>, @@ -21,30 +23,64 @@ pub struct Task { pub description: Option<String>, pub remote: bool, pub location: Option<String>, - pub start_at: Option<OffsetDateTime>, - pub end_at: Option<OffsetDateTime>, + pub start: OffsetDateTime, + pub duration: Duration, pub created_at: OffsetDateTime, pub updated_at: OffsetDateTime, } #[derive(Debug, Clone, Serialize, Deserialize)] +pub struct TaskQuery { + pub user_id: Option<Uuid>, + pub remote: Option<bool>, + pub title: Option<String>, + pub description: Option<String>, + pub time_range: Option<Range<OffsetDateTime>>, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct CreateTaskSchema { pub user_id: Uuid, pub title: String, pub description: Option<String>, pub remote: bool, pub location: Option<String>, - pub start_at: Option<OffsetDateTime>, - pub end_at: Option<OffsetDateTime>, + pub start: OffsetDateTime, + pub duration: Duration, } pub fn router() -> Resource<AppState> { Resource::named("tasks") + .index(index) .create(create) .show(show) .destroy(destroy) } +pub async fn index( + Query(task_query): Query<TaskQuery>, + State(pool): State<PgPool>, + _: AccessClaims, +) -> Result<Json<Vec<Task>>, Error> { + sqlx::query_as!( + Task, + "SELECT * FROM task + WHERE ($1::UUID IS NULL OR user_id = $1) + AND ($2::BOOLEAN IS NULL OR remote = $2) + AND ($3::TEXT IS NULL OR title LIKE '%$3%') + AND ($4::TEXT IS NULL OR title LIKE '%$4%') + LIMIT 100", + task_query.user_id, + task_query.remote, + task_query.title, + task_query.description, + ) + .fetch_all(&pool) + .await + .map(Json) + .map_err(Into::into) +} + pub async fn create( State(pool): State<PgPool>, AccessClaims { sub, .. }: AccessClaims, @@ -54,8 +90,8 @@ pub async fn create( description, remote, location, - start_at, - end_at, + start, + duration, }): Json<CreateTaskSchema>, ) -> Result<(StatusCode, Json<Task>), Error> { if sub != user_id { @@ -63,14 +99,14 @@ pub async fn create( } let task = sqlx::query_as!(Task, - "INSERT INTO task (user_id, title, description, remote, location, start_at, end_at) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", + "INSERT INTO task (user_id, title, description, remote, location, start, duration) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", user_id, title, description, remote, location, - start_at, - end_at, + start, + duration, ) .fetch_one(&pool) .await?; |