refactor: fewer non-macro queries
Some checks failed
/ integration-test--firefox (push) Failing after 3m5s
Some checks failed
/ integration-test--firefox (push) Failing after 3m5s
This commit is contained in:
parent
69e23fd9bb
commit
84c41dda4d
5 changed files with 119 additions and 111 deletions
|
|
@ -76,17 +76,7 @@ mod get {
|
|||
let user = auth_session.user.unwrap();
|
||||
let pool = &state.db(&user).pool;
|
||||
|
||||
let contact: HydratedContact = sqlx::query_as(
|
||||
"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)
|
||||
.await?;
|
||||
let contact = HydratedContact::load(contact_id, pool).await?;
|
||||
|
||||
let entries: Vec<JournalEntry> = sqlx::query_as(
|
||||
"select distinct j.id, j.value, j.date from journal_entries j
|
||||
|
|
@ -231,29 +221,7 @@ mod get {
|
|||
layout: Layout,
|
||||
) -> Result<Markup, AppError> {
|
||||
let pool = &state.db(&auth_session.user.unwrap()).pool;
|
||||
let contact: HydratedContact = sqlx::query_as(
|
||||
"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 mentions m on m.entity_id = jes.id
|
||||
where
|
||||
m.entity_type = $1 and (
|
||||
m.url = '/contact/'||c.id
|
||||
or m.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 = $2",
|
||||
)
|
||||
.bind(MentionHostType::JournalEntry as DbId)
|
||||
.bind(contact_id)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
let contact = HydratedContact::load(contact_id, pool).await?;
|
||||
|
||||
let addresses: Vec<Address> = sqlx::query_as!(
|
||||
Address,
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ use super::Layout;
|
|||
use crate::db::DbId;
|
||||
use crate::models::user::AuthSession;
|
||||
use crate::models::{Birthday, HydratedContact, JournalEntry};
|
||||
use crate::switchboard::{MentionHost, MentionHostType};
|
||||
use crate::{AppError, AppState};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -129,22 +128,7 @@ pub mod get {
|
|||
let user = auth_session.user.unwrap();
|
||||
let pool = &state.db(&user).pool;
|
||||
|
||||
let contacts: Vec<HydratedContact> = sqlx::query_as(
|
||||
"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 mentions ms on ms.entity_id = jes.id
|
||||
where ms.entity_type = $1
|
||||
and ms.url = '/contact/'||c.id
|
||||
order by jes.date desc limit 1
|
||||
) as last_mention_date from contacts c",
|
||||
)
|
||||
.bind(MentionHostType::JournalEntry as DbId)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
let contacts = HydratedContact::all(&pool).await?;
|
||||
let mut freshens: Vec<ContactFreshness> = contacts
|
||||
.clone()
|
||||
.into_iter()
|
||||
|
|
|
|||
|
|
@ -55,15 +55,9 @@ mod get {
|
|||
let mut calendar = Calendar::new();
|
||||
calendar.name(&calname);
|
||||
calendar.append_property(("PRODID", "Mascarpone CRM"));
|
||||
let contacts: Vec<HydratedContact> = sqlx::query_as(
|
||||
"select id, birthday, (
|
||||
select string_agg(name,'\x1c' order by sort)
|
||||
from names where contact_id = c.id
|
||||
) as names
|
||||
from contacts c",
|
||||
)
|
||||
.fetch_all(&pool)
|
||||
.await?;
|
||||
|
||||
// TODO; this does some db work to pull in last_modified_date that we don't use
|
||||
let contacts = HydratedContact::all(&pool).await?;
|
||||
for contact in &contacts {
|
||||
if let Some(Birthday::Date(yo_date)) = &contact.birthday {
|
||||
if let Some(date) = NaiveDate::from_ymd_opt(
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ use axum::extract::FromRequestParts;
|
|||
use cache_bust::asset;
|
||||
use http::request::Parts;
|
||||
use maud::{DOCTYPE, Markup, html};
|
||||
use sqlx::FromRow;
|
||||
|
||||
use super::models::user::{AuthSession, User};
|
||||
use super::{AppError, AppState};
|
||||
use crate::db::DbId;
|
||||
|
||||
pub mod auth;
|
||||
pub mod contact;
|
||||
|
|
@ -16,10 +16,10 @@ pub mod ics;
|
|||
pub mod journal;
|
||||
pub mod settings;
|
||||
|
||||
#[derive(Debug, FromRow)]
|
||||
#[derive(Debug)]
|
||||
struct ContactLink {
|
||||
name: String,
|
||||
contact_id: u32,
|
||||
contact_id: DbId,
|
||||
}
|
||||
pub struct Layout {
|
||||
contact_links: Vec<ContactLink>,
|
||||
|
|
@ -39,7 +39,8 @@ impl FromRequestParts<AppState> for Layout {
|
|||
.map_err(|_| anyhow::Error::msg("could not get session"))?;
|
||||
let user = auth_session.user.unwrap();
|
||||
|
||||
let contact_links: Vec<ContactLink> = sqlx::query_as(
|
||||
let contact_links = sqlx::query_as!(
|
||||
ContactLink,
|
||||
"select c.id as contact_id,
|
||||
coalesce(n.name, '(unnamed)') as name
|
||||
from contacts c
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue