Improve tag endpoint
This commit is contained in:
		
							parent
							
								
									8215461e0d
								
							
						
					
					
						commit
						bb76e3db4d
					
				
					 7 changed files with 147 additions and 12 deletions
				
			
		| 
						 | 
				
			
			@ -63,4 +63,25 @@ impl DbTag {
 | 
			
		|||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		filter_tag: &str,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		if sqlx::query_scalar!("SELECT 1 FROM tags WHERE tag = ?", filter_tag)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await?
 | 
			
		||||
			.is_none()
 | 
			
		||||
		{
 | 
			
		||||
			return Err(DatabaseError::NotFound);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		sqlx::query!("DELETE FROM tags WHERE tag = ?", filter_tag)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
				_ => Ok(()),
 | 
			
		||||
			})?
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,12 +1,14 @@
 | 
			
		|||
mod controller;
 | 
			
		||||
mod relay;
 | 
			
		||||
mod schedule;
 | 
			
		||||
mod tag;
 | 
			
		||||
 | 
			
		||||
pub use controller::Controller;
 | 
			
		||||
pub use relay::Relay;
 | 
			
		||||
pub use schedule::Schedule;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
pub use tag::Tag;
 | 
			
		||||
 | 
			
		||||
use crate::errors::DatabaseError;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										49
									
								
								emgauwa-lib/src/models/tag.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								emgauwa-lib/src/models/tag.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,49 @@
 | 
			
		|||
use actix::MessageResponse;
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbRelay, DbSchedule, DbTag};
 | 
			
		||||
use crate::errors::DatabaseError;
 | 
			
		||||
use crate::models::{convert_db_list, FromDbModel, Relay, Schedule};
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone, MessageResponse)]
 | 
			
		||||
pub struct Tag {
 | 
			
		||||
	pub tag: String,
 | 
			
		||||
	pub relays: Vec<Relay>,
 | 
			
		||||
	pub schedules: Vec<Schedule>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Tag {
 | 
			
		||||
	type DbModel = DbTag;
 | 
			
		||||
	type DbModelCache = (Vec<Relay>, Vec<Schedule>);
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let db_schedules = block_on(DbSchedule::get_by_tag(conn, &db_model))?;
 | 
			
		||||
		let schedules: Vec<Schedule> = convert_db_list(conn, db_schedules)?;
 | 
			
		||||
 | 
			
		||||
		let db_relays = block_on(DbRelay::get_by_tag(conn, &db_model))?;
 | 
			
		||||
		let relays: Vec<Relay> = convert_db_list(conn, db_relays)?;
 | 
			
		||||
 | 
			
		||||
		let cache = (relays, schedules);
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let tag = db_model.tag.clone();
 | 
			
		||||
		let (relays, schedules) = cache;
 | 
			
		||||
		Ok(Tag {
 | 
			
		||||
			tag,
 | 
			
		||||
			relays,
 | 
			
		||||
			schedules,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -38,6 +38,11 @@ pub struct RequestUpdateController {
 | 
			
		|||
	pub name: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestCreateTag {
 | 
			
		||||
	pub tag: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl RequestScheduleId {
 | 
			
		||||
	pub async fn get_schedule(
 | 
			
		||||
		&self,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue