Refactor models names
This commit is contained in:
parent
76b14ce75b
commit
be7f31906c
24 changed files with 461 additions and 340 deletions
emgauwa-lib/src/handlers/v1
|
@ -4,16 +4,16 @@ use sqlx::pool::PoolConnection;
|
|||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::{Periods, Schedule};
|
||||
use crate::db::Tag;
|
||||
use crate::db::types::ScheduleUid;
|
||||
use crate::db::DbTag;
|
||||
use crate::db::{DbPeriods, DbSchedule};
|
||||
use crate::handlers::errors::ApiError;
|
||||
use crate::return_models::ReturnSchedule;
|
||||
use crate::models::Schedule;
|
||||
use crate::types::ScheduleUid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RequestSchedule {
|
||||
name: String,
|
||||
periods: Periods,
|
||||
periods: DbPeriods,
|
||||
tags: Vec<String>,
|
||||
}
|
||||
|
||||
|
@ -21,10 +21,12 @@ pub struct RequestSchedule {
|
|||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let schedules = Schedule::get_all(&mut pool_conn).await?;
|
||||
let schedules = DbSchedule::get_all(&mut pool_conn).await?;
|
||||
|
||||
let return_schedules: Vec<ReturnSchedule> =
|
||||
schedules.into_iter().map(|s| ReturnSchedule::from_schedule(s, &mut pool_conn)).collect();
|
||||
let return_schedules: Vec<Schedule> = schedules
|
||||
.into_iter()
|
||||
.map(|s| Schedule::from_schedule(s, &mut pool_conn))
|
||||
.collect();
|
||||
|
||||
Ok(HttpResponse::Ok().json(return_schedules))
|
||||
}
|
||||
|
@ -37,12 +39,14 @@ pub async fn tagged(
|
|||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let (tag,) = path.into_inner();
|
||||
let tag_db = Tag::get_by_tag(&mut pool_conn, &tag).await?;
|
||||
let tag_db = DbTag::get_by_tag(&mut pool_conn, &tag).await?;
|
||||
|
||||
let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
|
||||
let schedules = DbSchedule::get_by_tag(&mut pool_conn, &tag_db).await?;
|
||||
|
||||
let return_schedules: Vec<ReturnSchedule> =
|
||||
schedules.into_iter().map(|s| ReturnSchedule::from_schedule(s, &mut pool_conn)).collect();
|
||||
let return_schedules: Vec<Schedule> = schedules
|
||||
.into_iter()
|
||||
.map(|s| Schedule::from_schedule(s, &mut pool_conn))
|
||||
.collect();
|
||||
|
||||
Ok(HttpResponse::Ok().json(return_schedules))
|
||||
}
|
||||
|
@ -57,9 +61,9 @@ pub async fn show(
|
|||
let (schedule_uid,) = path.into_inner();
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
|
||||
|
||||
let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
|
||||
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid).await?;
|
||||
|
||||
let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
|
||||
let return_schedule = Schedule::from_schedule(schedule, &mut pool_conn);
|
||||
Ok(HttpResponse::Ok().json(return_schedule))
|
||||
}
|
||||
|
||||
|
@ -70,22 +74,22 @@ pub async fn add(
|
|||
) -> Result<HttpResponse, ApiError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let new_schedule = Schedule::create(&mut pool_conn, &data.name, &data.periods).await?;
|
||||
let new_schedule = DbSchedule::create(&mut pool_conn, &data.name, &data.periods).await?;
|
||||
|
||||
new_schedule
|
||||
.set_tags(&mut pool_conn, data.tags.as_slice())
|
||||
.await?;
|
||||
|
||||
let return_schedule = ReturnSchedule::from_schedule(new_schedule, &mut pool_conn);
|
||||
let return_schedule = Schedule::from_schedule(new_schedule, &mut pool_conn);
|
||||
Ok(HttpResponse::Created().json(return_schedule))
|
||||
}
|
||||
|
||||
async fn add_list_single(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
request_schedule: &RequestSchedule,
|
||||
) -> Result<Schedule, DatabaseError> {
|
||||
) -> Result<DbSchedule, DatabaseError> {
|
||||
let new_schedule =
|
||||
Schedule::create(conn, &request_schedule.name, &request_schedule.periods).await?;
|
||||
DbSchedule::create(conn, &request_schedule.name, &request_schedule.periods).await?;
|
||||
|
||||
new_schedule
|
||||
.set_tags(conn, request_schedule.tags.as_slice())
|
||||
|
@ -101,7 +105,7 @@ pub async fn add_list(
|
|||
) -> Result<HttpResponse, ApiError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let result: Vec<Result<Schedule, DatabaseError>> = data
|
||||
let result: Vec<Result<DbSchedule, DatabaseError>> = data
|
||||
.as_slice()
|
||||
.iter()
|
||||
.map(|request_schedule| {
|
||||
|
@ -109,10 +113,12 @@ pub async fn add_list(
|
|||
})
|
||||
.collect();
|
||||
|
||||
let mut return_schedules: Vec<ReturnSchedule> = Vec::new();
|
||||
let mut return_schedules: Vec<Schedule> = Vec::new();
|
||||
for schedule in result {
|
||||
match schedule {
|
||||
Ok(schedule) => return_schedules.push(ReturnSchedule::from_schedule(schedule, &mut pool_conn)),
|
||||
Ok(schedule) => {
|
||||
return_schedules.push(Schedule::from_schedule(schedule, &mut pool_conn))
|
||||
}
|
||||
Err(e) => return Ok(HttpResponse::from(e)),
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +136,7 @@ pub async fn update(
|
|||
let (schedule_uid,) = path.into_inner();
|
||||
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
|
||||
|
||||
let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
|
||||
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid).await?;
|
||||
|
||||
let schedule = schedule
|
||||
.update(&mut pool_conn, data.name.as_str(), &data.periods)
|
||||
|
@ -140,7 +146,7 @@ pub async fn update(
|
|||
.set_tags(&mut pool_conn, data.tags.as_slice())
|
||||
.await?;
|
||||
|
||||
let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
|
||||
let return_schedule = Schedule::from_schedule(schedule, &mut pool_conn);
|
||||
Ok(HttpResponse::Ok().json(return_schedule))
|
||||
}
|
||||
|
||||
|
@ -158,7 +164,7 @@ pub async fn delete(
|
|||
ScheduleUid::Off => Err(ApiError::ProtectedSchedule),
|
||||
ScheduleUid::On => Err(ApiError::ProtectedSchedule),
|
||||
ScheduleUid::Any(_) => {
|
||||
Schedule::delete_by_uid(&mut pool_conn, uid).await?;
|
||||
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