diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-09-06 17:58:35 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-06 17:58:35 +0000 |
| commit | acc32ccc1650b99e03c390a87e71b0a85b073162 (patch) | |
| tree | 0de545ee0eb8eba5961c57c0a73ff7031e97613d /src/models/model.rs | |
| parent | e6dbcbb42e7a4a973887acde633cc3ee233aad3e (diff) | |
Create a 3d model object
Diffstat (limited to 'src/models/model.rs')
| -rw-r--r-- | src/models/model.rs | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/models/model.rs b/src/models/model.rs new file mode 100644 index 0000000..1fe9b61 --- /dev/null +++ b/src/models/model.rs @@ -0,0 +1,105 @@ +use crate::db::get_client; +use crate::errors::AppError; + +use chrono::{Local, NaiveDateTime}; +use serde::{Deserialize, Serialize}; +use validator::Validate; + +/// Model for models. +#[derive(Deserialize, Serialize, Validate)] +pub struct Model { + id: i32, + #[validate(length(min = 2, message = "Can not be empty"))] + name: String, + description: Option<String>, + duration: i32, + height: i32, + weight: i32, + printer: Option<String>, + material: Option<String>, + author_id: i32, + created: NaiveDateTime, + updated: NaiveDateTime, +} + +/// Payload used for model creation +#[derive(Deserialize)] +pub struct ModelCreate { + pub name: String, + pub description: Option<String>, + pub duration: i32, + pub height: i32, + pub weight: i32, + pub printer: Option<String>, + pub material: Option<String>, +} + +impl Model { + pub fn new( + name: String, + description: Option<String>, + duration: i32, + height: i32, + weight: i32, + printer: Option<String>, + material: Option<String>, + author_id: i32, + ) -> Self { + let now = Local::now().naive_utc(); + Self { + id: 0, + name, + description, + duration, + height, + weight, + printer, + material, + author_id, + created: now, + updated: now, + } + } + + /// Create a new model + pub async fn create(model: Model) -> Result<Model, AppError> { + let pool = unsafe { get_client() }; + + model + .validate() + .map_err(|error| AppError::BadRequest(error.to_string()))?; + + let rec = sqlx::query_as!( + Model, + r#" + INSERT INTO models (name, description, duration, height, weight, printer, material, author_id, created, updated) + VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + RETURNING * + "#, + model.name, + model.description, + model.duration, + model.height, + model.weight, + model.printer, + model.material, + model.author_id, + model.created, + model.updated, + ) + .fetch_one(pool) + .await?; + + Ok(rec) + } + + /// List all models + pub async fn list() -> Result<Vec<Model>, AppError> { + let pool = unsafe { get_client() }; + let rows = sqlx::query_as!(Model, r#"SELECT * FROM models"#) + .fetch_all(pool) + .await?; + + Ok(rows) + } +} |
