Set options on request types
This commit is contained in:
		
							parent
							
								
									2a82cf79c4
								
							
						
					
					
						commit
						e2cd84b136
					
				
					 4 changed files with 96 additions and 33 deletions
				
			
		| 
						 | 
					@ -2,7 +2,7 @@ use actix_web::{delete, get, put, web, HttpResponse};
 | 
				
			||||||
use emgauwa_lib::db::DbController;
 | 
					use emgauwa_lib::db::DbController;
 | 
				
			||||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
					use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
				
			||||||
use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel};
 | 
					use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel};
 | 
				
			||||||
use emgauwa_lib::types::{ControllerUid, RequestController};
 | 
					use emgauwa_lib::types::{ControllerUid, RequestUpdateController};
 | 
				
			||||||
use sqlx::{Pool, Sqlite};
 | 
					use sqlx::{Pool, Sqlite};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[get("/api/v1/controllers")]
 | 
					#[get("/api/v1/controllers")]
 | 
				
			||||||
| 
						 | 
					@ -38,7 +38,7 @@ pub async fn show(
 | 
				
			||||||
pub async fn update(
 | 
					pub async fn update(
 | 
				
			||||||
	pool: web::Data<Pool<Sqlite>>,
 | 
						pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
	path: web::Path<(String,)>,
 | 
						path: web::Path<(String,)>,
 | 
				
			||||||
	data: web::Json<RequestController>,
 | 
						data: web::Json<RequestUpdateController>,
 | 
				
			||||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
					) -> Result<HttpResponse, EmgauwaError> {
 | 
				
			||||||
	let mut pool_conn = pool.acquire().await?;
 | 
						let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,9 +1,10 @@
 | 
				
			||||||
use actix::Addr;
 | 
					use actix::Addr;
 | 
				
			||||||
use actix_web::{get, put, web, HttpResponse};
 | 
					use actix_web::{get, put, web, HttpResponse};
 | 
				
			||||||
use emgauwa_lib::db::{DbController, DbRelay, DbTag};
 | 
					use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbTag};
 | 
				
			||||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
					use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
				
			||||||
use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay};
 | 
					use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay};
 | 
				
			||||||
use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestRelay};
 | 
					use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestUpdateRelay};
 | 
				
			||||||
 | 
					use emgauwa_lib::utils;
 | 
				
			||||||
use sqlx::{Pool, Sqlite};
 | 
					use sqlx::{Pool, Sqlite};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::app_state;
 | 
					use crate::app_state;
 | 
				
			||||||
| 
						 | 
					@ -85,7 +86,7 @@ pub async fn update_for_controller(
 | 
				
			||||||
	pool: web::Data<Pool<Sqlite>>,
 | 
						pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
	app_server: web::Data<Addr<AppServer>>,
 | 
						app_server: web::Data<Addr<AppServer>>,
 | 
				
			||||||
	path: web::Path<(String, i64)>,
 | 
						path: web::Path<(String, i64)>,
 | 
				
			||||||
	data: web::Json<RequestRelay>,
 | 
						data: web::Json<RequestUpdateRelay>,
 | 
				
			||||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
					) -> Result<HttpResponse, EmgauwaError> {
 | 
				
			||||||
	let mut pool_conn = pool.acquire().await?;
 | 
						let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -96,13 +97,44 @@ pub async fn update_for_controller(
 | 
				
			||||||
		.await?
 | 
							.await?
 | 
				
			||||||
		.ok_or(DatabaseError::NotFound)?;
 | 
							.ok_or(DatabaseError::NotFound)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let relay = DbRelay::get_by_controller_and_num(&mut pool_conn, &controller, relay_num)
 | 
						let mut relay = DbRelay::get_by_controller_and_num(&mut pool_conn, &controller, relay_num)
 | 
				
			||||||
		.await?
 | 
							.await?
 | 
				
			||||||
		.ok_or(DatabaseError::NotFound)?;
 | 
							.ok_or(DatabaseError::NotFound)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let relay = relay.update(&mut pool_conn, data.name.as_str()).await?;
 | 
						if let Some(name) = &data.name {
 | 
				
			||||||
 | 
							relay = relay.update(&mut pool_conn, name.as_str()).await?;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	relay.set_tags(&mut pool_conn, data.tags.as_slice()).await?;
 | 
						if let Some(schedule_uids) = &data.schedules {
 | 
				
			||||||
 | 
							if schedule_uids.len() == 7 {
 | 
				
			||||||
 | 
								let mut schedules = Vec::new();
 | 
				
			||||||
 | 
								for s_uid in schedule_uids {
 | 
				
			||||||
 | 
									schedules.push(s_uid.get_schedule(&mut pool_conn).await?);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								DbJunctionRelaySchedule::set_schedules(
 | 
				
			||||||
 | 
									&mut pool_conn,
 | 
				
			||||||
 | 
									&relay,
 | 
				
			||||||
 | 
									schedules.iter().collect(),
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
								.await?;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if let Some(s_uid) = &data.active_schedule {
 | 
				
			||||||
 | 
							let schedule = s_uid.get_schedule(&mut pool_conn).await?;
 | 
				
			||||||
 | 
							DbJunctionRelaySchedule::set_schedule(
 | 
				
			||||||
 | 
								&mut pool_conn,
 | 
				
			||||||
 | 
								&relay,
 | 
				
			||||||
 | 
								&schedule,
 | 
				
			||||||
 | 
								utils::get_weekday(),
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
							.await?;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if let Some(tags) = &data.tags {
 | 
				
			||||||
 | 
							relay.set_tags(&mut pool_conn, tags.as_slice()).await?;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let return_relay = Relay::from_db_model(&mut pool_conn, relay)?;
 | 
						let return_relay = Relay::from_db_model(&mut pool_conn, relay)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@ use actix_web::{delete, get, post, put, web, HttpResponse};
 | 
				
			||||||
use emgauwa_lib::db::{DbSchedule, DbTag};
 | 
					use emgauwa_lib::db::{DbSchedule, DbTag};
 | 
				
			||||||
use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError};
 | 
					use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError};
 | 
				
			||||||
use emgauwa_lib::models::{convert_db_list, FromDbModel, Schedule};
 | 
					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::PoolConnection;
 | 
				
			||||||
use sqlx::{Pool, Sqlite};
 | 
					use sqlx::{Pool, Sqlite};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -55,7 +55,7 @@ pub async fn show(
 | 
				
			||||||
#[post("/api/v1/schedules")]
 | 
					#[post("/api/v1/schedules")]
 | 
				
			||||||
pub async fn add(
 | 
					pub async fn add(
 | 
				
			||||||
	pool: web::Data<Pool<Sqlite>>,
 | 
						pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
	data: web::Json<RequestSchedule>,
 | 
						data: web::Json<RequestCreateSchedule>,
 | 
				
			||||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
					) -> Result<HttpResponse, EmgauwaError> {
 | 
				
			||||||
	let mut pool_conn = pool.acquire().await?;
 | 
						let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -67,9 +67,11 @@ pub async fn add(
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	.await?;
 | 
						.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if let Some(tags) = &data.tags {
 | 
				
			||||||
		new_schedule
 | 
							new_schedule
 | 
				
			||||||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
								.set_tags(&mut pool_conn, tags.as_slice())
 | 
				
			||||||
			.await?;
 | 
								.await?;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let return_schedule = Schedule::from_db_model(&mut pool_conn, new_schedule)?;
 | 
						let return_schedule = Schedule::from_db_model(&mut pool_conn, new_schedule)?;
 | 
				
			||||||
	Ok(HttpResponse::Created().json(return_schedule))
 | 
						Ok(HttpResponse::Created().json(return_schedule))
 | 
				
			||||||
| 
						 | 
					@ -77,7 +79,7 @@ pub async fn add(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn add_list_single(
 | 
					async fn add_list_single(
 | 
				
			||||||
	conn: &mut PoolConnection<Sqlite>,
 | 
						conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	request_schedule: &RequestSchedule,
 | 
						request_schedule: &RequestCreateSchedule,
 | 
				
			||||||
) -> Result<DbSchedule, DatabaseError> {
 | 
					) -> Result<DbSchedule, DatabaseError> {
 | 
				
			||||||
	let new_schedule = DbSchedule::create(
 | 
						let new_schedule = DbSchedule::create(
 | 
				
			||||||
		conn,
 | 
							conn,
 | 
				
			||||||
| 
						 | 
					@ -87,9 +89,9 @@ async fn add_list_single(
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	.await?;
 | 
						.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	new_schedule
 | 
						if let Some(tags) = &request_schedule.tags {
 | 
				
			||||||
		.set_tags(conn, request_schedule.tags.as_slice())
 | 
							new_schedule.set_tags(conn, tags.as_slice()).await?;
 | 
				
			||||||
		.await?;
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Ok(new_schedule)
 | 
						Ok(new_schedule)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -97,7 +99,7 @@ async fn add_list_single(
 | 
				
			||||||
#[post("/api/v1/schedules/list")]
 | 
					#[post("/api/v1/schedules/list")]
 | 
				
			||||||
pub async fn add_list(
 | 
					pub async fn add_list(
 | 
				
			||||||
	pool: web::Data<Pool<Sqlite>>,
 | 
						pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
	data: web::Json<Vec<RequestSchedule>>,
 | 
						data: web::Json<Vec<RequestCreateSchedule>>,
 | 
				
			||||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
					) -> Result<HttpResponse, EmgauwaError> {
 | 
				
			||||||
	let mut pool_conn = pool.acquire().await?;
 | 
						let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -115,7 +117,7 @@ pub async fn add_list(
 | 
				
			||||||
pub async fn update(
 | 
					pub async fn update(
 | 
				
			||||||
	pool: web::Data<Pool<Sqlite>>,
 | 
						pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
	path: web::Path<(String,)>,
 | 
						path: web::Path<(String,)>,
 | 
				
			||||||
	data: web::Json<RequestSchedule>,
 | 
						data: web::Json<RequestUpdateSchedule>,
 | 
				
			||||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
					) -> Result<HttpResponse, EmgauwaError> {
 | 
				
			||||||
	let mut pool_conn = pool.acquire().await?;
 | 
						let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -126,13 +128,21 @@ pub async fn update(
 | 
				
			||||||
		.await?
 | 
							.await?
 | 
				
			||||||
		.ok_or(DatabaseError::NotFound)?;
 | 
							.ok_or(DatabaseError::NotFound)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let schedule = schedule
 | 
						let name = match &data.name {
 | 
				
			||||||
		.update(&mut pool_conn, data.name.as_str(), &data.periods)
 | 
							None => schedule.name.as_str(),
 | 
				
			||||||
		.await?;
 | 
							Some(name) => name.as_str(),
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	schedule
 | 
						let periods = match &data.periods {
 | 
				
			||||||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
							None => &schedule.periods,
 | 
				
			||||||
		.await?;
 | 
							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)?;
 | 
						let return_schedule = Schedule::from_db_model(&mut pool_conn, schedule)?;
 | 
				
			||||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
						Ok(HttpResponse::Ok().json(return_schedule))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,21 +1,31 @@
 | 
				
			||||||
use serde_derive::{Deserialize, Serialize};
 | 
					use serde_derive::{Deserialize, Serialize};
 | 
				
			||||||
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
 | 
					use sqlx::Sqlite;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::db::DbPeriods;
 | 
					use crate::db::{DbPeriods, DbSchedule};
 | 
				
			||||||
 | 
					use crate::errors::DatabaseError;
 | 
				
			||||||
use crate::types::ScheduleUid;
 | 
					use crate::types::ScheduleUid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Deserialize)]
 | 
					#[derive(Debug, Serialize, Deserialize)]
 | 
				
			||||||
pub struct RequestSchedule {
 | 
					pub struct RequestCreateSchedule {
 | 
				
			||||||
	pub name: String,
 | 
						pub name: String,
 | 
				
			||||||
	pub periods: DbPeriods,
 | 
						pub periods: DbPeriods,
 | 
				
			||||||
	pub tags: Vec<String>,
 | 
						pub tags: Option<Vec<String>>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Deserialize)]
 | 
					#[derive(Debug, Serialize, Deserialize)]
 | 
				
			||||||
pub struct RequestRelay {
 | 
					pub struct RequestUpdateSchedule {
 | 
				
			||||||
	pub name: String,
 | 
						pub name: Option<String>,
 | 
				
			||||||
 | 
						pub periods: Option<DbPeriods>,
 | 
				
			||||||
 | 
						pub tags: Option<Vec<String>>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Serialize, Deserialize)]
 | 
				
			||||||
 | 
					pub struct RequestUpdateRelay {
 | 
				
			||||||
 | 
						pub name: Option<String>,
 | 
				
			||||||
	pub active_schedule: Option<RequestScheduleId>,
 | 
						pub active_schedule: Option<RequestScheduleId>,
 | 
				
			||||||
	pub schedules: Vec<RequestScheduleId>,
 | 
						pub schedules: Option<Vec<RequestScheduleId>>,
 | 
				
			||||||
	pub tags: Vec<String>,
 | 
						pub tags: Option<Vec<String>>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Deserialize)]
 | 
					#[derive(Debug, Serialize, Deserialize)]
 | 
				
			||||||
| 
						 | 
					@ -24,6 +34,17 @@ pub struct RequestScheduleId {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Deserialize)]
 | 
					#[derive(Debug, Serialize, Deserialize)]
 | 
				
			||||||
pub struct RequestController {
 | 
					pub struct RequestUpdateController {
 | 
				
			||||||
	pub name: String,
 | 
						pub name: String,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl RequestScheduleId {
 | 
				
			||||||
 | 
						pub async fn get_schedule(
 | 
				
			||||||
 | 
							&self,
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
						) -> Result<DbSchedule, DatabaseError> {
 | 
				
			||||||
 | 
							DbSchedule::get_by_uid(conn, &self.id)
 | 
				
			||||||
 | 
								.await?
 | 
				
			||||||
 | 
								.ok_or(DatabaseError::NotFound)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue