use std::sync::Mutex; use crate::models::client::*; use actix_web::{web, get, post, patch, delete, Responder, HttpResponse}; use crate::State; #[get("/")] pub async fn get_clients(state: web::Data>) -> impl Responder { match state.lock() { Ok(guard) => match guard.client_repository.read_all().await { Ok(result) => HttpResponse::Ok().json(result), Err(error) => HttpResponse::InternalServerError().json(error.to_string()) }, Err(error) => HttpResponse::InternalServerError().json(error.to_string()) } } #[get("/{id}")] pub async fn get_client(state: web::Data>, path: web::Path<(u32, )>) -> impl Responder { match state.lock() { Ok(guard) => match guard.client_repository.read(path.into_inner().0).await { Ok(result) => HttpResponse::Ok().json(result), Err(error) => HttpResponse::InternalServerError().json(error.to_string()) }, Err(error) => HttpResponse::InternalServerError().json(error.to_string()) } } #[post("/")] pub async fn create_client(state: web::Data>, json: web::Json) -> impl Responder { match state.lock() { Ok(guard) => match guard.client_repository.create(json.0).await { Ok(result) => HttpResponse::Ok().json(result), Err(error) => HttpResponse::InternalServerError().json(error.to_string()) }, Err(error) => HttpResponse::InternalServerError().json(error.to_string()) } } #[patch("/{id}")] pub async fn update_client(state: web::Data>, json: web::Json, path: web::Path<(u32, )>) -> impl Responder { match state.lock() { Ok(guard) => match guard.client_repository.update(path.into_inner().0, json.0).await { Ok(result) => HttpResponse::Ok().json(result), Err(error) => HttpResponse::InternalServerError().json(error.to_string()) }, Err(error) => HttpResponse::InternalServerError().json(error.to_string()) } } #[delete("/{id}")] pub async fn delete_client(state: web::Data>, path: web::Path<(u32, )>) -> impl Responder { match state.lock() { Ok(guard) => match guard.client_repository.delete(path.into_inner().0).await { Ok(result) => HttpResponse::Ok().json(result), Err(error) => HttpResponse::InternalServerError().json(error.to_string()) }, Err(error) => HttpResponse::InternalServerError().json(error.to_string()) } }