Add controller to database

This commit is contained in:
Tobias Reisinger 2023-11-24 22:45:44 +01:00
parent 9f64075f5a
commit d193000aec
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
34 changed files with 1055 additions and 195 deletions
emgauwa-lib/src/handlers/v1

View file

@ -6,7 +6,7 @@ use sqlx::{Pool, Sqlite};
use crate::db::errors::DatabaseError;
use crate::db::{Periods, Schedule};
use crate::db::tag::Tag;
use crate::db::types::EmgauwaUid;
use crate::db::types::ScheduleUid;
use crate::handlers::errors::ApiError;
use crate::return_models::ReturnSchedule;
use crate::utils::vec_has_error;
@ -41,7 +41,7 @@ pub async fn tagged(
let mut pool_conn = pool.acquire().await?;
let (tag,) = path.into_inner();
let tag_db = Tag::get(&mut pool_conn, &tag).await?;
let tag_db = Tag::get_by_tag(&mut pool_conn, &tag).await?;
let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
@ -61,9 +61,9 @@ pub async fn show(
let mut pool_conn = pool.acquire().await?;
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
let mut return_schedule = ReturnSchedule::from(schedule);
return_schedule.load_tags(&mut pool_conn);
@ -148,9 +148,9 @@ pub async fn update(
let mut pool_conn = pool.acquire().await?;
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
let schedule = schedule
.update(&mut pool_conn, data.name.as_str(), &data.periods)
@ -173,13 +173,13 @@ pub async fn delete(
let mut pool_conn = pool.acquire().await?;
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
match emgauwa_uid {
EmgauwaUid::Off => Err(ApiError::ProtectedSchedule),
EmgauwaUid::On => Err(ApiError::ProtectedSchedule),
EmgauwaUid::Any(_) => {
Schedule::delete_by_uid(&mut pool_conn, emgauwa_uid).await?;
match uid {
ScheduleUid::Off => Err(ApiError::ProtectedSchedule),
ScheduleUid::On => Err(ApiError::ProtectedSchedule),
ScheduleUid::Any(_) => {
Schedule::delete_by_uid(&mut pool_conn, uid).await?;
Ok(HttpResponse::Ok().json("schedule got deleted"))
}
}