2023-03-31 21:19:59 +04:00
|
|
|
use std::sync::Mutex;
|
|
|
|
use crate::models::client::*;
|
2023-05-10 22:02:30 +04:00
|
|
|
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
|
2023-03-31 21:19:59 +04:00
|
|
|
use crate::State;
|
|
|
|
|
|
|
|
#[get("/")]
|
2023-05-10 22:02:30 +04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:54:54 +04:00
|
|
|
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())
|
|
|
|
}
|
2023-03-31 21:19:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/{id}")]
|
2023-05-18 23:53:59 +04:00
|
|
|
pub async fn get_client(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, request: HttpRequest) -> impl Responder {
|
2023-05-10 22:02:30 +04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:54:54 +04:00
|
|
|
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())
|
|
|
|
}
|
2023-03-31 21:19:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/")]
|
2023-05-10 22:02:30 +04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:54:54 +04:00
|
|
|
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())
|
|
|
|
}
|
2023-03-31 21:19:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[patch("/{id}")]
|
2023-05-18 23:53:59 +04:00
|
|
|
pub async fn update_client(state: web::Data<Mutex<State>>, json: web::Json<BindingClient>, path: web::Path<(String, )>, request: HttpRequest) -> impl Responder {
|
2023-05-10 22:02:30 +04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:54:54 +04:00
|
|
|
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())
|
|
|
|
}
|
2023-03-31 21:19:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/{id}")]
|
2023-05-18 23:53:59 +04:00
|
|
|
pub async fn delete_client(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, request: HttpRequest) -> impl Responder {
|
2023-05-10 22:02:30 +04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:54:54 +04:00
|
|
|
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())
|
|
|
|
}
|
2023-03-31 21:19:59 +04:00
|
|
|
}
|