diff --git a/crates/backend/src/form.rs b/crates/backend/src/form.rs index 3c645b7..db6fcb3 100644 --- a/crates/backend/src/form.rs +++ b/crates/backend/src/form.rs @@ -8,9 +8,7 @@ use forms_models::{ D1EntityQueries, }; use forms_shared::db::D1Action; -use forms_shared::{ - get_auth, get_body, string_into_response, FormsResponse, IntoResponse, WorkerHttpResponse, -}; +use forms_shared::{get_auth, get_body, FormsResponse, WorkerHttpResponse}; use crate::admins::needs_admin; use crate::session::get_device_id_hash; @@ -92,18 +90,18 @@ pub async fn post(req: Request, ctx: RouterContext) -> WorkerHttpResponse { let body = get_body::(&mut req).await?; - let Some(new_form) = D1EntityCreate::create_query(body, &db) + let new_form = D1EntityCreate::create_query(body, &db) .all::() - .await? - .first() else { - FormsResponse::json( - 201, + .await?; + + let Some(new_form) = new_form.first() else { + return FormsResponse::json( + 500, &serde_json::json!({ - "errors": [], - "success": true, - "data": new_form_id + "errors": ["Can't create form"], + "success": false, }), - ) + ); }; let new_form_id = new_form.id; diff --git a/crates/models/src/form.rs b/crates/models/src/form.rs index 0295e39..1449458 100644 --- a/crates/models/src/form.rs +++ b/crates/models/src/form.rs @@ -7,6 +7,7 @@ use crate::question::QuestionDetails; pub struct FormJs { pub id: usize, pub title: String, + pub description: String, pub require_login: u8, pub deleted: u8, pub edition: String, @@ -20,6 +21,7 @@ pub struct Form { pub id: usize, pub title: String, + pub description: String, pub edition: String, pub multiple_times: bool, pub require_login: bool, @@ -41,6 +43,7 @@ pub struct FormDetails { pub title: String, pub require_login: bool, pub deleted: bool, + pub description: String, pub edition: String, pub multiple_times: bool, @@ -56,6 +59,7 @@ pub struct FormDetails { pub struct FormCreate { pub title: String, pub require_login: bool, + pub description: String, pub edition: String, pub multiple_times: bool, } @@ -65,6 +69,7 @@ pub struct FormUpdate { pub id: usize, pub title: Option, pub require_login: Option, + pub description: Option, pub edition: Option, pub multiple_times: Option, } @@ -87,6 +92,7 @@ impl Form { require_login: self.require_login, deleted: self.deleted, multiple_times: self.multiple_times, + description: self.description, edition: self.edition, created_at: self.created_at, @@ -104,6 +110,7 @@ impl From for Form { title, require_login, deleted, + description, edition, created_at, multiple_times, @@ -114,6 +121,7 @@ impl From for Form { title, require_login: require_login == 1, deleted: deleted == 1, + description, edition, multiple_times: multiple_times == 1, created_at: time::OffsetDateTime::from_unix_timestamp(created_at).unwrap(), @@ -122,14 +130,15 @@ impl From for Form { } create_queries! { - Form where select_all = [ id, title, require_login, deleted, created_at, edition, multiple_times ], + Form where select_all = [ id, title, require_login, deleted, created_at, description, edition, multiple_times ], FormRead where select = with form; [ form.id; ], - FormCreate where create = with form; [ form.title, form.require_login, form.edition, form.multiple_times, ], + FormCreate where create = with form; [ form.title, form.require_login, form.description, form.edition, form.multiple_times, ], FormUpdate where update = with form; { where = [ form.id; ]; set = [ &form?.title; form?.require_login; + &form?.description; &form?.edition; form?.multiple_times; ]; diff --git a/migrations/0005_forms_description.sql b/migrations/0005_forms_description.sql new file mode 100644 index 0000000..8591bf3 --- /dev/null +++ b/migrations/0005_forms_description.sql @@ -0,0 +1,4 @@ +-- Migration number: 0005 2024-09-14T04:24:41.600Z + +ALTER TABLE Form + ADD COLUMN description String DEFAULT "";