Add get_task endpoint
This commit is contained in:
parent
9d492f6f91
commit
2074d4b476
@ -2,6 +2,7 @@
|
|||||||
// Copyright (C) 2024 Luke Harding
|
// Copyright (C) 2024 Luke Harding
|
||||||
|
|
||||||
use actix_web::{get, HttpResponse, Responder, web};
|
use actix_web::{get, HttpResponse, Responder, web};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::db;
|
use crate::db;
|
||||||
|
|
||||||
@ -18,3 +19,15 @@ pub async fn get_tasks(data: web::Data<AppState>) -> impl Responder {
|
|||||||
|
|
||||||
HttpResponse::Ok().json(tasks)
|
HttpResponse::Ok().json(tasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/get_task/{uuid}")]
|
||||||
|
pub async fn get_task(data: web::Data<AppState>, path: web::Path<Uuid>) -> 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)
|
||||||
|
}
|
||||||
|
@ -3,9 +3,12 @@
|
|||||||
|
|
||||||
use std::error;
|
use std::error;
|
||||||
|
|
||||||
use diesel::{Connection, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper};
|
use diesel::{
|
||||||
|
Connection, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper,
|
||||||
|
};
|
||||||
use diesel::pg::Pg;
|
use diesel::pg::Pg;
|
||||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use models::Task;
|
use models::Task;
|
||||||
|
|
||||||
@ -32,3 +35,11 @@ pub async fn get_tasks(database_url: String) -> Result<Vec<Task>, Box<dyn error:
|
|||||||
|
|
||||||
Ok(tasks.select(Task::as_select()).load(conn)?)
|
Ok(tasks.select(Task::as_select()).load(conn)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_task(database_url: String, uuid: Uuid) -> Result<Task, Box<dyn error::Error>> {
|
||||||
|
use crate::schema::tasks::dsl::*;
|
||||||
|
|
||||||
|
let conn = &mut establish_connection(&database_url)?;
|
||||||
|
|
||||||
|
Ok(tasks.filter(id.eq(uuid)).first(conn)?)
|
||||||
|
}
|
||||||
|
@ -77,6 +77,7 @@ async fn main() -> io::Result<()> {
|
|||||||
database_url: database_url.clone(),
|
database_url: database_url.clone(),
|
||||||
}))
|
}))
|
||||||
.service(api::get_tasks)
|
.service(api::get_tasks)
|
||||||
|
.service(api::get_task)
|
||||||
})
|
})
|
||||||
.bind((bind_addr, port))?
|
.bind((bind_addr, port))?
|
||||||
.run()
|
.run()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user