Server API v1 #1

Merged
luke merged 30 commits from development into main 2024-05-04 20:22:45 +00:00
2 changed files with 30 additions and 8 deletions
Showing only changes of commit 99f4dd4b42 - Show all commits

View File

@ -1,4 +1,4 @@
use actix_web::{get, HttpResponse, post, Responder, web};
use actix_web::{delete, get, HttpResponse, post, Responder, web};
use chrono::NaiveDateTime;
use serde::Deserialize;
use uuid::Uuid;
@ -10,7 +10,8 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
let scope = web::scope("/v1")
.service(get_task)
.service(get_tasks)
.service(create_task);
.service(create_task)
.service(delete_task);
cfg.service(scope);
}
@ -63,3 +64,19 @@ pub async fn create_task(data: web::Data<AppState>, task: web::Json<JsonTask>) -
HttpResponse::Ok().json(new_task)
}
#[delete("/delete_task/{uuid}")]
pub async fn delete_task(data: web::Data<AppState>, path: web::Path<Uuid>) -> impl Responder {
let uuid = path.into_inner();
let result = match db::delete_task(data.database_url.clone(), uuid).await {
Ok(result) => result,
Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
};
if result > 0 {
HttpResponse::Ok().body(uuid.to_string())
} else {
HttpResponse::NotFound().body("Unable to delete. Not found.")
}
}

View File

@ -33,19 +33,18 @@ pub fn run_migrations(
}
pub async fn get_tasks(database_url: String) -> Result<Vec<Task>, Box<dyn error::Error>> {
use crate::schema::tasks::dsl::*;
let conn = &mut establish_connection(&database_url)?;
Ok(tasks.select(Task::as_select()).load(conn)?)
Ok(tasks::dsl::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)?)
Ok(tasks::dsl::tasks
.find(uuid)
.select(Task::as_select())
.first(conn)?)
}
pub async fn create_task(
@ -69,3 +68,9 @@ pub async fn create_task(
.returning(Task::as_returning())
.get_result(conn)?)
}
pub async fn delete_task(database_url: String, uuid: Uuid) -> Result<usize, Box<dyn error::Error>> {
let conn = &mut establish_connection(&database_url)?;
Ok(diesel::delete(tasks::dsl::tasks.filter(tasks::dsl::id.eq(uuid))).execute(conn)?)
}