use std::fmt::Display;
use serde::{Serialize, Deserialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Product {
pub id: usize,
pub created: usize,
pub owner: usize,
pub name: String,
pub description: String,
pub likes: isize,
pub dislikes: isize,
pub product_type: ProductType,
pub price: ProductPrice,
pub uploads: Vec<usize>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProductType {
Data,
Commission,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Currency {
USD,
EUR,
GBP,
}
impl Display for Currency {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Currency::USD => "$",
Currency::EUR => "€",
Currency::GBP => "£",
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProductPrice(u64, u64, Currency);
impl Display for ProductPrice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("{}{}.{}", self.2, self.0, self.1))
}
}
impl Product {
pub fn new(
owner: usize,
name: String,
description: String,
price: ProductPrice,
r#type: ProductType,
) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
owner,
name,
description,
likes: 0,
dislikes: 0,
product_type: r#type,
price,
uploads: Vec::new(),
}
}
}