Skip to content

Commit

Permalink
feat(backend): Add description to Form
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayan-724 committed Sep 14, 2024
1 parent 8a63a5a commit 7300a88
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
22 changes: 10 additions & 12 deletions crates/backend/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,18 +90,18 @@ pub async fn post(req: Request, ctx: RouterContext) -> WorkerHttpResponse {

let body = get_body::<FormCreate>(&mut req).await?;

let Some(new_form) = D1EntityCreate::create_query(body, &db)
let new_form = D1EntityCreate::create_query(body, &db)
.all::<FormJs>()
.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;
Expand Down
13 changes: 11 additions & 2 deletions crates/models/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,

Expand All @@ -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,
}
Expand All @@ -65,6 +69,7 @@ pub struct FormUpdate {
pub id: usize,
pub title: Option<String>,
pub require_login: Option<bool>,
pub description: Option<String>,
pub edition: Option<String>,
pub multiple_times: Option<bool>,
}
Expand All @@ -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,

Expand All @@ -104,6 +110,7 @@ impl From<FormJs> for Form {
title,
require_login,
deleted,
description,
edition,
created_at,
multiple_times,
Expand All @@ -114,6 +121,7 @@ impl From<FormJs> 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(),
Expand All @@ -122,14 +130,15 @@ impl From<FormJs> 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;
];
Expand Down
4 changes: 4 additions & 0 deletions migrations/0005_forms_description.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Migration number: 0005 2024-09-14T04:24:41.600Z

ALTER TABLE Form
ADD COLUMN description String DEFAULT "";

0 comments on commit 7300a88

Please sign in to comment.