43 lines
1.1 KiB
Rust
43 lines
1.1 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use crate::models::item::Item;
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
pub struct ItemListing {
|
|
pub item: Item,
|
|
pub price: u64,
|
|
pub count_per_price: u64,
|
|
pub restocked_time: DateTime<Utc>
|
|
}
|
|
|
|
impl ItemListing {
|
|
fn new(item: &str, price: u64, count_per_price: u64, restocked_time: DateTime<Utc>) -> Self {
|
|
Self {
|
|
item: Item {name: item.to_string()},
|
|
price,
|
|
count_per_price,
|
|
restocked_time,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default, Serialize, Deserialize, Debug, Clone)]
|
|
pub struct ShopData {
|
|
pub item_listings: Vec<ItemListing>
|
|
}
|
|
|
|
impl ShopData {
|
|
fn add_item(&mut self, item_listing: ItemListing) {
|
|
self.item_listings.push(item_listing);
|
|
}
|
|
|
|
fn filter_items<F>(&self, filter: F) -> Vec<ItemListing> where F: FnMut(&&ItemListing) -> bool {
|
|
self.item_listings.iter().filter(filter).cloned().collect()
|
|
}
|
|
|
|
fn remove_item(&mut self, item_name: &str) {
|
|
self.item_listings.retain(|listing| &listing.item.name != item_name)
|
|
}
|
|
}
|