229 lines
6.0 KiB
Rust
229 lines
6.0 KiB
Rust
//!
|
|
//!
|
|
use super::{models, schema::api_kgon_games};
|
|
use beteran_common_rust as bcr;
|
|
use diesel::prelude::*;
|
|
use diesel::result::Error;
|
|
|
|
///
|
|
pub struct Repository {}
|
|
|
|
impl std::fmt::Debug for Repository {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
f.debug_struct("Repository of api_kgon_games").finish()
|
|
}
|
|
}
|
|
|
|
impl Default for Repository {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Repository {
|
|
///
|
|
pub fn new() -> Repository {
|
|
Repository {}
|
|
}
|
|
|
|
///
|
|
pub fn insert(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
new_member: &models::NewGame,
|
|
) -> Result<models::Game, Error> {
|
|
let inserted = diesel::insert_into(api_kgon_games::table)
|
|
.values(new_member)
|
|
.get_result::<models::Game>(conn)?;
|
|
|
|
Ok(inserted)
|
|
}
|
|
|
|
///
|
|
pub fn upserts(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
upsert_games: Vec<models::UpsertGame>,
|
|
) -> Result<usize, Error> {
|
|
use api_kgon_games::dsl;
|
|
use diesel::pg::upsert::excluded;
|
|
|
|
let affected = diesel::insert_into(api_kgon_games::table)
|
|
.values(upsert_games)
|
|
.on_conflict(dsl::id)
|
|
.do_update()
|
|
.set((
|
|
dsl::parent_id.eq(excluded(dsl::parent_id)),
|
|
dsl::key.eq(excluded(dsl::key)),
|
|
dsl::names.eq(excluded(dsl::names)),
|
|
dsl::platform.eq(excluded(dsl::platform)),
|
|
dsl::category.eq(excluded(dsl::category)),
|
|
dsl::game_type.eq(excluded(dsl::game_type)),
|
|
dsl::image.eq(excluded(dsl::image)),
|
|
))
|
|
.execute(conn)?;
|
|
|
|
Ok(affected)
|
|
}
|
|
|
|
///
|
|
pub fn select(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
id: i64,
|
|
) -> Result<Option<models::Game>, Error> {
|
|
match api_kgon_games::table.find(id).first::<models::Game>(conn) {
|
|
Ok(m) => Ok(Some(m)),
|
|
Err(e) => match e {
|
|
diesel::result::Error::NotFound => Ok(None),
|
|
_ => Err(e),
|
|
},
|
|
}
|
|
}
|
|
|
|
///
|
|
pub fn select_all_count(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
find_all: &models::FindAll,
|
|
) -> Result<i64, Error> {
|
|
let mut q = api_kgon_games::table.into_boxed();
|
|
|
|
if let Some(s) = &find_all.search {
|
|
if let Some(sp) = s.parent_id {
|
|
q = q.filter(api_kgon_games::dsl::parent_id.eq(sp));
|
|
}
|
|
if let Some(sp) = &s.key_like {
|
|
q = q.filter(api_kgon_games::dsl::key.like(sp));
|
|
}
|
|
if let Some(sp) = &s.platform_like {
|
|
q = q.filter(api_kgon_games::dsl::platform.like(sp));
|
|
}
|
|
if let Some(sp) = &s.category_like {
|
|
q = q.filter(api_kgon_games::dsl::category.like(sp));
|
|
}
|
|
if let Some(sp) = &s.game_type_like {
|
|
q = q.filter(api_kgon_games::dsl::game_type.like(sp));
|
|
}
|
|
}
|
|
|
|
q.count().get_result(conn)
|
|
}
|
|
|
|
///
|
|
pub fn select_all(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
find_all: &models::FindAll,
|
|
) -> Result<Vec<models::Game>, Error> {
|
|
let mut q = api_kgon_games::table.into_boxed();
|
|
|
|
if let Some(s) = &find_all.search {
|
|
if let Some(sp) = s.parent_id {
|
|
q = q.filter(api_kgon_games::dsl::parent_id.eq(sp));
|
|
}
|
|
if let Some(sp) = &s.key_like {
|
|
q = q.filter(api_kgon_games::dsl::key.like(sp));
|
|
}
|
|
if let Some(sp) = &s.platform_like {
|
|
q = q.filter(api_kgon_games::dsl::platform.like(sp));
|
|
}
|
|
if let Some(sp) = &s.category_like {
|
|
q = q.filter(api_kgon_games::dsl::category.like(sp));
|
|
}
|
|
if let Some(sp) = &s.game_type_like {
|
|
q = q.filter(api_kgon_games::dsl::game_type.like(sp));
|
|
}
|
|
}
|
|
|
|
if let Some(p) = &find_all.pagination {
|
|
let page = p.page.unwrap_or(1);
|
|
|
|
if let Some(page_size) = p.page_size {
|
|
q = q.offset(((page - 1) * page_size) as i64);
|
|
q = q.limit(page_size as i64);
|
|
}
|
|
}
|
|
if let Some(orderbys) = &find_all.sorts {
|
|
for s in orderbys {
|
|
match s {
|
|
bcr::pagination::Sort::ASC(property) => match property.as_str() {
|
|
"parent_id" => {
|
|
q = q.order_by(api_kgon_games::parent_id.asc());
|
|
}
|
|
"key" => {
|
|
q = q.order_by(api_kgon_games::key.asc());
|
|
}
|
|
"platform" => {
|
|
q = q.order_by(api_kgon_games::platform.asc());
|
|
}
|
|
"category" => {
|
|
q = q.order_by(api_kgon_games::category.asc());
|
|
}
|
|
"game_type" => {
|
|
q = q.order_by(api_kgon_games::game_type.asc());
|
|
}
|
|
"created_at" => {
|
|
q = q.order_by(api_kgon_games::created_at.asc());
|
|
}
|
|
"updated_at" => {
|
|
q = q.order_by(api_kgon_games::updated_at.asc());
|
|
}
|
|
_ => {}
|
|
},
|
|
bcr::pagination::Sort::DESC(property) => match property.as_str() {
|
|
"parent_id" => {
|
|
q = q.order_by(api_kgon_games::parent_id.desc());
|
|
}
|
|
"key" => {
|
|
q = q.order_by(api_kgon_games::key.desc());
|
|
}
|
|
"platform" => {
|
|
q = q.order_by(api_kgon_games::platform.desc());
|
|
}
|
|
"category" => {
|
|
q = q.order_by(api_kgon_games::category.desc());
|
|
}
|
|
"game_type" => {
|
|
q = q.order_by(api_kgon_games::game_type.desc());
|
|
}
|
|
"created_at" => {
|
|
q = q.order_by(api_kgon_games::created_at.desc());
|
|
}
|
|
"updated_at" => {
|
|
q = q.order_by(api_kgon_games::updated_at.desc());
|
|
}
|
|
_ => {}
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
q.load::<models::Game>(conn)
|
|
}
|
|
|
|
///
|
|
pub fn update(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
id: i64,
|
|
modify: &models::ModifyGame,
|
|
) -> Result<u64, Error> {
|
|
use api_kgon_games::dsl;
|
|
|
|
diesel::update(dsl::api_kgon_games.filter(dsl::id.eq(id)))
|
|
.set(modify)
|
|
.execute(conn)
|
|
.map(|c| c as u64)
|
|
}
|
|
|
|
///
|
|
pub fn delete(&self, conn: &diesel::PgConnection, id: i64) -> Result<u64, Error> {
|
|
use api_kgon_games::dsl;
|
|
|
|
diesel::delete(api_kgon_games::table.filter(dsl::id.eq(id)))
|
|
.execute(conn)
|
|
.map(|c| c as u64)
|
|
}
|
|
}
|