From 2074d4b4769215c869d3aec71b46cab9c97a3ec0 Mon Sep 17 00:00:00 2001 From: Luke Harding Date: Fri, 3 May 2024 12:24:46 -0400 Subject: [PATCH] Add get_task endpoint --- server/src/api/mod.rs | 13 +++++++++++++ server/src/db/mod.rs | 13 ++++++++++++- server/src/main.rs | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index b47657d..8e8c0f2 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -2,6 +2,7 @@ // Copyright (C) 2024 Luke Harding use actix_web::{get, HttpResponse, Responder, web}; +use uuid::Uuid; use crate::db; @@ -18,3 +19,15 @@ pub async fn get_tasks(data: web::Data) -> impl Responder { HttpResponse::Ok().json(tasks) } + +#[get("/get_task/{uuid}")] +pub async fn get_task(data: web::Data, path: web::Path) -> impl Responder { + let uuid = path.into_inner(); + + let task = match db::get_task(data.database_url.clone(), uuid).await { + Ok(task) => task, + Err(e) => return HttpResponse::InternalServerError().body(e.to_string()), + }; + + HttpResponse::Ok().json(task) +} diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index f9804f1..fdf2bcb 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -3,9 +3,12 @@ use std::error; -use diesel::{Connection, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper}; +use diesel::{ + Connection, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper, +}; use diesel::pg::Pg; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; +use uuid::Uuid; use models::Task; @@ -32,3 +35,11 @@ pub async fn get_tasks(database_url: String) -> Result, Box Result> { + use crate::schema::tasks::dsl::*; + + let conn = &mut establish_connection(&database_url)?; + + Ok(tasks.filter(id.eq(uuid)).first(conn)?) +} diff --git a/server/src/main.rs b/server/src/main.rs index 76c90ca..8cbd5cd 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -77,6 +77,7 @@ async fn main() -> io::Result<()> { database_url: database_url.clone(), })) .service(api::get_tasks) + .service(api::get_task) }) .bind((bind_addr, port))? .run()