lemmy/crates/db_views/src/site_view.rs

38 lines
888 B
Rust
Raw Normal View History

2020-12-02 19:32:47 +00:00
use diesel::{result::Error, *};
use lemmy_db_queries::{aggregates::site_aggregates::SiteAggregates, ToSafe};
use lemmy_db_schema::{
2021-03-11 04:43:11 +00:00
schema::{person, site, site_aggregates},
source::{
2021-03-11 04:43:11 +00:00
person::{Person, PersonSafe},
site::Site,
},
2020-12-02 19:32:47 +00:00
};
use serde::{Deserialize, Serialize};
2020-12-02 19:32:47 +00:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2020-12-02 19:32:47 +00:00
pub struct SiteView {
pub site: Site,
2021-03-10 22:33:55 +00:00
pub creator: PersonSafe,
pub counts: SiteAggregates,
2020-12-02 19:32:47 +00:00
}
impl SiteView {
pub fn read(conn: &PgConnection) -> Result<Self, Error> {
let (site, creator, counts) = site::table
2021-03-10 22:33:55 +00:00
.inner_join(person::table)
.inner_join(site_aggregates::table)
.select((
site::all_columns,
2021-03-10 22:33:55 +00:00
Person::safe_columns_tuple(),
site_aggregates::all_columns,
))
2021-03-10 22:33:55 +00:00
.first::<(Site, PersonSafe, SiteAggregates)>(conn)?;
2020-12-02 19:32:47 +00:00
Ok(SiteView {
site,
creator,
counts,
})
2020-12-02 19:32:47 +00:00
}
}