Add delete_task
This commit is contained in:
parent
ff3d302f18
commit
99f4dd4b42
@ -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 chrono::NaiveDateTime;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@ -10,7 +10,8 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
|
|||||||
let scope = web::scope("/v1")
|
let scope = web::scope("/v1")
|
||||||
.service(get_task)
|
.service(get_task)
|
||||||
.service(get_tasks)
|
.service(get_tasks)
|
||||||
.service(create_task);
|
.service(create_task)
|
||||||
|
.service(delete_task);
|
||||||
|
|
||||||
cfg.service(scope);
|
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)
|
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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -33,19 +33,18 @@ pub fn run_migrations(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_tasks(database_url: String) -> Result<Vec<Task>, Box<dyn error::Error>> {
|
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)?;
|
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>> {
|
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)?;
|
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(
|
pub async fn create_task(
|
||||||
@ -69,3 +68,9 @@ pub async fn create_task(
|
|||||||
.returning(Task::as_returning())
|
.returning(Task::as_returning())
|
||||||
.get_result(conn)?)
|
.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)?)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user