119 lines
4.8 KiB
Rust
Raw Normal View History

use std::sync::Mutex;
use crate::models::client::*;
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
use crate::State;
#[get("/")]
pub async fn get_clients(state: web::Data<Mutex<State>>, request: HttpRequest) -> impl Responder {
let token = request.headers().get("Authorization");
if token.is_none() {
return HttpResponse::Unauthorized().body("There is no token");
}
let token = token.unwrap();
if let Ok(guard) = state.lock() {
if guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await.is_err() {
return HttpResponse::Unauthorized().body("Invalid token");
}
}
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<Mutex<State>>, path: web::Path<(String, )>, request: HttpRequest) -> impl Responder {
let token = request.headers().get("Authorization");
if token.is_none() {
return HttpResponse::Unauthorized().body("There is no token");
}
let token = token.unwrap();
if let Ok(guard) = state.lock() {
if guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await.is_err() {
return HttpResponse::Unauthorized().body("Invalid token");
}
}
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<Mutex<State>>, json: web::Json<BindingClient>, request: HttpRequest) -> impl Responder {
let token = request.headers().get("Authorization");
if token.is_none() {
return HttpResponse::Unauthorized().body("There is no token");
}
let token = token.unwrap();
if let Ok(guard) = state.lock() {
if guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await.is_err() {
return HttpResponse::Unauthorized().body("Invalid token");
}
}
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<Mutex<State>>, json: web::Json<BindingClient>, path: web::Path<(String, )>, request: HttpRequest) -> impl Responder {
let token = request.headers().get("Authorization");
if token.is_none() {
return HttpResponse::Unauthorized().body("There is no token");
}
let token = token.unwrap();
if let Ok(guard) = state.lock() {
if guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await.is_err() {
return HttpResponse::Unauthorized().body("Invalid token");
}
}
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<Mutex<State>>, path: web::Path<(String, )>, request: HttpRequest) -> impl Responder {
let token = request.headers().get("Authorization");
if token.is_none() {
return HttpResponse::Unauthorized().body("There is no token");
}
let token = token.unwrap();
if let Ok(guard) = state.lock() {
if guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await.is_err() {
return HttpResponse::Unauthorized().body("Invalid token");
}
}
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())
}
}