mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 15:55:45 +00:00
63 lines
2.2 KiB
Rust
63 lines
2.2 KiB
Rust
use crate::todo::{Todo, TodoRequest};
|
|
use actix_web::{delete, get, post, put, web, HttpResponse, Responder};
|
|
use sqlx::PgPool;
|
|
|
|
#[get("/todos")]
|
|
async fn find_all(db_pool: web::Data<PgPool>) -> impl Responder {
|
|
let result = Todo::find_all(db_pool.get_ref()).await;
|
|
match result {
|
|
Ok(todos) => HttpResponse::Ok().json(todos),
|
|
_ => HttpResponse::BadRequest().body("Error trying to read all todos from database")
|
|
}
|
|
}
|
|
|
|
#[get("/todo/{id}")]
|
|
async fn find(id: web::Path<i32>, db_pool: web::Data<PgPool>) -> impl Responder {
|
|
let result = Todo::find_by_id(id.into_inner(), db_pool.get_ref()).await;
|
|
match result {
|
|
Ok(todo) => HttpResponse::Ok().json(todo),
|
|
_ => HttpResponse::BadRequest().body("Todo not found")
|
|
}
|
|
}
|
|
|
|
#[post("/todo")]
|
|
async fn create(todo: web::Json<TodoRequest>, db_pool: web::Data<PgPool>) -> impl Responder {
|
|
let result = Todo::create(todo.into_inner(), db_pool.get_ref()).await;
|
|
match result {
|
|
Ok(todo) => HttpResponse::Ok().json(todo),
|
|
_ => HttpResponse::BadRequest().body("Error trying to create new todo")
|
|
}
|
|
}
|
|
|
|
#[put("/todo/{id}")]
|
|
async fn update(id: web::Path<i32>, todo: web::Json<TodoRequest>, db_pool: web::Data<PgPool>) -> impl Responder {
|
|
let result = Todo::update(id.into_inner(), todo.into_inner(),db_pool.get_ref()).await;
|
|
match result {
|
|
Ok(todo) => HttpResponse::Ok().json(todo),
|
|
_ => HttpResponse::BadRequest().body("Todo not found")
|
|
}
|
|
}
|
|
|
|
#[delete("/todo/{id}")]
|
|
async fn delete(id: web::Path<i32>, db_pool: web::Data<PgPool>) -> impl Responder {
|
|
let result = Todo::delete(id.into_inner(), db_pool.get_ref()).await;
|
|
match result {
|
|
Ok(rows) => {
|
|
if rows > 0 {
|
|
HttpResponse::Ok().body(format!("Successfully deleted {} record(s)", rows))
|
|
} else {
|
|
HttpResponse::BadRequest().body("Todo not found")
|
|
}
|
|
},
|
|
_ => HttpResponse::BadRequest().body("Todo not found")
|
|
}
|
|
}
|
|
|
|
// function that will be called on new Application to configure routes for this module
|
|
pub fn init(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(find_all);
|
|
cfg.service(find);
|
|
cfg.service(create);
|
|
cfg.service(update);
|
|
cfg.service(delete);
|
|
} |