adds UpdateResult class of queries

This commit is contained in:
2019-10-29 16:19:00 +01:00
parent 089aaf9a6d
commit ee0b7b2b7a
4 changed files with 64 additions and 59 deletions

View File

@@ -3,7 +3,7 @@ use diesel::expression::exists::Exists;
use diesel::prelude::*;
use crate::schema::{items, looted};
use crate::{DbConnection, QueryResult};
use crate::{DbConnection, QueryResult, Update, UpdateResult };
type ItemColumns = (looted::id, looted::name, looted::base_price);
const ITEM_COLUMNS: ItemColumns = (looted::id, looted::name, looted::base_price);
type OwnedBy = Select<OwnedLoot, ItemColumns>;
@@ -27,6 +27,11 @@ impl Item {
self.base_price / 2
}
pub fn remove(self, conn: &DbConnection) -> UpdateResult {
diesel::delete(looted::table.find(self.id)).execute(conn)?;
Ok(Update::ItemRemoved(self))
}
fn owned_by(player: i32) -> OwnedBy {
Loot::owned_by(player).select(ITEM_COLUMNS)
}
@@ -124,7 +129,7 @@ impl<'q> LootManager<'q> {
.first(self.0)?)
}
pub(crate) fn add<S: Into<String>>(self, name: S, base_price: i32) -> QueryResult<Item> {
pub(crate) fn add<S: Into<String>>(self, name: S, base_price: i32) -> UpdateResult {
self.add_from(&Item {
id: 0,
name: name.into(),
@@ -133,7 +138,7 @@ impl<'q> LootManager<'q> {
}
/// Adds a copy of the given item inside player chest
pub fn add_from(self, item: &Item) -> QueryResult<Item> {
pub fn add_from(self, item: &Item) -> UpdateResult {
let new_item = NewLoot {
name: &item.name,
base_price: item.base_price,
@@ -142,13 +147,7 @@ impl<'q> LootManager<'q> {
diesel::insert_into(looted::table)
.values(&new_item)
.execute(self.0)?;
self.last()
}
pub fn remove(self, item_id: i32) -> QueryResult<Item> {
let deleted = self.find(item_id)?;
diesel::delete(looted::table.find(deleted.id)).execute(self.0)?;
Ok(deleted)
Ok(Update::ItemAdded(self.last()?))
}
}