From fd5f1899c13332f28f48117f7342c839c62a155a Mon Sep 17 00:00:00 2001 From: Robert Perce Date: Sat, 24 Jan 2026 11:13:27 -0600 Subject: [PATCH] feat: lives-with field --- migrations/demo.sql | 14 +++++ .../each_user/0009_contact-lives-with.sql | 1 + src/models/contact.rs | 4 ++ src/web/contact.rs | 51 ++++++++++++------- 4 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 migrations/each_user/0009_contact-lives-with.sql diff --git a/migrations/demo.sql b/migrations/demo.sql index a81f905..37e676e 100644 --- a/migrations/demo.sql +++ b/migrations/demo.sql @@ -30,6 +30,20 @@ insert into names(contact_id, sort, name) values (3, 0, 'Eleanor Edgeworth'), (3, 1, 'Eleanor'); +insert into contacts(id, lives_with) values (4, 'Henrietta'); +insert into names(contact_id, sort, name) values + (4, 0, 'Felicia Homeowner'); + +insert into contacts(id, lives_with) values (5, 'Henrietta'); +insert into names(contact_id, sort, name) values + (5, 0, 'Gregory Homeowner'); + +insert into contacts(id) values (6); +insert into names(contact_id, sort, name) values + (6, 0, 'Henrietta Homeowner'); + +insert into addresses(contact_id, label, value) values + (6, null, '123 Main St., Realville, WI 99999'); insert into journal_entries(id, date, value) values (0, '2020-02-27', 'Lunch with [[Bazel Bagend]] and his wife'), diff --git a/migrations/each_user/0009_contact-lives-with.sql b/migrations/each_user/0009_contact-lives-with.sql new file mode 100644 index 0000000..20f1edc --- /dev/null +++ b/migrations/each_user/0009_contact-lives-with.sql @@ -0,0 +1 @@ +alter table contacts add column lives_with text not null default ''; diff --git a/src/models/contact.rs b/src/models/contact.rs index ca2bf98..ebcb7f6 100644 --- a/src/models/contact.rs +++ b/src/models/contact.rs @@ -11,6 +11,7 @@ pub struct Contact { pub id: DbId, pub birthday: Option, pub manually_freshened_at: Option>, + pub lives_with: String, } #[derive(Clone, Debug)] @@ -55,10 +56,13 @@ impl FromRow<'_, SqliteRow> for Contact { .map(|d| d.to_utc()) }); + let lives_with: String = row.try_get("lives_with")?; + Ok(Self { id, birthday, manually_freshened_at, + lives_with, }) } } diff --git a/src/web/contact.rs b/src/web/contact.rs index 1808025..25ca427 100644 --- a/src/web/contact.rs +++ b/src/web/contact.rs @@ -74,12 +74,12 @@ mod get { ) -> Result { let pool = &state.db(&auth_session.user.unwrap()).pool; let contact: HydratedContact = sqlx::query_as( - "select id, birthday, manually_freshened_at, ( - select string_agg(name,'\x1c' order by sort) - from names where contact_id = c.id - ) as names - from contacts c - where c.id = $1", + "select *, ( + select string_agg(name,'\x1c' order by sort) + from names where contact_id = c.id + ) as names + from contacts c + where c.id = $1", ) .bind(contact_id) .fetch_one(pool) @@ -156,6 +156,12 @@ mod get { "(never)" } } + + @if contact.lives_with.len() > 0 { + label { "lives with" } + div { (contact.lives_with) } + } + @if addresses.len() == 1 { label { "address" } #addresses { @@ -207,16 +213,21 @@ mod get { ) -> Result { let pool = &state.db(&auth_session.user.unwrap()).pool; let contact: HydratedContact = sqlx::query_as( - "select id, birthday, manually_freshened_at, ( - select string_agg(name,'\x1c' order by sort) - from names where contact_id = c.id - ) as names, ( - select jes.date from journal_entries jes - join journal_mentions cms on cms.entry_id = jes.id - where cms.url = '/contact/'||c.id - order by jes.date desc limit 1 - ) as last_mention_date from contacts c - where c.id = $1", + "select *, ( + select string_agg(name,'\x1c' order by sort) + from names where contact_id = c.id + ) as names, ( + select jes.date from journal_entries jes + join journal_mentions cms on cms.entry_id = jes.id + where cms.url = '/contact/'||c.id + or cms.url in ( + select '/group/'||name + from groups + where contact_id = c.id + ) + order by jes.date desc limit 1 + ) as last_mention_date from contacts c + where c.id = $1", ) .bind(contact_id) .fetch_one(pool) @@ -289,6 +300,10 @@ mod get { span x-text="date.length ? date.split('T')[0] : '(never)'" {} input type="button" value="Mark fresh now" x-on:click="date = new Date().toISOString()"; } + label { "lives with" } + div { + input name="lives_with" value=(contact.lives_with); + } label { "addresses" } div x-data=(json!({ "addresses": addresses, "new_label": "", "new_address": "" })) { template x-for="(address, index) in addresses" x-bind:key="index" { @@ -361,6 +376,7 @@ mod put { name: Option>, birthday: String, manually_freshened_at: String, + lives_with: String, address_label: Option>, address_value: Option>, group: Option>, @@ -400,9 +416,10 @@ mod put { }; sqlx::query!( - "update contacts set (birthday, manually_freshened_at, text_body) = ($1, $2, $3) where id = $4", + "update contacts set (birthday, manually_freshened_at, lives_with, text_body) = ($1, $2, $3, $4) where id = $5", birthday, manually_freshened_at, + payload.lives_with, text_body, contact_id )