Cleanup db/schedule namespace

This commit is contained in:
Tobias Reisinger 2023-11-21 16:33:41 +01:00
parent 09c50411d1
commit effd3f3b18
8 changed files with 155 additions and 209 deletions
src/handlers/v1

View file

@ -7,7 +7,6 @@ use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
use crate::db::errors::DatabaseError;
use crate::db::models::{Periods, Schedule};
use crate::db::schedules::*;
use crate::db::tag::get_tag;
use crate::handlers::errors::ApiError;
@ -26,7 +25,7 @@ pub struct RequestSchedule {
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let schedules = get_schedules(&mut pool_conn).await?;
let schedules = Schedule::get_all(&mut pool_conn).await?;
let mut return_schedules: Vec<ReturnSchedule> =
schedules.iter().map(ReturnSchedule::from).collect();
@ -47,7 +46,7 @@ pub async fn tagged(
let (tag,) = path.into_inner();
let tag_db = get_tag(&mut pool_conn, &tag).await?;
let schedules = get_schedules_by_tag(&mut pool_conn, &tag_db).await?;
let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
let mut return_schedules: Vec<ReturnSchedule> =
schedules.iter().map(ReturnSchedule::from).collect();
@ -67,7 +66,7 @@ pub async fn show(
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = get_schedule_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let mut return_schedule = ReturnSchedule::from(schedule);
return_schedule.load_tags(&mut pool_conn);
@ -81,9 +80,11 @@ pub async fn add(
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let new_schedule = create_schedule(&mut pool_conn, &data.name, &data.periods).await?;
let new_schedule = Schedule::create(&mut pool_conn, &data.name, &data.periods).await?;
set_schedule_tags(&mut pool_conn, &new_schedule, data.tags.as_slice()).await?;
new_schedule
.set_tags(&mut pool_conn, data.tags.as_slice())
.await?;
let mut return_schedule = ReturnSchedule::from(new_schedule);
return_schedule.load_tags(&mut pool_conn);
@ -95,9 +96,11 @@ async fn add_list_single(
request_schedule: &RequestSchedule,
) -> Result<Schedule, DatabaseError> {
let new_schedule =
create_schedule(conn, &request_schedule.name, &request_schedule.periods).await?;
Schedule::create(conn, &request_schedule.name, &request_schedule.periods).await?;
set_schedule_tags(conn, &new_schedule, request_schedule.tags.as_slice()).await?;
new_schedule
.set_tags(conn, request_schedule.tags.as_slice())
.await?;
Ok(new_schedule)
}
@ -150,17 +153,15 @@ pub async fn update(
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = get_schedule_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let schedule = update_schedule(
&mut pool_conn,
&schedule,
data.name.as_str(),
data.periods.borrow(),
)
.await?;
let schedule = schedule
.update(&mut pool_conn, data.name.as_str(), data.periods.borrow())
.await?;
set_schedule_tags(&mut pool_conn, &schedule, data.tags.as_slice()).await?;
schedule
.set_tags(&mut pool_conn, data.tags.as_slice())
.await?;
let mut return_schedule = ReturnSchedule::from(schedule);
return_schedule.load_tags(&mut pool_conn);
@ -181,7 +182,7 @@ pub async fn delete(
EmgauwaUid::Off => Err(ApiError::ProtectedSchedule),
EmgauwaUid::On => Err(ApiError::ProtectedSchedule),
EmgauwaUid::Any(_) => {
delete_schedule_by_uid(&mut pool_conn, emgauwa_uid).await?;
Schedule::delete_by_uid(&mut pool_conn, emgauwa_uid).await?;
Ok(HttpResponse::Ok().json("schedule got deleted"))
}
}