Skip to content

Commit

Permalink
employee: Better dob
Browse files Browse the repository at this point in the history
  • Loading branch information
trygvis committed Dec 31, 2024
1 parent fadee44 commit 12985bf
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
url = "2.5.4"
headers = "0.4.0"
once_cell = "1.20.2"
time = "0.3.37"
17 changes: 8 additions & 9 deletions backend/src/employee.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use sqlx::*;
use sqlx::types::time::Date;

#[derive(Debug, Clone, sqlx::FromRow)]
pub struct Employee {
pub id: i64,
pub email: String,
pub name: String,
pub dob_month: Option<i32>,
pub dob_day: Option<i32>,
pub dob: Option<Date>
// pub some_accounts: Vec<SomeAccount>,
}

Expand All @@ -23,7 +23,7 @@ impl EmployeeDao {
pub(crate) async fn employees(&self) -> Result<Vec<Employee>, Error> {
sqlx::query_as!(
Employee,
"SELECT id, email, name, dob_month, dob_day FROM skjera.employee"
"SELECT id, email, name, dob FROM skjera.employee"
)
.fetch_all(&self.pool)
.await
Expand All @@ -32,7 +32,7 @@ impl EmployeeDao {
pub(crate) async fn employee_by_id(&self, id: i64) -> Result<Option<Employee>, Error> {
sqlx::query_as!(
Employee,
"SELECT id, email, name, dob_month, dob_day FROM skjera.employee WHERE id=$1",
"SELECT id, email, name, dob FROM skjera.employee WHERE id=$1",
id
)
.fetch_optional(&self.pool)
Expand All @@ -42,7 +42,7 @@ impl EmployeeDao {
pub(crate) async fn employee_by_email(&self, email: String) -> Result<Option<Employee>, Error> {
sqlx::query_as!(
Employee,
"SELECT id, email, name, dob_month, dob_day FROM skjera.employee WHERE email=$1",
"SELECT id, email, name, dob FROM skjera.employee WHERE email=$1",
email
)
.fetch_optional(&self.pool)
Expand All @@ -52,10 +52,9 @@ impl EmployeeDao {
pub(crate) async fn update(&self, employee: &Employee) -> Result<Employee, Error> {
sqlx::query_as!(
Employee,
"UPDATE skjera.employee SET dob_month=$1, dob_day=$2 WHERE id=$3
RETURNING id, email, name, dob_month, dob_day",
employee.dob_month,
employee.dob_day,
"UPDATE skjera.employee SET dob=$1 WHERE id=$2
RETURNING id, email, name, dob",
employee.dob,
employee.id,
)
.fetch_one(&self.pool)
Expand Down
34 changes: 22 additions & 12 deletions backend/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use axum::response::{Html, Redirect};
use axum::Form;
use once_cell::sync::Lazy;
use serde::Deserialize;
use time::{format_description, Date, Month};
use tracing::debug;
use url;
use url::Url;
Expand Down Expand Up @@ -50,17 +51,17 @@ pub async fn get_me(
let template = MeTemplate {
month_names: MONTH_NAMES.as_slice(),
days: (1..31).collect::<Vec<i32>>(),
dob_month: me.dob_month.unwrap_or_default().try_into().unwrap_or_default(),
dob_day: me.dob_day.unwrap_or_default().try_into().unwrap_or_default(),
dob_month: me.dob.map(|d| d.month() as usize).unwrap_or_default(),
dob_day: me.dob.map(|d| d.day() as usize).unwrap_or_default(),
};

Ok(Html(template.render()?))
}

#[derive(Deserialize, Debug)]
pub(crate) struct MeForm {
dob_month: i32,
dob_day: i32,
dob_month: u8,
dob_day: u8,
}

pub async fn post_me(
Expand All @@ -76,14 +77,19 @@ pub async fn post_me(
.await?
.context("error loading me")?;

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

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

debug!("Updated Employee: {:?}", me);
}
me.dob = dob;

me = app.employee_dao.update(&me).await?;

debug!("Updated Employee: {:?}", me);

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

impl EmployeeTemplate {
pub fn dob(&self) -> String {
match (self.employee.dob_month, self.employee.dob_day) {
(Some(m), Some(d)) => format!("{}-{}", m, d),
let f = format_description::parse("[year]-[month]-[day]")
.ok()
.unwrap();

match self.employee.dob {
Some(dob) => dob.format(&f).ok().unwrap_or_default(),
_ => "".to_string(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/skjera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Skjera for ServerImpl {
host: Host,
cookies: CookieJar,
) -> Result<ListEmployeesResponse, String> {
let employees = sqlx::query_as!(Employee, "SELECT id, email, name, dob_month, dob_day FROM skjera.employee")
let employees = sqlx::query_as!(Employee, "SELECT id, email, name, dob FROM skjera.employee")
.fetch_all(&self.pool)
.await
.map_err(|e| {
Expand Down
4 changes: 3 additions & 1 deletion backend/templates/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ <h2>Employees</h2>
<a href="/employee/{{ employee.id }}">{{ employee.name }}</a>
</td>
<td>
{{ employee.dob_day|fmt("{:?}") }} {{ employee.dob_month|fmt("{:?}") }}
{% if let Some(dob) = employee.dob %}
{{ dob.day()|fmt("{:?}") }}{{ dob.month()|fmt("{:?}") }}
{% endif %}
</td>
</tr>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion backend/templates/me.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<h1>Edit profile</h1>
<p>
month: {{ dob_month }}<br>
day: {{ dob_day }}
day: {{ dob_day }}
</p>
<form action="/me" method="POST">
<label for="dob_month">Month of birth</label>
Expand Down
11 changes: 11 additions & 0 deletions migrations/20241231152001_dob.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ALTER TABLE skjera.employee
DROP COLUMN IF EXISTS dob_month,
DROP COLUMN IF EXISTS dob_day,
DROP COLUMN IF EXISTS dob;

ALTER TABLE skjera.employee
ADD COLUMN dob DATE;

UPDATE skjera.employee
SET dob = '1980-12-09'
WHERE email = 'trygvis@scienta.no';

0 comments on commit 12985bf

Please sign in to comment.