Add endpoints for tags

This commit is contained in:
Tobias Reisinger 2023-11-30 03:24:13 +01:00
parent c8f40284ef
commit 8d996888bd
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
10 changed files with 141 additions and 18 deletions

View file

@ -1,4 +1,5 @@
pub mod controllers;
pub mod relays;
pub mod schedules;
pub mod tags;
pub mod ws;

View file

@ -1,6 +1,8 @@
use actix_web::{get, web, HttpResponse};
use emgauwa_lib::db::DbRelay;
use emgauwa_lib::models::{convert_db_list, Relay};
use actix_web::{get, put, web, HttpResponse};
use emgauwa_lib::db::errors::DatabaseError;
use emgauwa_lib::db::{DbController, DbRelay, DbTag};
use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay};
use emgauwa_lib::types::ControllerUid;
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
@ -22,3 +24,90 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
Ok(HttpResponse::Ok().json(relays))
}
#[get("/api/v1/relays/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 = DbTag::get_by_tag(&mut pool_conn, &tag)
.await?
.ok_or(DatabaseError::NotFound)?;
let db_relays = DbRelay::get_by_tag(&mut pool_conn, &tag_db).await?;
let relays: Vec<Relay> = convert_db_list(&mut pool_conn, db_relays)?;
Ok(HttpResponse::Ok().json(relays))
}
#[get("/api/v1/controllers/{controller_id}/relays")]
pub async fn index_for_controller(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid,) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
.await?
.ok_or(DatabaseError::NotFound)?;
let db_relays = controller.get_relays(&mut pool_conn).await?;
let relays: Vec<Relay> = convert_db_list(&mut pool_conn, db_relays)?;
Ok(HttpResponse::Ok().json(relays))
}
#[get("/api/v1/controllers/{controller_id}/relays/{relay_num}")]
pub async fn show_for_controller(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid, relay_num) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
.await?
.ok_or(DatabaseError::NotFound)?;
let relay = DbRelay::get_by_controller_and_num(&mut pool_conn, &controller, relay_num)
.await?
.ok_or(DatabaseError::NotFound)?;
let return_relay = Relay::from_db_model(&mut pool_conn, relay)?;
Ok(HttpResponse::Ok().json(return_relay))
}
#[put("/api/v1/controllers/{controller_id}/relays/{relay_num}")]
pub async fn update_for_controller(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String, i64)>,
data: web::Json<RequestRelay>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid, relay_num) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
.await?
.ok_or(DatabaseError::NotFound)?;
let relay = DbRelay::get_by_controller_and_num(&mut pool_conn, &controller, relay_num)
.await?
.ok_or(DatabaseError::NotFound)?;
let relay = relay.update(&mut pool_conn, data.name.as_str()).await?;
relay.set_tags(&mut pool_conn, data.tags.as_slice()).await?;
let return_relay = Relay::from_db_model(&mut pool_conn, relay)?;
Ok(HttpResponse::Ok().json(return_relay))
}

View file

@ -13,7 +13,6 @@ use crate::handlers::errors::ApiError;
pub struct RequestSchedule {
name: String,
periods: DbPeriods,
#[serde(default)] // empty tags are allowed
tags: Vec<String>,
}

View file

@ -0,0 +1,16 @@
use actix_web::{get, web, HttpResponse};
use emgauwa_lib::db::DbTag;
use sqlx::{Pool, Sqlite};
use crate::handlers::errors::ApiError;
#[get("/api/v1/tags")]
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let db_tags = DbTag::get_all(&mut pool_conn).await?;
let tags: Vec<String> = db_tags.iter().map(|t| t.tag.clone()).collect();
Ok(HttpResponse::Ok().json(tags))
}

View file

@ -106,9 +106,7 @@ impl ControllerWs {
}
}
/// helper method that sends ping to client every 5 seconds (HEARTBEAT_INTERVAL).
///
/// also this method checks heartbeats from client
// helper method that sends ping to client every 5 seconds (HEARTBEAT_INTERVAL).
fn hb(&self, ctx: &mut ws::WebsocketContext<Self>) {
ctx.run_interval(HEARTBEAT_INTERVAL, |act, ctx| {
// check client heartbeats

View file

@ -61,6 +61,10 @@ async fn main() -> std::io::Result<()> {
.service(handlers::v1::controllers::update)
.service(handlers::v1::controllers::delete)
.service(handlers::v1::relays::index)
.service(handlers::v1::relays::tagged)
.service(handlers::v1::relays::index_for_controller)
.service(handlers::v1::relays::show_for_controller)
.service(handlers::v1::relays::update_for_controller)
.service(handlers::v1::schedules::index)
.service(handlers::v1::schedules::tagged)
.service(handlers::v1::schedules::show)
@ -68,6 +72,7 @@ async fn main() -> std::io::Result<()> {
.service(handlers::v1::schedules::add_list)
.service(handlers::v1::schedules::update)
.service(handlers::v1::schedules::delete)
.service(handlers::v1::tags::index)
.service(handlers::v1::ws::ws_controllers)
})
.listen(listener)?