Refactor errors and some other stuff/fixes

This commit is contained in:
Tobias Reisinger 2023-12-01 18:27:04 +01:00
parent 8d996888bd
commit 5a7b2de0ea
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
28 changed files with 507 additions and 341 deletions
emgauwa-core/src/handlers/v1

View file

@ -1,20 +1,18 @@
use actix_web::{delete, get, put, web, HttpResponse};
use emgauwa_lib::db::errors::DatabaseError;
use emgauwa_lib::db::DbController;
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel};
use emgauwa_lib::types::ControllerUid;
use serde_derive::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
use crate::handlers::errors::ApiError;
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestController {
name: String,
}
#[get("/api/v1/controllers")]
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
let db_controllers = DbController::get_all(&mut pool_conn).await?;
@ -28,11 +26,11 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
pub async fn show(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid,) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let uid = ControllerUid::try_from(controller_uid.as_str())?;
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
.await?
@ -47,11 +45,11 @@ pub async fn update(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
data: web::Json<RequestController>,
) -> Result<HttpResponse, ApiError> {
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid,) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let uid = ControllerUid::try_from(controller_uid.as_str())?;
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
.await?
@ -69,11 +67,11 @@ pub async fn update(
pub async fn delete(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid,) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let uid = ControllerUid::try_from(controller_uid.as_str())?;
DbController::delete_by_uid(&mut pool_conn, uid).await?;
Ok(HttpResponse::Ok().json("controller got deleted"))