Skip to content

Commit 0e2f81d

Browse files
pohlm01marlonbaeten
authored andcommitted
Create first handlers for political groups
1 parent 651f179 commit 0e2f81d

File tree

11 files changed

+134
-8
lines changed

11 files changed

+134
-8
lines changed

src/candidate_lists/pages/update.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ mod tests {
129129
created_at: creation_date,
130130
updated_at: creation_date,
131131
};
132-
candidate_lists::create_candidate_list(&mut conn, &candidate_list).await?;
132+
let candidate_list =
133+
candidate_lists::create_candidate_list(&mut conn, &candidate_list).await?;
133134

134135
let form = CandidateListForm {
135136
electoral_districts: vec![ElectoralDistrict::DR],
@@ -170,10 +171,10 @@ mod tests {
170171
vec![ElectoralDistrict::DR],
171172
updated_list.electoral_districts
172173
);
173-
assert!(creation_date - Utc::now() < Duration::seconds(10));
174+
assert!((candidate_list.created_at - Utc::now()).abs() < Duration::seconds(10));
174175
// we don't know the exact update date
175176
// best we can do is to check it at least got updated (i.e. not equal to creation_date)
176-
assert_ne!(creation_date, updated_list.updated_at);
177+
assert_ne!(candidate_list.created_at, updated_list.updated_at);
177178

178179
Ok(())
179180
}

src/candidate_lists/structs/candidate_position.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use serde::{Deserialize, Serialize};
2-
use std::str::FromStr;
32
use strum::{Display, EnumString};
43

54
use crate::form::{TokenValue, WithCsrfToken};

src/common/new_type.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ macro_rules! id_newtype {
1919
fn from(v: uuid::Uuid) -> Self { Self(v) }
2020
}
2121

22+
impl std::str::FromStr for $name {
23+
type Err = uuid::Error;
24+
fn from_str(s: &str) -> Result<Self, Self::Err> {
25+
Ok(Self(uuid::Uuid::parse_str(s)?))
26+
}
27+
}
28+
2229
impl From<$name> for uuid::Uuid {
2330
fn from(v: $name) -> Self { v.0 }
2431
}

src/persons/pages/create.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ pub async fn new_person_form(
2727
person_pagination,
2828
},
2929
context,
30-
)
31-
.into_response())
30+
))
3231
}
3332

3433
pub async fn create_person(

src/political_group/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
mod pages;
12
mod repository;
23
mod structs;
4+
5+
pub use repository::*;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::{
2+
AppError, Context, DbConnection, HtmlTemplate,
3+
form::{FormData, Validate},
4+
political_group,
5+
political_group::{pages::PoliticalGroupNewPath, structs::PoliticalGroupForm},
6+
};
7+
use askama::Template;
8+
use axum::{
9+
Form,
10+
response::{IntoResponse, Redirect, Response},
11+
};
12+
13+
#[derive(Template)]
14+
#[template(path = "political_group/create.html")]
15+
struct PoliticalGroupCreateTemplate {
16+
form: FormData<PoliticalGroupForm>,
17+
}
18+
19+
pub async fn new_political_group_form(
20+
_: PoliticalGroupNewPath,
21+
context: Context,
22+
) -> Result<impl IntoResponse, AppError> {
23+
Ok(HtmlTemplate(
24+
PoliticalGroupCreateTemplate {
25+
form: FormData::new(&context.csrf_tokens),
26+
},
27+
context,
28+
))
29+
}
30+
31+
pub async fn create_political_group(
32+
_: PoliticalGroupNewPath,
33+
context: Context,
34+
DbConnection(mut conn): DbConnection,
35+
Form(form): Form<PoliticalGroupForm>,
36+
) -> Result<Response, AppError> {
37+
match form.validate_create(&context.csrf_tokens) {
38+
Err(form) => {
39+
Ok(HtmlTemplate(PoliticalGroupCreateTemplate { form }, context).into_response())
40+
}
41+
Ok(political_group) => {
42+
political_group::create_political_group(&mut conn, &political_group).await?;
43+
44+
Ok(Redirect::to("/").into_response())
45+
}
46+
}
47+
}

src/political_group/pages/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use axum_extra::routing::TypedPath;
2+
3+
mod create;
4+
5+
#[derive(TypedPath)]
6+
#[typed_path("/political_group/new", rejection(crate::AppError))]
7+
pub struct PoliticalGroupNewPath;

src/political_group/structs/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ mod authorized_agent_form;
33
mod list_submitter;
44
mod list_submitter_form;
55
mod political_group;
6+
mod political_group_form;
67

78
pub use authorized_agent::{AuthorisedAgent, AuthorisedAgentId};
9+
pub use authorized_agent_form::AuthorisedAgentForm;
810
pub use list_submitter::{ListSubmitter, ListSubmitterId};
11+
pub use list_submitter_form::ListSubmitterForm;
912
pub use political_group::{PoliticalGroup, PoliticalGroupId};
13+
pub use political_group_form::PoliticalGroupForm;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::{
2+
TokenValue,
3+
form::*,
4+
political_group::structs::{
5+
AuthorisedAgentId, ListSubmitterId, PoliticalGroup, PoliticalGroupId,
6+
},
7+
};
8+
use chrono::Utc;
9+
use serde::Deserialize;
10+
use validate::Validate;
11+
12+
#[derive(Default, Deserialize, Debug, Validate)]
13+
#[validate(
14+
target = "PoliticalGroup",
15+
build = "PoliticalGroupForm::build_political_group"
16+
)]
17+
pub struct PoliticalGroupForm {
18+
#[validate(with = "validate_length(2, 255)")]
19+
legal_name: String,
20+
#[validate(with = "validate_length(2, 255)")]
21+
display_name: String,
22+
#[validate(parse = "AuthorisedAgentId", optional)]
23+
authorised_agent_id: String,
24+
#[validate(parse = "ListSubmitterId", optional)]
25+
list_submitter_id: String,
26+
#[validate(csrf)]
27+
pub csrf_token: TokenValue,
28+
}
29+
30+
impl WithCsrfToken for PoliticalGroupForm {
31+
fn with_csrf_token(self, csrf_token: CsrfToken) -> Self {
32+
PoliticalGroupForm {
33+
csrf_token: csrf_token.value,
34+
..self
35+
}
36+
}
37+
}
38+
39+
impl PoliticalGroupForm {
40+
fn build_political_group(
41+
validated: PoliticalGroupFormValidated,
42+
current: Option<PoliticalGroup>,
43+
) -> PoliticalGroup {
44+
if let Some(current) = current {
45+
current
46+
} else {
47+
PoliticalGroup {
48+
id: PoliticalGroupId::new(),
49+
50+
legal_name: validated.legal_name,
51+
display_name: validated.display_name,
52+
authorised_agent_id: validated.authorised_agent_id,
53+
list_submitter_id: validated.list_submitter_id,
54+
created_at: Utc::now(),
55+
updated_at: Utc::now(),
56+
}
57+
}
58+
}
59+
}

templates/political_group/create.html

Whitespace-only changes.

0 commit comments

Comments
 (0)