Set options on request types
This commit is contained in:
parent
2a82cf79c4
commit
e2cd84b136
4 changed files with 96 additions and 33 deletions
emgauwa-core/src/handlers/v1
|
@ -2,7 +2,7 @@ use actix_web::{delete, get, post, put, web, HttpResponse};
|
|||
use emgauwa_lib::db::{DbSchedule, DbTag};
|
||||
use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError};
|
||||
use emgauwa_lib::models::{convert_db_list, FromDbModel, Schedule};
|
||||
use emgauwa_lib::types::{RequestSchedule, ScheduleUid};
|
||||
use emgauwa_lib::types::{RequestCreateSchedule, RequestUpdateSchedule, ScheduleUid};
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
|
@ -55,7 +55,7 @@ pub async fn show(
|
|||
#[post("/api/v1/schedules")]
|
||||
pub async fn add(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
data: web::Json<RequestSchedule>,
|
||||
data: web::Json<RequestCreateSchedule>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
|
@ -67,9 +67,11 @@ pub async fn add(
|
|||
)
|
||||
.await?;
|
||||
|
||||
new_schedule
|
||||
.set_tags(&mut pool_conn, data.tags.as_slice())
|
||||
.await?;
|
||||
if let Some(tags) = &data.tags {
|
||||
new_schedule
|
||||
.set_tags(&mut pool_conn, tags.as_slice())
|
||||
.await?;
|
||||
}
|
||||
|
||||
let return_schedule = Schedule::from_db_model(&mut pool_conn, new_schedule)?;
|
||||
Ok(HttpResponse::Created().json(return_schedule))
|
||||
|
@ -77,7 +79,7 @@ pub async fn add(
|
|||
|
||||
async fn add_list_single(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
request_schedule: &RequestSchedule,
|
||||
request_schedule: &RequestCreateSchedule,
|
||||
) -> Result<DbSchedule, DatabaseError> {
|
||||
let new_schedule = DbSchedule::create(
|
||||
conn,
|
||||
|
@ -87,9 +89,9 @@ async fn add_list_single(
|
|||
)
|
||||
.await?;
|
||||
|
||||
new_schedule
|
||||
.set_tags(conn, request_schedule.tags.as_slice())
|
||||
.await?;
|
||||
if let Some(tags) = &request_schedule.tags {
|
||||
new_schedule.set_tags(conn, tags.as_slice()).await?;
|
||||
}
|
||||
|
||||
Ok(new_schedule)
|
||||
}
|
||||
|
@ -97,7 +99,7 @@ async fn add_list_single(
|
|||
#[post("/api/v1/schedules/list")]
|
||||
pub async fn add_list(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
data: web::Json<Vec<RequestSchedule>>,
|
||||
data: web::Json<Vec<RequestCreateSchedule>>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
|
@ -115,7 +117,7 @@ pub async fn add_list(
|
|||
pub async fn update(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
path: web::Path<(String,)>,
|
||||
data: web::Json<RequestSchedule>,
|
||||
data: web::Json<RequestUpdateSchedule>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
|
@ -126,13 +128,21 @@ pub async fn update(
|
|||
.await?
|
||||
.ok_or(DatabaseError::NotFound)?;
|
||||
|
||||
let schedule = schedule
|
||||
.update(&mut pool_conn, data.name.as_str(), &data.periods)
|
||||
.await?;
|
||||
let name = match &data.name {
|
||||
None => schedule.name.as_str(),
|
||||
Some(name) => name.as_str(),
|
||||
};
|
||||
|
||||
schedule
|
||||
.set_tags(&mut pool_conn, data.tags.as_slice())
|
||||
.await?;
|
||||
let periods = match &data.periods {
|
||||
None => &schedule.periods,
|
||||
Some(period) => period,
|
||||
};
|
||||
|
||||
let schedule = schedule.update(&mut pool_conn, name, periods).await?;
|
||||
|
||||
if let Some(tags) = &data.tags {
|
||||
schedule.set_tags(&mut pool_conn, tags.as_slice()).await?;
|
||||
}
|
||||
|
||||
let return_schedule = Schedule::from_db_model(&mut pool_conn, schedule)?;
|
||||
Ok(HttpResponse::Ok().json(return_schedule))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue