From c1b3625a14f9dfd21b96ed348a9a932851af43a4 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Mon, 10 Jul 2023 12:41:46 +0100 Subject: [PATCH] chore: list animations --- src/animations.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++- src/configure.rs | 22 +--------------------- src/utils.rs | 27 +++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 23 deletions(-) diff --git a/src/animations.rs b/src/animations.rs index 2b5f616..70c49ee 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -8,7 +8,7 @@ use rocket::{ State, }; -use crate::{DBPool, utils::{get_error_message, create_message}}; +use crate::{DBPool, utils::{get_error_message, create_message, get_conn, prepare_statement}}; use crate::{ utils::{ApiResponse, ApiResponseActiveList}, AReceiver, ASender, Action, DBConn, RenderMessage, @@ -100,6 +100,52 @@ impl Animation { } } + +#[get("/animations")] +pub async fn get_animations(db: &State) -> String { + let conn = get_conn(db); + if let Err(e) = conn { + return e; + } + let conn = conn.unwrap(); + + let stmt = prepare_statement(&conn, "select id,priority,name,repeat from animation;"); + if let Err(e) = stmt { + return e; + } + let mut stmt = stmt.unwrap(); + + #[derive(Deserialize, Serialize)] + #[serde(crate = "rocket::serde")] + #[serde(rename_all = "camelCase")] + struct Data { + id: u32, + priority: u32, + name: String, + repeat: bool, + } + + let animations = stmt.query_map([], |row| { + let repeat: u32 = row.get(3)?; + Ok(Data { + id: row.get(0)?, + priority: row.get(1)?, + name: row.get(2)?, + repeat: 1 == repeat, + }) + }); + + if animations.is_err() || animations.as_ref().ok().is_none() { + return json!(ApiResponse {code: 500, message: "Clould not get data of db".to_string()}).to_string(); + } + + let animations = animations.unwrap(); + + let data: Vec = animations.into_iter().map(|a| a.unwrap()).collect(); + + json!(data).to_string() +} + // Add animation #[post("/animation", data = "")] pub async fn animation(data: Json, db: &State) -> String { diff --git a/src/configure.rs b/src/configure.rs index 8058b43..6823476 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -2,7 +2,7 @@ use r2d2::PooledConnection; use r2d2_sqlite::{SqliteConnectionManager, rusqlite::Statement}; use rocket::{serde::{json::{Json, serde_json::json}, Deserialize, Serialize}, State}; -use crate::{utils::{ApiResponse, get_error_message}, Action, ASender}; +use crate::{utils::{ApiResponse, get_error_message, Conn, get_conn, prepare_statement}, Action, ASender}; use crate::DBPool; #[derive(Debug, Deserialize, Serialize, Clone)] @@ -75,26 +75,6 @@ pub async fn configure(data: Json, db: &State json!(ApiResponse {code: 200, message: "Configuration was successful".to_string()}).to_string() } -type Conn = PooledConnection; - -fn get_conn(db: &State) -> Result { - let conn = db.get(); - if let Ok(conn) = conn { - return Ok(conn); - } - Err(json!(ApiResponse {code: 500, message: "Clould not conncect to the db.".to_string()}).to_string()) -} - -fn prepare_statement<'a>(conn: &'a Conn, stat: &str) -> Result, String> { - let stmt = conn.prepare(stat); - if stmt.is_err() || stmt.as_ref().ok().is_none() { - println!("stmt: {}", stat); - return Err(get_error_message("Could not construct statetment")); - } - let stmt: Statement<'a> = stmt.unwrap(); - return Ok(stmt); -} - fn is_configured(conn: &Conn) -> Result { let mut stmt = prepare_statement(conn, "SELECT configured from meta;")?; diff --git a/src/utils.rs b/src/utils.rs index 9455fa4..9a2ef33 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,8 @@ -use rocket::{serde::{Deserialize, Serialize, json::{self, serde_json::json}}, Responder}; +use r2d2::{PooledConnection}; +use r2d2_sqlite::{SqliteConnectionManager, rusqlite::Statement}; +use rocket::{serde::{Deserialize, Serialize, json::{self, serde_json::json}}, Responder, State}; + +use crate::DBPool; #[derive(Deserialize, Serialize)] #[serde(crate="rocket::serde")] @@ -25,3 +29,24 @@ pub fn create_message(code: u32, message: &str) -> String { pub fn get_error_message(message: &str) -> String { return create_message(500, message); } + +pub type Conn = PooledConnection; + +pub fn get_conn(db: &State) -> Result { + let conn = db.get(); + if let Ok(conn) = conn { + return Ok(conn); + } + Err(json!(ApiResponse {code: 500, message: "Clould not conncect to the db.".to_string()}).to_string()) +} + +pub fn prepare_statement<'a>(conn: &'a Conn, stat: &str) -> Result, String> { + let stmt = conn.prepare(stat); + if stmt.is_err() || stmt.as_ref().ok().is_none() { + println!("stmt: {}", stat); + return Err(get_error_message("Could not construct statetment")); + } + let stmt: Statement<'a> = stmt.unwrap(); + return Ok(stmt); +} +