refactors, makes Loot private to module

This commit is contained in:
2019-10-14 16:28:18 +02:00
parent b2e319dc15
commit c72169281d
3 changed files with 54 additions and 26 deletions

View File

@@ -19,7 +19,14 @@ pub struct Item {
}
impl Item {
/// Public proxy for Loot::owned_by that selects only Item fields
pub fn value(&self) -> i32 {
self.base_price
}
pub fn sell_value(&self) -> i32 {
self.base_price / 2
}
fn owned_by(player: i32) -> OwnedBy {
Loot::owned_by(player).select(ITEM_COLUMNS)
}
@@ -62,24 +69,30 @@ impl Loot {
looted::table.filter(looted::owner_id.eq(id))
}
pub(super) fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
exists(looted::table.find(id))
}
pub(crate) fn set_owner(&self, owner: i32) -> () {
pub(super) fn set_owner(&self, owner: i32, conn: &DbConnection) -> QueryResult<()> {
diesel::update(looted::table.find(self.id))
.set(looted::dsl::owner_id.eq(owner))
.execute(conn)?;
Ok(())
}
/// TODO: should belong inside Item impl
pub(crate) fn value(&self) -> i32 {
self.base_price
pub(super) fn into_item(self) -> Item {
Item {
id: self.id,
name: self.name,
base_price: self.base_price,
}
}
/// TODO: should belong inside Item impl
pub(crate) fn sell_value(&self) -> i32 {
self.base_price / 2
pub(super) fn find(id: i32) -> Find<looted::table, i32> {
looted::table
.find(id)
}
}
/// Manager for a player's loot
@@ -94,10 +107,10 @@ impl<'q> LootManager<'q> {
/// Finds an item by id
pub fn find(&self, loot_id: i32) -> QueryResult<Item> {
Ok(
looted::table
.find(loot_id)
.first::<Loot>(self.0)
.and_then(|loot| {
Loot::find(loot_id)
.first(self.0)
.and_then(|loot: Loot| {
if loot.owner != self.1 {
Err(diesel::result::Error::NotFound)
} else {
@@ -135,6 +148,8 @@ impl<'q> LootManager<'q> {
diesel::delete(looted::table.find(deleted.id)).execute(self.0)?;
Ok(deleted)
}
}
/// An item being looted or bought.