thoughts on new api structure, formats code
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
|
||||
|
||||
use diesel::dsl::{exists, Eq, Filter, Find, Select};
|
||||
use diesel::expression::exists::Exists;
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::{DbConnection, QueryResult};
|
||||
use crate::schema::{items, looted};
|
||||
use crate::{DbConnection, QueryResult};
|
||||
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>;
|
||||
@@ -35,23 +33,18 @@ impl Item {
|
||||
pub struct Inventory<'q>(pub &'q DbConnection);
|
||||
|
||||
impl<'q> Inventory<'q> {
|
||||
|
||||
pub fn all(&self) -> QueryResult<Vec<Item>> {
|
||||
items::table.load::<Item>(self.0)
|
||||
}
|
||||
|
||||
pub fn find(&self, item_id: i32) -> QueryResult<Item> {
|
||||
items::table
|
||||
.find(item_id)
|
||||
.first::<Item>(self.0)
|
||||
items::table.find(item_id).first::<Item>(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
type WithOwner = Eq<looted::owner_id, i32>;
|
||||
type OwnedLoot = Filter<looted::table, WithOwner>;
|
||||
|
||||
|
||||
|
||||
/// Represents an item that has been looted,
|
||||
/// hence has an owner.
|
||||
#[derive(Identifiable, Debug, Queryable, Serialize)]
|
||||
@@ -89,10 +82,8 @@ impl Loot {
|
||||
}
|
||||
|
||||
pub(super) fn find(id: i32) -> Find<looted::table, i32> {
|
||||
looted::table
|
||||
.find(id)
|
||||
looted::table.find(id)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Manager for a player's loot
|
||||
@@ -106,28 +97,24 @@ impl<'q> LootManager<'q> {
|
||||
|
||||
/// Finds an item by id
|
||||
pub fn find(&self, loot_id: i32) -> QueryResult<Item> {
|
||||
Ok(
|
||||
|
||||
Loot::find(loot_id)
|
||||
.first(self.0)
|
||||
.and_then(|loot: Loot| {
|
||||
if loot.owner != self.1 {
|
||||
Err(diesel::result::Error::NotFound)
|
||||
} else {
|
||||
Ok( Item { id: loot.id, name: loot.name, base_price: loot.base_price } )
|
||||
}
|
||||
})?
|
||||
)
|
||||
|
||||
Ok(Loot::find(loot_id).first(self.0).and_then(|loot: Loot| {
|
||||
if loot.owner != self.1 {
|
||||
Err(diesel::result::Error::NotFound)
|
||||
} else {
|
||||
Ok(Item {
|
||||
id: loot.id,
|
||||
name: loot.name,
|
||||
base_price: loot.base_price,
|
||||
})
|
||||
}
|
||||
})?)
|
||||
}
|
||||
|
||||
/// The last item added to the chest
|
||||
pub fn last(&self) -> QueryResult<Item> {
|
||||
Ok(
|
||||
Item::owned_by(self.1)
|
||||
.order(looted::dsl::id.desc())
|
||||
.first(self.0)?
|
||||
)
|
||||
Ok(Item::owned_by(self.1)
|
||||
.order(looted::dsl::id.desc())
|
||||
.first(self.0)?)
|
||||
}
|
||||
|
||||
/// Adds a copy of the given item inside player chest
|
||||
@@ -137,10 +124,10 @@ impl<'q> LootManager<'q> {
|
||||
base_price: item.base_price,
|
||||
owner_id: self.1,
|
||||
};
|
||||
diesel::insert_into(looted::table)
|
||||
.values(&new_item)
|
||||
.execute(self.0)?;
|
||||
self.last()
|
||||
diesel::insert_into(looted::table)
|
||||
.values(&new_item)
|
||||
.execute(self.0)?;
|
||||
self.last()
|
||||
}
|
||||
|
||||
pub fn remove(self, item_id: i32) -> QueryResult<Item> {
|
||||
@@ -148,8 +135,6 @@ impl<'q> LootManager<'q> {
|
||||
diesel::delete(looted::table.find(deleted.id)).execute(self.0)?;
|
||||
Ok(deleted)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// An item being looted or bought.
|
||||
|
||||
Reference in New Issue
Block a user