thoughts on new api structure, formats code
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use diesel::prelude::*;
|
||||
use crate::{DbConnection, QueryResult};
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::models::{self, item::Loot};
|
||||
use crate::schema::claims;
|
||||
@@ -27,8 +27,7 @@ impl Claim {
|
||||
}
|
||||
|
||||
fn remove(&self, conn: &DbConnection) -> QueryResult<()> {
|
||||
diesel::delete(claims::table.find(self.id))
|
||||
.execute(conn)?;
|
||||
diesel::delete(claims::table.find(self.id)).execute(conn)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -36,10 +35,8 @@ impl Claim {
|
||||
pub struct Claims<'q>(pub &'q DbConnection);
|
||||
|
||||
impl<'q> Claims<'q> {
|
||||
|
||||
pub fn all(&self) -> QueryResult<Vec<Claim>> {
|
||||
claims::table
|
||||
.load(self.0)
|
||||
claims::table.load(self.0)
|
||||
}
|
||||
|
||||
/// Finds a single claim by association of player and loot ids.
|
||||
@@ -85,15 +82,12 @@ impl<'q> Claims<'q> {
|
||||
|
||||
pub(crate) fn grouped_by_item(&self) -> QueryResult<Vec<(models::item::Item, Vec<Claim>)>> {
|
||||
let group_loot: Vec<Loot> = Loot::owned_by(0).load(self.0)?;
|
||||
let claims = claims::table
|
||||
.load(self.0)?
|
||||
.grouped_by(&group_loot);
|
||||
Ok(
|
||||
group_loot.into_iter()
|
||||
.map(|loot| loot.into_item())
|
||||
.zip(claims)
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
let claims = claims::table.load(self.0)?.grouped_by(&group_loot);
|
||||
Ok(group_loot
|
||||
.into_iter()
|
||||
.map(|loot| loot.into_item())
|
||||
.zip(claims)
|
||||
.collect::<Vec<_>>())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,17 +111,23 @@ mod tests {
|
||||
type TestResult = Result<(), diesel::result::Error>;
|
||||
|
||||
fn test_connection() -> Result<DbConnection, diesel::result::Error> {
|
||||
let conn = DbConnection::establish(":memory:")
|
||||
.map_err(|_| diesel::result::Error::NotFound)?;
|
||||
let conn =
|
||||
DbConnection::establish(":memory:").map_err(|_| diesel::result::Error::NotFound)?;
|
||||
diesel_migrations::run_pending_migrations(&conn)
|
||||
.map_err(|_| diesel::result::Error::NotFound)?;
|
||||
let manager = models::player::Players(&conn);
|
||||
manager.add("Player1", 0.0)?;
|
||||
manager.add("Player2", 0.0)?;
|
||||
crate::LootManager(&conn, 0)
|
||||
.add_from(&crate::Item{ id: 0, name: "Epee".to_string(), base_price: 30 })?;
|
||||
crate::LootManager(&conn, 1)
|
||||
.add_from(&crate::Item{ id: 0, name: "Arc".to_string(), base_price: 20 })?;
|
||||
crate::LootManager(&conn, 0).add_from(&crate::Item {
|
||||
id: 0,
|
||||
name: "Epee".to_string(),
|
||||
base_price: 30,
|
||||
})?;
|
||||
crate::LootManager(&conn, 1).add_from(&crate::Item {
|
||||
id: 0,
|
||||
name: "Arc".to_string(),
|
||||
base_price: 20,
|
||||
})?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user