will need to clean up db API

This commit is contained in:
2020-01-05 15:37:40 +01:00
parent a47646bd5f
commit 6149dfd297
2 changed files with 82 additions and 45 deletions

View File

@@ -44,6 +44,57 @@ impl Item {
}
}
const OF_GROUP = 0;
const OF_SHOP = -1;
const SOLD = -2;
type WithOwner = Eq<looted::owner_id, i32>;
type OwnedLoot = Filter<looted::table, WithOwner>;
/// An owned item
///
/// The owner is a Player, the Group, the Merchant
/// OR the SOLD state.
#[derive(Identifiable, Debug, Queryable)]
#[table_name = "looted"]
pub(super) struct Loot {
id: i32,
name: String,
base_price: i32,
owner: i32,
}
impl Loot {
/// A filter on Loot that is owned by given player
pub(super) fn owned_by(id: i32) -> OwnedLoot {
looted::table.filter(looted::owner_id.eq(id))
}
fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
exists(looted::table.find(id))
}
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(())
}
pub(super) fn into_item(self) -> Item {
Item {
id: self.id,
name: self.name,
base_price: self.base_price,
}
}
pub(super) fn find(id: i32) -> Find<looted::table, i32> {
looted::table.find(id)
}
}
pub struct Inventory<'q>(pub &'q DbConnection);
impl<'q> Inventory<'q> {
@@ -58,13 +109,18 @@ impl<'q> Inventory<'q> {
}
}
/// The shop resource
pub struct Shop<'q>(pub &'q DbConnection);
impl<'q> Shop<'q> {
// Rename to list
pub fn all(&self) -> QueryResult<Vec<Item>> {
// Loot::owned_by(OF_SHOP).load(self.0)
shop::table.load(self.0)
}
// pub fn buy(&self, items: Vec<(i32, f64)>, buyer: i32) -> UpdateResult {}
pub fn get(&self, id: i32) -> QueryResult<Item> {
shop::table.find(&id).first::<Item>(self.0)
}
@@ -99,50 +155,6 @@ impl<'q> Shop<'q> {
}
}
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)]
#[table_name = "looted"]
pub(super) struct Loot {
id: i32,
name: String,
base_price: i32,
owner: i32,
}
impl Loot {
/// A filter on Loot that is owned by given player
pub(super) fn owned_by(id: i32) -> OwnedLoot {
looted::table.filter(looted::owner_id.eq(id))
}
fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
exists(looted::table.find(id))
}
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(())
}
pub(super) fn into_item(self) -> Item {
Item {
id: self.id,
name: self.name,
base_price: self.base_price,
}
}
pub(super) fn find(id: i32) -> Find<looted::table, i32> {
looted::table.find(id)
}
}
/// Manager for a *single* player loot
pub struct LootManager<'q>(pub &'q DbConnection, pub i32);