|
| 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 | +} |
0 commit comments