add: product types
This commit is contained in:
parent
2705608903
commit
ea13526515
9 changed files with 163 additions and 8 deletions
|
@ -600,7 +600,7 @@ input[type="checkbox"]:checked {
|
|||
padding: 0;
|
||||
}
|
||||
|
||||
.notification .icon {
|
||||
.notification:not(.chip) .icon {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
|
|
@ -368,6 +368,7 @@
|
|||
ADMINISTRATOR: 1 << 1,
|
||||
MANAGE_DOMAINS: 1 << 2,
|
||||
MANAGE_SERVICES: 1 << 3,
|
||||
MANAGE_PRODUCTS: 1 << 4,
|
||||
},
|
||||
\"secondary_role\",
|
||||
\"add_permission_to_secondary_role\",
|
||||
|
|
|
@ -42,6 +42,7 @@ impl DataManager {
|
|||
execute!(&conn, common::CREATE_TABLE_INVITE_CODES).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_DOMAINS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_SERVICES).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_PRODUCTS).unwrap();
|
||||
|
||||
self.0
|
||||
.1
|
||||
|
|
|
@ -29,3 +29,4 @@ pub const CREATE_TABLE_MESSAGE_REACTIONS: &str = include_str!("./sql/create_mess
|
|||
pub const CREATE_TABLE_INVITE_CODES: &str = include_str!("./sql/create_invite_codes.sql");
|
||||
pub const CREATE_TABLE_DOMAINS: &str = include_str!("./sql/create_domains.sql");
|
||||
pub const CREATE_TABLE_SERVICES: &str = include_str!("./sql/create_services.sql");
|
||||
pub const CREATE_TABLE_PRODUCTS: &str = include_str!("./sql/create_products.sql");
|
||||
|
|
12
crates/core/src/database/drivers/sql/create_products.sql
Normal file
12
crates/core/src/database/drivers/sql/create_products.sql
Normal file
|
@ -0,0 +1,12 @@
|
|||
CREATE TABLE IF NOT EXISTS products (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
created BIGINT NOT NULL,
|
||||
owner BIGINT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
likes INT NOT NULL,
|
||||
dislikes INT NOT NULL,
|
||||
product_type TEXT NOT NULL,
|
||||
stripe_id TEXT NOT NULL,
|
||||
price TEXT NOT NULL
|
||||
)
|
|
@ -21,6 +21,7 @@ mod notifications;
|
|||
mod polls;
|
||||
mod pollvotes;
|
||||
mod posts;
|
||||
mod products;
|
||||
mod questions;
|
||||
mod reactions;
|
||||
mod reports;
|
||||
|
|
138
crates/core/src/database/products.rs
Normal file
138
crates/core/src/database/products.rs
Normal file
|
@ -0,0 +1,138 @@
|
|||
use crate::model::{
|
||||
auth::User,
|
||||
products::Product,
|
||||
permissions::{FinePermission, SecondaryPermission},
|
||||
Error, Result,
|
||||
};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{cache::Cache, execute, get, params, query_rows, PostgresRow};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`Product`] from an SQL row.
|
||||
pub(crate) fn get_product_from_row(x: &PostgresRow) -> Product {
|
||||
Product {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
name: get!(x->3(String)),
|
||||
description: get!(x->4(String)),
|
||||
likes: get!(x->5(i32)) as isize,
|
||||
dislikes: get!(x->6(i32)) as isize,
|
||||
product_type: serde_json::from_str(&get!(x->7(String))).unwrap(),
|
||||
stripe_id: get!(x->8(String)),
|
||||
price: serde_json::from_str(&get!(x->9(String))).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_product_by_id(usize as i64)@get_product_from_row -> "SELECT * FROM products WHERE id = $1" --name="product" --returns=Product --cache-key-tmpl="atto.product:{}");
|
||||
|
||||
/// Get all products by user.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch products for
|
||||
pub async fn get_products_by_user(&self, id: usize) -> Result<Vec<Product>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM products WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_product_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("product".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_PRODUCTS: usize = 15;
|
||||
|
||||
/// Create a new product in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`Product`] object to insert
|
||||
pub async fn create_product(&self, data: Product) -> Result<Product> {
|
||||
// check values
|
||||
if data.name.len() < 2 {
|
||||
return Err(Error::DataTooShort("name".to_string()));
|
||||
} else if data.name.len() > 128 {
|
||||
return Err(Error::DataTooLong("name".to_string()));
|
||||
}
|
||||
|
||||
// check number of products
|
||||
let owner = self.get_user_by_id(data.owner).await?;
|
||||
|
||||
if !owner.permissions.check(FinePermission::SUPPORTER) {
|
||||
let products = self.get_products_by_user(data.owner).await?;
|
||||
|
||||
if products.len() >= Self::MAXIMUM_FREE_PRODUCTS {
|
||||
return Err(Error::MiscError(
|
||||
"You already have the maximum number of products you can have".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO products VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&(data.owner as i64),
|
||||
&data.name,
|
||||
&data.description,
|
||||
&0_i32,
|
||||
&0_i32,
|
||||
&serde_json::to_string(&data.product_type).unwrap(),
|
||||
&data.stripe_id,
|
||||
&serde_json::to_string(&data.price).unwrap(),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_product(&self, id: usize, user: &User) -> Result<()> {
|
||||
let product = self.get_product_by_id(id).await?;
|
||||
|
||||
// check user permission
|
||||
if user.id != product.owner
|
||||
&& !user
|
||||
.secondary_permissions
|
||||
.check(SecondaryPermission::MANAGE_PRODUCTS)
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, "DELETE FROM products WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
self.0.1.remove(format!("atto.product:{}", id)).await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -176,6 +176,7 @@ bitflags! {
|
|||
const ADMINISTRATOR = 1 << 1;
|
||||
const MANAGE_DOMAINS = 1 << 2;
|
||||
const MANAGE_SERVICES = 1 << 3;
|
||||
const MANAGE_PRODUCTS = 1 << 4;
|
||||
|
||||
const _ = !0;
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@ pub struct Product {
|
|||
pub id: usize,
|
||||
pub created: usize,
|
||||
pub owner: usize,
|
||||
pub title: String,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub likes: usize,
|
||||
pub dislikes: usize,
|
||||
pub r#type: ProductType,
|
||||
pub likes: isize,
|
||||
pub dislikes: isize,
|
||||
pub product_type: ProductType,
|
||||
pub stripe_id: String,
|
||||
pub price: ProductPrice,
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ impl Product {
|
|||
/// Create a new [`Product`].
|
||||
pub fn new(
|
||||
owner: usize,
|
||||
title: String,
|
||||
name: String,
|
||||
description: String,
|
||||
price: ProductPrice,
|
||||
r#type: ProductType,
|
||||
|
@ -46,11 +46,11 @@ impl Product {
|
|||
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
created: unix_epoch_timestamp(),
|
||||
owner,
|
||||
title,
|
||||
name,
|
||||
description,
|
||||
likes: 0,
|
||||
dislikes: 0,
|
||||
r#type,
|
||||
product_type: r#type,
|
||||
stripe_id: String::new(),
|
||||
price,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue