Skip to content

Commit

Permalink
Add name, make location optional
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepdeJong committed Oct 2, 2023
1 parent 2473baa commit 823b232
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize)]
pub struct Pass {
pub name: String,
pub title: String,
pub description: String,
pub date: String,
pub time: String,
pub location: String,
pub location: Option<String>,
pub name: Option<String>,
pub code: String,
}

Expand Down
27 changes: 18 additions & 9 deletions src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ use wallet_pass::{
use crate::models::Pass as PassModel;

pub async fn passes_handler(Query(data): Query<PassModel>) -> Result<impl IntoResponse, String> {
match (data.validate_date(), data.validate_time()) {
(Ok(_), Ok(_)) => format!(
"Your pass for {} at {} on {} at {} has been confirmed.",
data.name, data.location, data.date, data.time
),
let error = match (data.validate_date(), data.validate_time()) {
(Ok(_), Ok(_)) => "".to_string(),
(Err(_), Ok(_)) => "Invalid date format. Please use yyyy-mm-dd.".to_string(),
(Ok(_), Err(_)) => "Invalid time format. Please use HH:MM.".to_string(),
(Err(_), Err(_)) => {
Expand All @@ -22,6 +19,10 @@ pub async fn passes_handler(Query(data): Query<PassModel>) -> Result<impl IntoRe
}
};

if !error.is_empty() {
return Err(error);
}

println!(
"-- Loading template from {}",
env::var("TEMPLATE_PATH").unwrap()
Expand All @@ -43,7 +44,7 @@ pub async fn passes_handler(Query(data): Query<PassModel>) -> Result<impl IntoRe

let mut event_ticket = Details::new();

let mut field = Field::new_string("event", data.name.as_str());
let mut field = Field::new_string("event", data.title.as_str());
field.label("Event");
event_ticket.add_primary_field(field);

Expand All @@ -55,9 +56,17 @@ pub async fn passes_handler(Query(data): Query<PassModel>) -> Result<impl IntoRe
field.label("Time");
event_ticket.add_secondary_field(field);

let mut field = Field::new_string("loc", data.location.as_str());
field.label("Location");
event_ticket.add_auxiliary_field(field);
if let Some(location) = data.location {
let mut field = Field::new_string("loc", location.as_str());
field.label("Location");
event_ticket.add_auxiliary_field(field);
}

if let Some(name) = data.name {
let mut field = Field::new_string("name", name.as_str());
field.label("Name");
event_ticket.add_auxiliary_field(field);
}

pass.event_ticket(event_ticket);

Expand Down

0 comments on commit 823b232

Please sign in to comment.