lemmy/lemmy_db_views/src/site_view.rs

39 lines
851 B
Rust
Raw Normal View History

2020-12-02 19:32:47 +00:00
use diesel::{result::Error, *};
use lemmy_db::ToSafe;
use lemmy_db_aggregates::site_aggregates::SiteAggregates;
use lemmy_db_schema::{
schema::{site, site_aggregates, user_},
source::{
site::Site,
user::{UserSafe, User_},
},
2020-12-02 19:32:47 +00:00
};
use serde::Serialize;
#[derive(Debug, Serialize, Clone)]
pub struct SiteView {
pub site: Site,
pub creator: UserSafe,
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
2020-12-02 19:32:47 +00:00
.inner_join(user_::table)
.inner_join(site_aggregates::table)
.select((
site::all_columns,
User_::safe_columns_tuple(),
site_aggregates::all_columns,
))
.first::<(Site, UserSafe, SiteAggregates)>(conn)?;
2020-12-02 19:32:47 +00:00
Ok(SiteView {
site,
creator,
counts,
})
2020-12-02 19:32:47 +00:00
}
}