diff --git a/src/routes.rs b/src/routes.rs index 26185f0..9620089 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -7,6 +7,7 @@ use actix_web::web::{Data, Form, Path}; use actix_web::HttpMessage; use actix_web::{get, post, HttpRequest, HttpResponse, Responder}; use askama::Template; +use askama_actix::TemplateToResponse; use sea_orm::{prelude::*, QuerySelect}; use serde::Deserialize; @@ -62,6 +63,9 @@ struct RecipeForm { ingeredients: String, steps: String, tags: String, + image_url: String, + time: Option, + author: Option, error: Option, session: Option, page: Page, @@ -287,6 +291,9 @@ async fn recipe_form(admin: Identity) -> RecipeForm { ingeredients: "".into(), steps: "".into(), tags: "".into(), + image_url: "".into(), + author: None, + time: None, error: None, session: admin.id().ok(), page: Page::Index, @@ -295,29 +302,45 @@ async fn recipe_form(admin: Identity) -> RecipeForm { #[post("/create")] async fn create_recipe( - admin: Identity, + _admin: Identity, form: Form, db: Data, -) -> RecipeForm { +) -> HttpResponse { let form = form.into_inner(); let mut failure = form.clone(); - let title = form.title; + match save_recipe(form, db.into_inner()).await { + Err(e) => { + failure.error = Some(e.to_string()); + failure.to_response() + } + Ok(_) => HttpResponse::TemporaryRedirect() + .append_header(("location", "/")) + .finish(), + } +} + +async fn save_recipe(form: RecipeForm, db: DatabaseConnection) -> Result<(), Error> { + use crate::entities::recipies::ActiveModel as RAM; + use sea_orm::ActiveValue::*; let mut summary = String::new(); let parser = pulldown_cmark::Parser::new(&form.summary); pulldown_cmark::html::push_html(&mut summary, parser); - let Ok(steps) = crate::utils::parse_steps(&form.steps, 0) else { - failure.error = Some("Invalid steps list".into()); - return failure; - }; - let Ok(ingeredients) = crate::utils::parse_steps(&form.ingeredients, 0) else { - failure.error = Some("Invalid ingeredients list".into()); - return failure; + let model = RAM { + title: Set(form.title), + image_url: Set(form.image_url), + author: Set(None), + time: Set(None), + summary: Set(Some(summary)), + ..Default::default() }; - failure + let steps = crate::utils::parse_steps(&form.steps, 0)?; + let ingeredients = crate::utils::parse_steps(&form.ingeredients, 0)?; + + Ok(()) } #[get("/styles.css")] diff --git a/src/utils.rs b/src/utils.rs index 664d88b..388efbf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,11 @@ use crate::entities::recipe_ingeredients::ActiveModel as RIAM; use crate::entities::recipe_steps::ActiveModel as RSAM; +use crate::types::Error; use crate::types::Error::*; use sea_orm::prelude::*; -use sea_orm::ActivrValue::*; +use sea_orm::ActiveValue::*; -pub fn parse_steps(s: &str, recipe_id: u64) -> Result, InvalidStepList> { +pub fn parse_steps(s: &str, recipe_id: i32) -> Result, Error> { s.lines() .filter(|s| !s.trim().is_empty()) .try_fold(Vec::new(), |mut v, line| { @@ -12,16 +13,15 @@ pub fn parse_steps(s: &str, recipe_id: u64) -> Result, InvalidStepList match line.chars().next() { Some('*') => { - let line = line.replacen("*", "", 1).trim(); v.push(RSAM { - body: Set(line.to_string()), + body: Set(line.replacen("*", "", 1).trim().to_string()), recipe_id: Set(recipe_id), ..Default::default() }); } Some('>') => { - let line = line.replacen(">", "", 1).trim(); - v.last_mut().ok_or(InvalidStepList)?.hint = Set(line.to_string()); + v.last_mut().ok_or(InvalidStepList)?.hint = + Set(Some(line.replacen(">", "", 1).trim().to_string())); } _ => return Err(InvalidStepList), }; @@ -30,7 +30,7 @@ pub fn parse_steps(s: &str, recipe_id: u64) -> Result, InvalidStepList }) } -pub fn parse_ingenedients(s: &str, recipe_id: u64) -> Result, InvalidIngeredientList> { +pub fn parse_ingenedients(s: &str, recipe_id: i32) -> Result, Error> { s.lines() .map(|s| s.trim()) .filter(|s| !s.is_empty()) @@ -43,7 +43,10 @@ pub fn parse_ingenedients(s: &str, recipe_id: u64) -> Result, InvalidI 0 | 1 => return Err(InvalidIngeredientList), // no unit 2 => { - let qty = pieces.remove(0).parse().map_err(|_| InvalidIngeredientList); + let qty = pieces + .remove(0) + .parse() + .map_err(|_| InvalidIngeredientList)?; Ok(RIAM { recipe_id: Set(recipe_id), name: Set(pieces.join(" ")), @@ -52,12 +55,15 @@ pub fn parse_ingenedients(s: &str, recipe_id: u64) -> Result, InvalidI }) } _ => { - let qty = pieces.remove(0).parse().map_err(|_| InvalidIngeredientList); + let qty = pieces + .remove(0) + .parse() + .map_err(|_| InvalidIngeredientList)?; let unit = pieces.remove(0); Ok(RIAM { recipe_id: Set(recipe_id), name: Set(pieces.join(" ")), - unit: Set(unit), + unit: Set(unit.to_string()), qty: Set(qty), ..Default::default() })