Refactor errors and some other stuff/fixes
This commit is contained in:
parent
8d996888bd
commit
5a7b2de0ea
28 changed files with 507 additions and 341 deletions
emgauwa-core/src/handlers/v1
|
@ -1,14 +1,12 @@
|
|||
use actix_web::{delete, get, post, put, web, HttpResponse};
|
||||
use emgauwa_lib::db::errors::DatabaseError;
|
||||
use emgauwa_lib::db::{DbPeriods, DbSchedule, DbTag};
|
||||
use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError};
|
||||
use emgauwa_lib::models::{convert_db_list, FromDbModel, Schedule};
|
||||
use emgauwa_lib::types::ScheduleUid;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
use crate::handlers::errors::ApiError;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RequestSchedule {
|
||||
name: String,
|
||||
|
@ -17,7 +15,7 @@ pub struct RequestSchedule {
|
|||
}
|
||||
|
||||
#[get("/api/v1/schedules")]
|
||||
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_schedules = DbSchedule::get_all(&mut pool_conn).await?;
|
||||
|
@ -30,7 +28,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();
|
||||
|
@ -48,11 +46,11 @@ pub async fn tagged(
|
|||
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 (schedule_uid,) = path.into_inner();
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str())?;
|
||||
|
||||
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid)
|
||||
.await?
|
||||
|
@ -66,7 +64,7 @@ pub async fn show(
|
|||
pub async fn add(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
data: web::Json<RequestSchedule>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let new_schedule = DbSchedule::create(
|
||||
|
@ -108,7 +106,7 @@ async fn add_list_single(
|
|||
pub async fn add_list(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
data: web::Json<Vec<RequestSchedule>>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let mut db_schedules: Vec<DbSchedule> = Vec::new();
|
||||
|
@ -126,11 +124,11 @@ pub async fn update(
|
|||
pool: web::Data<Pool<Sqlite>>,
|
||||
path: web::Path<(String,)>,
|
||||
data: web::Json<RequestSchedule>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let (schedule_uid,) = path.into_inner();
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str())?;
|
||||
|
||||
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid)
|
||||
.await?
|
||||
|
@ -152,15 +150,15 @@ 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 (schedule_uid,) = path.into_inner();
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str())?;
|
||||
|
||||
match uid {
|
||||
ScheduleUid::Off => Err(ApiError::ProtectedSchedule),
|
||||
ScheduleUid::On => Err(ApiError::ProtectedSchedule),
|
||||
ScheduleUid::Off => Err(EmgauwaError::from(ApiError::ProtectedSchedule)),
|
||||
ScheduleUid::On => Err(EmgauwaError::from(ApiError::ProtectedSchedule)),
|
||||
ScheduleUid::Any(_) => {
|
||||
DbSchedule::delete_by_uid(&mut pool_conn, uid).await?;
|
||||
Ok(HttpResponse::Ok().json("schedule got deleted"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue