Skip to content

Commit 12985bf

Browse files
committed
employee: Better dob
1 parent fadee44 commit 12985bf

File tree

8 files changed

+48
-24
lines changed

8 files changed

+48
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
2424
url = "2.5.4"
2525
headers = "0.4.0"
2626
once_cell = "1.20.2"
27+
time = "0.3.37"

backend/src/employee.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use sqlx::*;
2+
use sqlx::types::time::Date;
23

34
#[derive(Debug, Clone, sqlx::FromRow)]
45
pub struct Employee {
56
pub id: i64,
67
pub email: String,
78
pub name: String,
8-
pub dob_month: Option<i32>,
9-
pub dob_day: Option<i32>,
9+
pub dob: Option<Date>
1010
// pub some_accounts: Vec<SomeAccount>,
1111
}
1212

@@ -23,7 +23,7 @@ impl EmployeeDao {
2323
pub(crate) async fn employees(&self) -> Result<Vec<Employee>, Error> {
2424
sqlx::query_as!(
2525
Employee,
26-
"SELECT id, email, name, dob_month, dob_day FROM skjera.employee"
26+
"SELECT id, email, name, dob FROM skjera.employee"
2727
)
2828
.fetch_all(&self.pool)
2929
.await
@@ -32,7 +32,7 @@ impl EmployeeDao {
3232
pub(crate) async fn employee_by_id(&self, id: i64) -> Result<Option<Employee>, Error> {
3333
sqlx::query_as!(
3434
Employee,
35-
"SELECT id, email, name, dob_month, dob_day FROM skjera.employee WHERE id=$1",
35+
"SELECT id, email, name, dob FROM skjera.employee WHERE id=$1",
3636
id
3737
)
3838
.fetch_optional(&self.pool)
@@ -42,7 +42,7 @@ impl EmployeeDao {
4242
pub(crate) async fn employee_by_email(&self, email: String) -> Result<Option<Employee>, Error> {
4343
sqlx::query_as!(
4444
Employee,
45-
"SELECT id, email, name, dob_month, dob_day FROM skjera.employee WHERE email=$1",
45+
"SELECT id, email, name, dob FROM skjera.employee WHERE email=$1",
4646
email
4747
)
4848
.fetch_optional(&self.pool)
@@ -52,10 +52,9 @@ impl EmployeeDao {
5252
pub(crate) async fn update(&self, employee: &Employee) -> Result<Employee, Error> {
5353
sqlx::query_as!(
5454
Employee,
55-
"UPDATE skjera.employee SET dob_month=$1, dob_day=$2 WHERE id=$3
56-
RETURNING id, email, name, dob_month, dob_day",
57-
employee.dob_month,
58-
employee.dob_day,
55+
"UPDATE skjera.employee SET dob=$1 WHERE id=$2
56+
RETURNING id, email, name, dob",
57+
employee.dob,
5958
employee.id,
6059
)
6160
.fetch_one(&self.pool)

backend/src/html.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use axum::response::{Html, Redirect};
77
use axum::Form;
88
use once_cell::sync::Lazy;
99
use serde::Deserialize;
10+
use time::{format_description, Date, Month};
1011
use tracing::debug;
1112
use url;
1213
use url::Url;
@@ -50,17 +51,17 @@ pub async fn get_me(
5051
let template = MeTemplate {
5152
month_names: MONTH_NAMES.as_slice(),
5253
days: (1..31).collect::<Vec<i32>>(),
53-
dob_month: me.dob_month.unwrap_or_default().try_into().unwrap_or_default(),
54-
dob_day: me.dob_day.unwrap_or_default().try_into().unwrap_or_default(),
54+
dob_month: me.dob.map(|d| d.month() as usize).unwrap_or_default(),
55+
dob_day: me.dob.map(|d| d.day() as usize).unwrap_or_default(),
5556
};
5657

5758
Ok(Html(template.render()?))
5859
}
5960

6061
#[derive(Deserialize, Debug)]
6162
pub(crate) struct MeForm {
62-
dob_month: i32,
63-
dob_day: i32,
63+
dob_month: u8,
64+
dob_day: u8,
6465
}
6566

6667
pub async fn post_me(
@@ -76,14 +77,19 @@ pub async fn post_me(
7677
.await?
7778
.context("error loading me")?;
7879

79-
if input.dob_month >= 1 && input.dob_day >= 1 {
80-
me.dob_month = Some(input.dob_month);
81-
me.dob_day = Some(input.dob_day);
80+
let year = me.dob.map(|dob| dob.year()).unwrap_or_else(|| 1900);
81+
let month: Option<Month> = input.dob_month.try_into().ok();
8282

83-
me = app.employee_dao.update(&me).await?;
83+
let dob: Option<Date> = match (month, input.dob_day) {
84+
(Some(m), d) if d >= 1 => Date::from_calendar_date(year, m, d).ok(),
85+
_ => None,
86+
};
8487

85-
debug!("Updated Employee: {:?}", me);
86-
}
88+
me.dob = dob;
89+
90+
me = app.employee_dao.update(&me).await?;
91+
92+
debug!("Updated Employee: {:?}", me);
8793

8894
Ok(Redirect::to("/"))
8995
}
@@ -96,8 +102,12 @@ struct EmployeeTemplate {
96102

97103
impl EmployeeTemplate {
98104
pub fn dob(&self) -> String {
99-
match (self.employee.dob_month, self.employee.dob_day) {
100-
(Some(m), Some(d)) => format!("{}-{}", m, d),
105+
let f = format_description::parse("[year]-[month]-[day]")
106+
.ok()
107+
.unwrap();
108+
109+
match self.employee.dob {
110+
Some(dob) => dob.format(&f).ok().unwrap_or_default(),
101111
_ => "".to_string(),
102112
}
103113
}

backend/src/skjera.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Skjera for ServerImpl {
1616
host: Host,
1717
cookies: CookieJar,
1818
) -> Result<ListEmployeesResponse, String> {
19-
let employees = sqlx::query_as!(Employee, "SELECT id, email, name, dob_month, dob_day FROM skjera.employee")
19+
let employees = sqlx::query_as!(Employee, "SELECT id, email, name, dob FROM skjera.employee")
2020
.fetch_all(&self.pool)
2121
.await
2222
.map_err(|e| {

backend/templates/hello.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ <h2>Employees</h2>
2727
<a href="/employee/{{ employee.id }}">{{ employee.name }}</a>
2828
</td>
2929
<td>
30-
{{ employee.dob_day|fmt("{:?}") }} {{ employee.dob_month|fmt("{:?}") }}
30+
{% if let Some(dob) = employee.dob %}
31+
{{ dob.day()|fmt("{:?}") }}{{ dob.month()|fmt("{:?}") }}
32+
{% endif %}
3133
</td>
3234
</tr>
3335
{% endfor %}

backend/templates/me.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<h1>Edit profile</h1>
88
<p>
99
month: {{ dob_month }}<br>
10-
day: {{ dob_day }}
10+
day: {{ dob_day }}
1111
</p>
1212
<form action="/me" method="POST">
1313
<label for="dob_month">Month of birth</label>

migrations/20241231152001_dob.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ALTER TABLE skjera.employee
2+
DROP COLUMN IF EXISTS dob_month,
3+
DROP COLUMN IF EXISTS dob_day,
4+
DROP COLUMN IF EXISTS dob;
5+
6+
ALTER TABLE skjera.employee
7+
ADD COLUMN dob DATE;
8+
9+
UPDATE skjera.employee
10+
SET dob = '1980-12-09'
11+
WHERE email = 'trygvis@scienta.no';

0 commit comments

Comments
 (0)