diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/api/tasks.rs | 60 | ||||
-rw-r--r-- | src/api/users.rs | 9 |
2 files changed, 54 insertions, 15 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?; diff --git a/src/api/users.rs b/src/api/users.rs index bfbc0d6..86007ca 100644 --- a/src/api/users.rs +++ b/src/api/users.rs @@ -4,7 +4,7 @@ use axum::{ extract::{Path, State}, http::header::LOCATION, response::{IntoResponse, IntoResponseParts}, - Json, + Json, Router, }; use axum_extra::routing::Resource; use serde::{Deserialize, Serialize}; @@ -13,14 +13,17 @@ use time::OffsetDateTime; use uuid::Uuid; use crate::{ + api::tasks::Task, auth::{credentials::Credential, AccessClaims}, state::AppState, }; use super::error::Error; -pub fn router() -> Resource<AppState> { - Resource::named("users").create(create).show(show) +pub fn router() -> Router<AppState> { + let resource = Resource::named("users").create(create).show(show); + + Router::from(resource) } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |