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,13 +1,11 @@
use actix_web::{get, put, web, HttpResponse};
use emgauwa_lib::db::errors::DatabaseError;
use emgauwa_lib::db::{DbController, DbRelay, DbTag};
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay};
use emgauwa_lib::types::ControllerUid;
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
use crate::handlers::errors::ApiError;
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestRelay {
name: String,
@ -15,7 +13,7 @@ pub struct RequestRelay {
}
#[get("/api/v1/relays")]
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_relays = DbRelay::get_all(&mut pool_conn).await?;
@ -29,7 +27,7 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
pub async fn tagged(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
let (tag,) = path.into_inner();
@ -47,11 +45,11 @@ pub async fn tagged(
pub async fn index_for_controller(
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?
@ -67,11 +65,11 @@ pub async fn index_for_controller(
pub async fn show_for_controller(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid, relay_num) = 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?
@ -90,11 +88,11 @@ pub async fn update_for_controller(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String, i64)>,
data: web::Json<RequestRelay>,
) -> Result<HttpResponse, ApiError> {
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid, relay_num) = 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?