chore: list animations
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2023-07-10 12:41:46 +01:00
parent c25a8d633e
commit c1b3625a14
3 changed files with 74 additions and 23 deletions

View File

@ -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<DBPool>) -> 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<Data> = animations.into_iter().map(|a| a.unwrap()).collect();
json!(data).to_string()
}
// Add animation
#[post("/animation", data = "<data>")]
pub async fn animation(data: Json<Animation>, db: &State<DBPool>) -> String {

View File

@ -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<LightConfigurationRequest>, db: &State<DBPool>
json!(ApiResponse {code: 200, message: "Configuration was successful".to_string()}).to_string()
}
type Conn = PooledConnection<SqliteConnectionManager>;
fn get_conn(db: &State<DBPool>) -> Result<Conn, String> {
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<Statement<'a>, 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<bool, String> {
let mut stmt = prepare_statement(conn, "SELECT configured from meta;")?;

View File

@ -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<SqliteConnectionManager>;
pub fn get_conn(db: &State<DBPool>) -> Result<Conn, String> {
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<Statement<'a>, 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);
}