Add WIP relays to database and api
This commit is contained in:
		
							parent
							
								
									4e3df272c3
								
							
						
					
					
						commit
						734f8b291c
					
				
					 14 changed files with 387 additions and 88 deletions
				
			
		| 
						 | 
				
			
			@ -1,12 +1,11 @@
 | 
			
		|||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use serde_derive::Serialize;
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
use crate::db::tag::Tag;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::types::ControllerUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
| 
						 | 
				
			
			@ -18,9 +17,6 @@ pub struct Controller {
 | 
			
		|||
	pub active: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
 | 
			
		||||
pub struct Periods(pub Vec<Period>);
 | 
			
		||||
 | 
			
		||||
impl Controller {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
| 
						 | 
				
			
			@ -117,6 +113,6 @@ impl Controller {
 | 
			
		|||
		.execute(conn.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Controller::get_by_uid(conn, &self.uid).await
 | 
			
		||||
		Self::get(conn, self.id).await
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,14 +10,16 @@ use crate::db::types::ScheduleUid;
 | 
			
		|||
 | 
			
		||||
pub mod errors;
 | 
			
		||||
mod model_utils;
 | 
			
		||||
mod models;
 | 
			
		||||
mod schedules;
 | 
			
		||||
pub mod tag;
 | 
			
		||||
mod tag;
 | 
			
		||||
pub mod types;
 | 
			
		||||
mod controllers;
 | 
			
		||||
mod relays;
 | 
			
		||||
 | 
			
		||||
pub use controllers::Controller;
 | 
			
		||||
pub use relays::Relay;
 | 
			
		||||
pub use schedules::{Periods, Schedule};
 | 
			
		||||
pub use tag::Tag;
 | 
			
		||||
 | 
			
		||||
static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,8 +0,0 @@
 | 
			
		|||
use serde::Serialize;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(skip)]
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	// TODO
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										130
									
								
								emgauwa-lib/src/db/relays.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								emgauwa-lib/src/db/relays.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,130 @@
 | 
			
		|||
use serde_derive::Serialize;
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
use crate::db::Controller;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
#[derive(sqlx::FromRow)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(skip)]
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	pub name: String,
 | 
			
		||||
	pub number: i64,
 | 
			
		||||
	#[serde(skip)]
 | 
			
		||||
	pub controller_id: i64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Relay {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<Relay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Relay, "SELECT * FROM relays")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Relay, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Relay,
 | 
			
		||||
			"SELECT * FROM relays WHERE id = ?",
 | 
			
		||||
			id
 | 
			
		||||
		)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|s| s.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tag: &Tag,
 | 
			
		||||
	) -> Result<Vec<Relay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Relay, "SELECT schedule.* FROM relays AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_number: i64,
 | 
			
		||||
		new_controller: &Controller,
 | 
			
		||||
	) -> Result<Relay, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Relay,
 | 
			
		||||
			"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
			new_name,
 | 
			
		||||
			new_number,
 | 
			
		||||
			new_controller.id,
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM relays WHERE id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
				_ => Ok(()),
 | 
			
		||||
			})?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn update(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_number: i64,
 | 
			
		||||
		new_controller: &Controller,
 | 
			
		||||
	) -> Result<Relay, DatabaseError> {
 | 
			
		||||
		sqlx::query!(
 | 
			
		||||
			"UPDATE relays SET name = ?, number = ?, controller_id = ? WHERE id = ?",
 | 
			
		||||
			new_name,
 | 
			
		||||
			new_number,
 | 
			
		||||
			new_controller.id,
 | 
			
		||||
			self.id,
 | 
			
		||||
		)
 | 
			
		||||
		.execute(conn.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Relay::get(conn, self.id).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_controller(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Controller, DatabaseError> {
 | 
			
		||||
		Controller::get(conn, self.controller_id).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_tags(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Vec<String>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn set_tags(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_tags: &[String],
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM junction_tag WHERE relay_id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_tag in new_tags {
 | 
			
		||||
			let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			tag.link_relay(conn, self).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ use sqlx::Sqlite;
 | 
			
		|||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
use crate::db::tag::Tag;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::types::ScheduleUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
| 
						 | 
				
			
			@ -34,7 +34,7 @@ impl Schedule {
 | 
			
		|||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		id: &i64,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Schedule,
 | 
			
		||||
| 
						 | 
				
			
			@ -127,7 +127,7 @@ impl Schedule {
 | 
			
		|||
		.execute(conn.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Schedule::get_by_uid(conn, &self.uid).await
 | 
			
		||||
		Schedule::get(conn, self.id).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_tags(
 | 
			
		||||
| 
						 | 
				
			
			@ -149,16 +149,7 @@ impl Schedule {
 | 
			
		|||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_tag in new_tags {
 | 
			
		||||
			let tag: Option<Tag> =
 | 
			
		||||
				sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", new_tag)
 | 
			
		||||
					.fetch_optional(conn.deref_mut())
 | 
			
		||||
					.await?;
 | 
			
		||||
 | 
			
		||||
			let tag = match tag {
 | 
			
		||||
				Some(id) => id,
 | 
			
		||||
				None => Tag::create(conn, new_tag).await?,
 | 
			
		||||
			};
 | 
			
		||||
 | 
			
		||||
			let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			tag.link_schedule(conn, self).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,8 +5,7 @@ use sqlx::pool::PoolConnection;
 | 
			
		|||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::models::*;
 | 
			
		||||
use crate::db::Schedule;
 | 
			
		||||
use crate::db::{Relay, Schedule};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
pub struct Tag {
 | 
			
		||||
| 
						 | 
				
			
			@ -46,6 +45,17 @@ impl Tag {
 | 
			
		|||
			.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_tag: &str,
 | 
			
		||||
	) -> Result<Tag, DatabaseError> {
 | 
			
		||||
		match Tag::get_by_tag(conn, target_tag).await {
 | 
			
		||||
			Ok(tag) => Ok(tag),
 | 
			
		||||
			Err(DatabaseError::NotFound) => Tag::create(conn, target_tag).await,
 | 
			
		||||
			Err(e) => Err(e),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_tag: &str,
 | 
			
		||||
| 
						 | 
				
			
			@ -56,7 +66,6 @@ impl Tag {
 | 
			
		|||
			.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	#[allow(dead_code)]
 | 
			
		||||
	pub async fn link_relay(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,2 +1,3 @@
 | 
			
		|||
pub mod schedules;
 | 
			
		||||
pub mod ws;
 | 
			
		||||
pub mod relays;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										163
									
								
								emgauwa-lib/src/handlers/v1/relays.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								emgauwa-lib/src/handlers/v1/relays.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,163 @@
 | 
			
		|||
use actix_web::{delete, get, post, put, web, HttpResponse};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::Relay;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::return_models::ReturnRelay;
 | 
			
		||||
 | 
			
		||||
#[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, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let relays = Relay::get_all(&mut pool_conn).await?;
 | 
			
		||||
 | 
			
		||||
	let return_relays: Vec<ReturnRelay> =
 | 
			
		||||
		relays.iter().map(|s| ReturnRelay::from_relay_ref(s, &mut pool_conn)).collect();
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_relays))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//#[get("/api/v1/tags/tag/{tag}")]
 | 
			
		||||
//pub async fn tagged(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	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 relays = Relay::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relays: Vec<ReturnRelay> =
 | 
			
		||||
//		relays.iter().map(|s| ReturnRelay::from_relay_ref(s, &mut pool_conn)).collect();
 | 
			
		||||
//
 | 
			
		||||
//	Ok(HttpResponse::Ok().json(return_relays))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[get("/api/v1/tags/{relay_id}")]
 | 
			
		||||
//pub async fn show(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let (relay_uid,) = path.into_inner();
 | 
			
		||||
//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
//
 | 
			
		||||
//	let relay = Relay::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relay = ReturnRelay::from_relay(relay, &mut pool_conn);
 | 
			
		||||
//	Ok(HttpResponse::Ok().json(return_relay))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[post("/api/v1/tags")]
 | 
			
		||||
//pub async fn add(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	data: web::Json<RequestRelay>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let new_relay = Relay::create(&mut pool_conn, &data.name, &data.periods).await?;
 | 
			
		||||
//
 | 
			
		||||
//	new_relay
 | 
			
		||||
//		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relay = ReturnRelay::from_relay(new_relay, &mut pool_conn);
 | 
			
		||||
//	Ok(HttpResponse::Created().json(return_relay))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//async fn add_list_single(
 | 
			
		||||
//	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
//	request_relay: &RequestRelay,
 | 
			
		||||
//) -> Result<Relay, DatabaseError> {
 | 
			
		||||
//	let new_relay =
 | 
			
		||||
//		Relay::create(conn, &request_relay.name, &request_relay.periods).await?;
 | 
			
		||||
//
 | 
			
		||||
//	new_relay
 | 
			
		||||
//		.set_tags(conn, request_relay.tags.as_slice())
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	Ok(new_relay)
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[post("/api/v1/tags/list")]
 | 
			
		||||
//pub async fn add_list(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	data: web::Json<Vec<RequestRelay>>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let result: Vec<Result<Relay, DatabaseError>> = data
 | 
			
		||||
//		.as_slice()
 | 
			
		||||
//		.iter()
 | 
			
		||||
//		.map(|request_relay| {
 | 
			
		||||
//			futures::executor::block_on(add_list_single(&mut pool_conn, request_relay))
 | 
			
		||||
//		})
 | 
			
		||||
//		.collect();
 | 
			
		||||
//
 | 
			
		||||
//	let mut return_relays: Vec<ReturnRelay> = Vec::new();
 | 
			
		||||
//	for relay in result {
 | 
			
		||||
//		match relay {
 | 
			
		||||
//			Ok(relay) => return_relays.push(ReturnRelay::from_relay(relay, &mut pool_conn)),
 | 
			
		||||
//			Err(e) => return Ok(HttpResponse::from(e)),
 | 
			
		||||
//		}
 | 
			
		||||
//	}
 | 
			
		||||
//	Ok(HttpResponse::Created().json(return_relays))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[put("/api/v1/tags/{relay_id}")]
 | 
			
		||||
//pub async fn update(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//	data: web::Json<RequestRelay>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let (relay_uid,) = path.into_inner();
 | 
			
		||||
//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
//
 | 
			
		||||
//	let relay = Relay::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
//
 | 
			
		||||
//	let relay = relay
 | 
			
		||||
//		.update(&mut pool_conn, data.name.as_str(), &data.periods)
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	relay
 | 
			
		||||
//		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relay = ReturnRelay::from_relay(relay, &mut pool_conn);
 | 
			
		||||
//	Ok(HttpResponse::Ok().json(return_relay))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[delete("/api/v1/tags/{relay_id}")]
 | 
			
		||||
//pub async fn delete(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let (relay_uid,) = path.into_inner();
 | 
			
		||||
//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
//
 | 
			
		||||
//	match uid {
 | 
			
		||||
//		RelayUid::Off => Err(ApiError::ProtectedRelay),
 | 
			
		||||
//		RelayUid::On => Err(ApiError::ProtectedRelay),
 | 
			
		||||
//		RelayUid::Any(_) => {
 | 
			
		||||
//			Relay::delete_by_uid(&mut pool_conn, uid).await?;
 | 
			
		||||
//			Ok(HttpResponse::Ok().json("relay got deleted"))
 | 
			
		||||
//		}
 | 
			
		||||
//	}
 | 
			
		||||
//}
 | 
			
		||||
| 
						 | 
				
			
			@ -5,11 +5,10 @@ use sqlx::{Pool, Sqlite};
 | 
			
		|||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::{Periods, Schedule};
 | 
			
		||||
use crate::db::tag::Tag;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::types::ScheduleUid;
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::return_models::ReturnSchedule;
 | 
			
		||||
use crate::utils::vec_has_error;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestSchedule {
 | 
			
		||||
| 
						 | 
				
			
			@ -24,11 +23,8 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
 | 
			
		|||
 | 
			
		||||
	let schedules = Schedule::get_all(&mut pool_conn).await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.iter().map(ReturnSchedule::from).collect();
 | 
			
		||||
	for schedule in return_schedules.iter_mut() {
 | 
			
		||||
		schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	}
 | 
			
		||||
	let return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.iter().map(|s| ReturnSchedule::from_schedule_ref(s, &mut pool_conn)).collect();
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -45,11 +41,9 @@ pub async fn tagged(
 | 
			
		|||
 | 
			
		||||
	let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.iter().map(ReturnSchedule::from).collect();
 | 
			
		||||
	for schedule in return_schedules.iter_mut() {
 | 
			
		||||
		schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	}
 | 
			
		||||
	let return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.iter().map(|s| ReturnSchedule::from_schedule_ref(s, &mut pool_conn)).collect();
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -65,8 +59,7 @@ pub async fn show(
 | 
			
		|||
 | 
			
		||||
	let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedule = ReturnSchedule::from(schedule);
 | 
			
		||||
	return_schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -83,8 +76,7 @@ pub async fn add(
 | 
			
		|||
		.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);
 | 
			
		||||
	let return_schedule = ReturnSchedule::from_schedule(new_schedule, &mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Created().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -117,26 +109,14 @@ pub async fn add_list(
 | 
			
		|||
		})
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	match vec_has_error(&result) {
 | 
			
		||||
		true => Ok(HttpResponse::from(
 | 
			
		||||
			result
 | 
			
		||||
				.into_iter()
 | 
			
		||||
				.find(|r| r.is_err())
 | 
			
		||||
				.unwrap()
 | 
			
		||||
				.unwrap_err(),
 | 
			
		||||
		)),
 | 
			
		||||
		false => {
 | 
			
		||||
			let mut return_schedules: Vec<ReturnSchedule> = result
 | 
			
		||||
				.iter()
 | 
			
		||||
				.map(|s| ReturnSchedule::from(s.as_ref().unwrap()))
 | 
			
		||||
				.collect();
 | 
			
		||||
 | 
			
		||||
			for schedule in return_schedules.iter_mut() {
 | 
			
		||||
				schedule.load_tags(&mut pool_conn);
 | 
			
		||||
			}
 | 
			
		||||
			Ok(HttpResponse::Created().json(return_schedules))
 | 
			
		||||
	let mut return_schedules: Vec<ReturnSchedule> = Vec::new();
 | 
			
		||||
	for schedule in result {
 | 
			
		||||
		match schedule {
 | 
			
		||||
			Ok(schedule) => return_schedules.push(ReturnSchedule::from_schedule(schedule, &mut pool_conn)),
 | 
			
		||||
			Err(e) => return Ok(HttpResponse::from(e)),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	Ok(HttpResponse::Created().json(return_schedules))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[put("/api/v1/schedules/{schedule_id}")]
 | 
			
		||||
| 
						 | 
				
			
			@ -160,8 +140,7 @@ pub async fn update(
 | 
			
		|||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedule = ReturnSchedule::from(schedule);
 | 
			
		||||
	return_schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,8 +1,9 @@
 | 
			
		|||
use crate::db::Schedule;
 | 
			
		||||
use crate::db::{Controller, Relay, Schedule};
 | 
			
		||||
use futures::executor;
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
use crate::db::types::ControllerUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize)]
 | 
			
		||||
pub struct ReturnSchedule {
 | 
			
		||||
| 
						 | 
				
			
			@ -12,22 +13,46 @@ pub struct ReturnSchedule {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
impl ReturnSchedule {
 | 
			
		||||
	pub fn load_tags(&mut self, conn: &mut PoolConnection<Sqlite>) {
 | 
			
		||||
		self.tags = executor::block_on(self.schedule.get_tags(conn)).unwrap();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
	pub fn from_schedule(schedule: Schedule, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		let schedule = schedule.clone();
 | 
			
		||||
		let tags = executor::block_on(schedule.get_tags(conn)).unwrap();
 | 
			
		||||
 | 
			
		||||
impl From<Schedule> for ReturnSchedule {
 | 
			
		||||
	fn from(schedule: Schedule) -> Self {
 | 
			
		||||
		ReturnSchedule {
 | 
			
		||||
			schedule,
 | 
			
		||||
			tags: vec![],
 | 
			
		||||
			tags,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn from_schedule_ref(schedule: &Schedule, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		Self::from_schedule(schedule.clone(), conn)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&Schedule> for ReturnSchedule {
 | 
			
		||||
	fn from(schedule: &Schedule) -> Self {
 | 
			
		||||
		ReturnSchedule::from(schedule.clone())
 | 
			
		||||
#[derive(Debug, Serialize)]
 | 
			
		||||
pub struct ReturnRelay {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub relay: Relay,
 | 
			
		||||
	pub controller: Controller,
 | 
			
		||||
	pub controller_id: ControllerUid,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ReturnRelay {
 | 
			
		||||
	pub fn from_relay(relay: Relay, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		let relay = relay.clone();
 | 
			
		||||
		let controller = executor::block_on(Controller::get(conn, relay.controller_id)).unwrap();
 | 
			
		||||
		let controller_uid = controller.uid.clone();
 | 
			
		||||
		let tags = executor::block_on(relay.get_tags(conn)).unwrap();
 | 
			
		||||
 | 
			
		||||
		ReturnRelay {
 | 
			
		||||
			relay,
 | 
			
		||||
			controller,
 | 
			
		||||
			controller_id: controller_uid,
 | 
			
		||||
			tags,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn from_relay_ref(relay: &Relay, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		Self::from_relay(relay.clone(), conn)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,3 @@
 | 
			
		|||
pub fn vec_has_error<T, E>(target: &[Result<T, E>]) -> bool {
 | 
			
		||||
	target.iter().any(|t| t.is_err())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn load_settings<T>(config_name: &str, env_prefix: &str) -> T
 | 
			
		||||
where
 | 
			
		||||
	for<'de> T: serde::Deserialize<'de>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue