add: products api
This commit is contained in:
parent
2be2409d66
commit
cf2af1e1e9
6 changed files with 241 additions and 9 deletions
|
@ -6,6 +6,7 @@ pub mod domains;
|
||||||
pub mod journals;
|
pub mod journals;
|
||||||
pub mod notes;
|
pub mod notes;
|
||||||
pub mod notifications;
|
pub mod notifications;
|
||||||
|
pub mod products;
|
||||||
pub mod reactions;
|
pub mod reactions;
|
||||||
pub mod reports;
|
pub mod reports;
|
||||||
pub mod requests;
|
pub mod requests;
|
||||||
|
@ -31,6 +32,7 @@ use tetratto_core::model::{
|
||||||
littleweb::{DomainData, DomainTld, ServiceFsEntry},
|
littleweb::{DomainData, DomainTld, ServiceFsEntry},
|
||||||
oauth::AppScope,
|
oauth::AppScope,
|
||||||
permissions::{FinePermission, SecondaryPermission},
|
permissions::{FinePermission, SecondaryPermission},
|
||||||
|
products::{ProductType, ProductPrice},
|
||||||
reactions::AssetType,
|
reactions::AssetType,
|
||||||
stacks::{StackMode, StackPrivacy, StackSort},
|
stacks::{StackMode, StackPrivacy, StackSort},
|
||||||
};
|
};
|
||||||
|
@ -652,6 +654,17 @@ pub fn routes() -> Router {
|
||||||
.route("/domains/{id}", get(domains::get_request))
|
.route("/domains/{id}", get(domains::get_request))
|
||||||
.route("/domains/{id}", delete(domains::delete_request))
|
.route("/domains/{id}", delete(domains::delete_request))
|
||||||
.route("/domains/{id}/data", post(domains::update_data_request))
|
.route("/domains/{id}/data", post(domains::update_data_request))
|
||||||
|
// products
|
||||||
|
.route("/products", get(products::list_request))
|
||||||
|
.route("/products", post(products::create_request))
|
||||||
|
.route("/products/{id}", get(products::get_request))
|
||||||
|
.route("/products/{id}", delete(products::delete_request))
|
||||||
|
.route("/products/{id}/name", post(products::update_name_request))
|
||||||
|
.route(
|
||||||
|
"/products/{id}/description",
|
||||||
|
post(products::update_description_request),
|
||||||
|
)
|
||||||
|
.route("/products/{id}/price", post(products::update_price_request))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lw_routes() -> Router {
|
pub fn lw_routes() -> Router {
|
||||||
|
@ -1086,3 +1099,26 @@ pub struct CreateDomain {
|
||||||
pub struct UpdateDomainData {
|
pub struct UpdateDomainData {
|
||||||
pub data: Vec<(String, DomainData)>,
|
pub data: Vec<(String, DomainData)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreateProduct {
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub product_type: ProductType,
|
||||||
|
pub price: ProductPrice,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct UpdateProductName {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct UpdateProductDescription {
|
||||||
|
pub description: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct UpdateProductPrice {
|
||||||
|
pub price: ProductPrice,
|
||||||
|
}
|
||||||
|
|
162
crates/app/src/routes/api/v1/products.rs
Normal file
162
crates/app/src/routes/api/v1/products.rs
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
use crate::{
|
||||||
|
get_user_from_token,
|
||||||
|
routes::api::v1::{
|
||||||
|
CreateProduct, UpdateProductDescription, UpdateProductName, UpdateProductPrice,
|
||||||
|
},
|
||||||
|
State,
|
||||||
|
};
|
||||||
|
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||||||
|
use axum_extra::extract::CookieJar;
|
||||||
|
use tetratto_core::model::{products::Product, oauth, ApiReturn, Error};
|
||||||
|
|
||||||
|
pub async fn get_request(
|
||||||
|
Path(id): Path<usize>,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
match data.get_product_by_id(id).await {
|
||||||
|
Ok(x) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Success".to_string(),
|
||||||
|
payload: Some(x),
|
||||||
|
}),
|
||||||
|
Err(e) => return Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReadProducts) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data.get_products_by_user(user.id).await {
|
||||||
|
Ok(x) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Success".to_string(),
|
||||||
|
payload: Some(x),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Json(req): Json<CreateProduct>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserCreateProducts) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data
|
||||||
|
.create_product(Product::new(
|
||||||
|
user.id,
|
||||||
|
req.name,
|
||||||
|
req.description,
|
||||||
|
req.price,
|
||||||
|
req.product_type,
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(x) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Product created".to_string(),
|
||||||
|
payload: x.id.to_string(),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_name_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Path(id): Path<usize>,
|
||||||
|
Json(req): Json<UpdateProductName>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageProducts) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data.update_product_name(id, &user, &req.name).await {
|
||||||
|
Ok(_) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Product updated".to_string(),
|
||||||
|
payload: (),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_description_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Path(id): Path<usize>,
|
||||||
|
Json(req): Json<UpdateProductDescription>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageProducts) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data
|
||||||
|
.update_product_description(id, &user, &req.description)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Product updated".to_string(),
|
||||||
|
payload: (),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_price_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Path(id): Path<usize>,
|
||||||
|
Json(req): Json<UpdateProductPrice>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageProducts) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data.update_product_price(id, &user, req.price).await {
|
||||||
|
Ok(_) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Product updated".to_string(),
|
||||||
|
payload: (),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Path(id): Path<usize>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageProducts) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data.delete_product(id, &user).await {
|
||||||
|
Ok(_) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Product deleted".to_string(),
|
||||||
|
payload: (),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,5 @@ CREATE TABLE IF NOT EXISTS products (
|
||||||
likes INT NOT NULL,
|
likes INT NOT NULL,
|
||||||
dislikes INT NOT NULL,
|
dislikes INT NOT NULL,
|
||||||
product_type TEXT NOT NULL,
|
product_type TEXT NOT NULL,
|
||||||
stripe_id TEXT NOT NULL,
|
|
||||||
price TEXT NOT NULL
|
price TEXT NOT NULL
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::model::{
|
use crate::model::{
|
||||||
auth::User,
|
auth::User,
|
||||||
products::Product,
|
|
||||||
permissions::{FinePermission, SecondaryPermission},
|
permissions::{FinePermission, SecondaryPermission},
|
||||||
|
products::{Product, ProductPrice},
|
||||||
Error, Result,
|
Error, Result,
|
||||||
};
|
};
|
||||||
use crate::{auto_method, DataManager};
|
use crate::{auto_method, DataManager};
|
||||||
|
@ -19,7 +19,6 @@ impl DataManager {
|
||||||
likes: get!(x->5(i32)) as isize,
|
likes: get!(x->5(i32)) as isize,
|
||||||
dislikes: get!(x->6(i32)) as isize,
|
dislikes: get!(x->6(i32)) as isize,
|
||||||
product_type: serde_json::from_str(&get!(x->7(String))).unwrap(),
|
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(),
|
price: serde_json::from_str(&get!(x->9(String))).unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +84,7 @@ impl DataManager {
|
||||||
|
|
||||||
let res = execute!(
|
let res = execute!(
|
||||||
&conn,
|
&conn,
|
||||||
"INSERT INTO products VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
"INSERT INTO products VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||||
params![
|
params![
|
||||||
&(data.id as i64),
|
&(data.id as i64),
|
||||||
&(data.created as i64),
|
&(data.created as i64),
|
||||||
|
@ -95,7 +94,6 @@ impl DataManager {
|
||||||
&0_i32,
|
&0_i32,
|
||||||
&0_i32,
|
&0_i32,
|
||||||
&serde_json::to_string(&data.product_type).unwrap(),
|
&serde_json::to_string(&data.product_type).unwrap(),
|
||||||
&data.stripe_id,
|
|
||||||
&serde_json::to_string(&data.price).unwrap(),
|
&serde_json::to_string(&data.price).unwrap(),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
@ -135,4 +133,8 @@ impl DataManager {
|
||||||
self.0.1.remove(format!("atto.product:{}", id)).await;
|
self.0.1.remove(format!("atto.product:{}", id)).await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto_method!(update_product_name(&str)@get_product_by_id:FinePermission::MANAGE_USERS; -> "UPDATE products SET name = $1 WHERE id = $2" --cache-key-tmpl="atto.product:{}");
|
||||||
|
auto_method!(update_product_description(&str)@get_product_by_id:FinePermission::MANAGE_USERS; -> "UPDATE products SET description = $1 WHERE id = $2" --cache-key-tmpl="atto.product:{}");
|
||||||
|
auto_method!(update_product_price(ProductPrice)@get_product_by_id:FinePermission::MANAGE_USERS; -> "UPDATE products SET price = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.product:{}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,8 @@ pub enum AppScope {
|
||||||
UserReadDomains,
|
UserReadDomains,
|
||||||
/// Read the user's services.
|
/// Read the user's services.
|
||||||
UserReadServices,
|
UserReadServices,
|
||||||
|
/// Read the user's products.
|
||||||
|
UserReadProducts,
|
||||||
/// Create posts as the user.
|
/// Create posts as the user.
|
||||||
UserCreatePosts,
|
UserCreatePosts,
|
||||||
/// Create messages as the user.
|
/// Create messages as the user.
|
||||||
|
@ -98,6 +100,8 @@ pub enum AppScope {
|
||||||
UserCreateDomains,
|
UserCreateDomains,
|
||||||
/// Create services on behalf of the user.
|
/// Create services on behalf of the user.
|
||||||
UserCreateServices,
|
UserCreateServices,
|
||||||
|
/// Create products on behalf of the user.
|
||||||
|
UserCreateProducts,
|
||||||
/// Delete posts owned by the user.
|
/// Delete posts owned by the user.
|
||||||
UserDeletePosts,
|
UserDeletePosts,
|
||||||
/// Delete messages owned by the user.
|
/// Delete messages owned by the user.
|
||||||
|
@ -138,6 +142,8 @@ pub enum AppScope {
|
||||||
UserManageDomains,
|
UserManageDomains,
|
||||||
/// Manage the user's services.
|
/// Manage the user's services.
|
||||||
UserManageServices,
|
UserManageServices,
|
||||||
|
/// Manage the user's products.
|
||||||
|
UserManageProducts,
|
||||||
/// Edit posts created by the user.
|
/// Edit posts created by the user.
|
||||||
UserEditPosts,
|
UserEditPosts,
|
||||||
/// Edit drafts created by the user.
|
/// Edit drafts created by the user.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
|
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
|
||||||
|
|
||||||
|
@ -11,14 +13,13 @@ pub struct Product {
|
||||||
pub likes: isize,
|
pub likes: isize,
|
||||||
pub dislikes: isize,
|
pub dislikes: isize,
|
||||||
pub product_type: ProductType,
|
pub product_type: ProductType,
|
||||||
pub stripe_id: String,
|
|
||||||
pub price: ProductPrice,
|
pub price: ProductPrice,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub enum ProductType {
|
pub enum ProductType {
|
||||||
/// Text + images.
|
/// Text + images.
|
||||||
Message,
|
Data,
|
||||||
/// When a commission product is purchased, the creator will receive a request
|
/// When a commission product is purchased, the creator will receive a request
|
||||||
/// prompting them to respond with text + images.
|
/// prompting them to respond with text + images.
|
||||||
///
|
///
|
||||||
|
@ -26,12 +27,39 @@ pub enum ProductType {
|
||||||
/// customer, as seller input is required.
|
/// customer, as seller input is required.
|
||||||
///
|
///
|
||||||
/// If the request is deleted, the purchase should be immediately refunded.
|
/// If the request is deleted, the purchase should be immediately refunded.
|
||||||
|
///
|
||||||
|
/// Commissions are paid beforehand to prevent theft. This means it is vital
|
||||||
|
/// that refunds are enforced.
|
||||||
Commission,
|
Commission,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A currency.
|
||||||
|
#[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 => "£",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Price in USD. `(dollars, cents)`.
|
/// Price in USD. `(dollars, cents)`.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ProductPrice(u64, u64);
|
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 {
|
impl Product {
|
||||||
/// Create a new [`Product`].
|
/// Create a new [`Product`].
|
||||||
|
@ -51,7 +79,6 @@ impl Product {
|
||||||
likes: 0,
|
likes: 0,
|
||||||
dislikes: 0,
|
dislikes: 0,
|
||||||
product_type: r#type,
|
product_type: r#type,
|
||||||
stripe_id: String::new(),
|
|
||||||
price,
|
price,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue