Move request models
This commit is contained in:
		
							parent
							
								
									b3228ea6b5
								
							
						
					
					
						commit
						2a82cf79c4
					
				
					 7 changed files with 48 additions and 32 deletions
				
			
		| 
						 | 
				
			
			@ -2,15 +2,9 @@ use actix_web::{delete, get, put, web, HttpResponse};
 | 
			
		|||
use emgauwa_lib::db::DbController;
 | 
			
		||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
			
		||||
use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel};
 | 
			
		||||
use emgauwa_lib::types::ControllerUid;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use emgauwa_lib::types::{ControllerUid, RequestController};
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestController {
 | 
			
		||||
	name: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/controllers")]
 | 
			
		||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,19 +3,12 @@ use actix_web::{get, put, web, HttpResponse};
 | 
			
		|||
use emgauwa_lib::db::{DbController, DbRelay, DbTag};
 | 
			
		||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
			
		||||
use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay};
 | 
			
		||||
use emgauwa_lib::types::{ControllerUid, ControllerWsAction};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestRelay};
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
use crate::app_state;
 | 
			
		||||
use crate::app_state::AppServer;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestRelay {
 | 
			
		||||
	name: String,
 | 
			
		||||
	tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/relays")]
 | 
			
		||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
| 
						 | 
				
			
			@ -68,7 +61,6 @@ pub async fn index_for_controller(
 | 
			
		|||
#[get("/api/v1/controllers/{controller_id}/relays/{relay_num}")]
 | 
			
		||||
pub async fn show_for_controller(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	app_server: web::Data<Addr<AppServer>>,
 | 
			
		||||
	path: web::Path<(String, i64)>,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
| 
						 | 
				
			
			@ -91,6 +83,7 @@ pub async fn show_for_controller(
 | 
			
		|||
#[put("/api/v1/controllers/{controller_id}/relays/{relay_num}")]
 | 
			
		||||
pub async fn update_for_controller(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	app_server: web::Data<Addr<AppServer>>,
 | 
			
		||||
	path: web::Path<(String, i64)>,
 | 
			
		||||
	data: web::Json<RequestRelay>,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -112,5 +105,13 @@ pub async fn update_for_controller(
 | 
			
		|||
	relay.set_tags(&mut pool_conn, data.tags.as_slice()).await?;
 | 
			
		||||
 | 
			
		||||
	let return_relay = Relay::from_db_model(&mut pool_conn, relay)?;
 | 
			
		||||
 | 
			
		||||
	app_server
 | 
			
		||||
		.send(app_state::Action {
 | 
			
		||||
			controller_uid: uid,
 | 
			
		||||
			action: ControllerWsAction::Relays(vec![return_relay.clone()]),
 | 
			
		||||
		})
 | 
			
		||||
		.await??;
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_relay))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,19 +1,11 @@
 | 
			
		|||
use actix_web::{delete, get, post, put, web, HttpResponse};
 | 
			
		||||
use emgauwa_lib::db::{DbPeriods, DbSchedule, DbTag};
 | 
			
		||||
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::ScheduleUid;
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use emgauwa_lib::types::{RequestSchedule, ScheduleUid};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestSchedule {
 | 
			
		||||
	name: String,
 | 
			
		||||
	periods: DbPeriods,
 | 
			
		||||
	tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/schedules")]
 | 
			
		||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue