add: products api
This commit is contained in:
parent
3c4ce1fae5
commit
df5eaf24f7
29 changed files with 1022 additions and 110 deletions
|
@ -147,6 +147,8 @@ pub const MAIL_SENT: &str = include_str!("./public/html/mail/sent.lisp");
|
|||
pub const MAIL_COMPOSE: &str = include_str!("./public/html/mail/compose.lisp");
|
||||
pub const MAIL_LETTER: &str = include_str!("./public/html/mail/letter.lisp");
|
||||
|
||||
pub const ECONOMY_WALLET: &str = include_str!("./public/html/economy/wallet.lisp");
|
||||
|
||||
// langs
|
||||
pub const LANG_EN_US: &str = include_str!("./langs/en-US.toml");
|
||||
|
||||
|
@ -379,6 +381,8 @@ pub(crate) async fn write_assets(config: &Config) -> PathBufD {
|
|||
write_template!(html_path->"mail/compose.html"(crate::assets::MAIL_COMPOSE) --config=config --lisp plugins);
|
||||
write_template!(html_path->"mail/letter.html"(crate::assets::MAIL_LETTER) --config=config --lisp plugins);
|
||||
|
||||
write_template!(html_path->"economy/wallet.html"(crate::assets::ECONOMY_WALLET) -d "economy" --config=config --lisp plugins);
|
||||
|
||||
html_path
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ version = "1.0.0"
|
|||
"general:link.achievements" = "Achievements"
|
||||
"general:link.little_web" = "Little web"
|
||||
"general:link.mail" = "Mail"
|
||||
"general:link.wallet" = "Wallet"
|
||||
"general:action.save" = "Save"
|
||||
"general:action.delete" = "Delete"
|
||||
"general:action.purge" = "Purge"
|
||||
|
@ -324,3 +325,5 @@ version = "1.0.0"
|
|||
"mail:label.content" = "Content"
|
||||
"mail:action.send" = "Send"
|
||||
"mail:action.send_mail" = "Send mail"
|
||||
|
||||
"economy:label.recent_transfers" = "Recent transfers"
|
||||
|
|
|
@ -272,6 +272,16 @@ table ol {
|
|||
}
|
||||
}
|
||||
|
||||
.card.button {
|
||||
justify-content: flex-start;
|
||||
align-items: flex-start;
|
||||
flex-direction: column;
|
||||
gap: var(--pad-2);
|
||||
width: 100%;
|
||||
height: max-content;
|
||||
padding: var(--pad-4);
|
||||
}
|
||||
|
||||
/* supporter card */
|
||||
@property --border-angle {
|
||||
syntax: "<angle>";
|
||||
|
|
65
crates/app/src/public/html/economy/wallet.lisp
Normal file
65
crates/app/src/public/html/economy/wallet.lisp
Normal file
|
@ -0,0 +1,65 @@
|
|||
(text "{% extends \"root.html\" %} {% block head %}")
|
||||
(title
|
||||
(text "Wallet - {{ config.name }}"))
|
||||
(text "{% endblock %} {% block body %} {{ macros::nav(selected=\"wallet\") }}")
|
||||
(main
|
||||
("class" "flex flex_col gap_2")
|
||||
(div
|
||||
("class" "card_nest")
|
||||
(div
|
||||
("class" "card small flex items_center justify_between gap_2")
|
||||
(span
|
||||
("class" "flex items_center gap_2")
|
||||
(icon (text "piggy-bank"))
|
||||
(span (str (text "general:link.wallet")))))
|
||||
(div
|
||||
("class" "card lowered flex flex_col gap_4")
|
||||
(a
|
||||
("class" "card button raised")
|
||||
("href" "/wallet/buy")
|
||||
(b (text "Coin balance"))
|
||||
(h3
|
||||
("class" "flex gap_2 items_center")
|
||||
("style" "height: 24px")
|
||||
(icon (text "badge-cent"))
|
||||
(text "{{ user.coins }}")))))
|
||||
(div
|
||||
("class" "card_nest")
|
||||
(div
|
||||
("class" "card small flex items_center justify_between gap_2")
|
||||
(span
|
||||
("class" "flex items_center gap_2")
|
||||
(icon (text "clock"))
|
||||
(span (str (text "economy:label.recent_transfers")))))
|
||||
(div
|
||||
("class" "card lowered flex flex_col gap_4")
|
||||
(div
|
||||
("class" "w_full")
|
||||
("style" "overflow: auto")
|
||||
(table
|
||||
("class" "w_full")
|
||||
(thead
|
||||
(th (text "Created"))
|
||||
(th (text "Sender"))
|
||||
(th (text "Receiver"))
|
||||
(th (text "Amount"))
|
||||
(th (text "Product")))
|
||||
(tbody
|
||||
(text "{% for transfer in list -%}")
|
||||
(tr
|
||||
(td (span ("class" "date short") (text "{{ transfer[1] }}")))
|
||||
(td ("class" "w_content") (text "{{ components::full_username(user=transfer[3]) }}"))
|
||||
(td ("class" "w_content") (text "{{ components::full_username(user=transfer[4]) }}"))
|
||||
(td
|
||||
(text "{{ transfer[2] }}")
|
||||
(text "{% if transfer[6] -%}")
|
||||
(span ("title" "Pending") (icon (text "clock")))
|
||||
(text "{%- endif %}"))
|
||||
(td
|
||||
(text "{% if transfer[5] -%}")
|
||||
(a
|
||||
("href" "/product/{{ transfer[5].id }}")
|
||||
(icon (text "external-link")))
|
||||
(text "{%- endif %}")))
|
||||
(text "{%- endfor %}")))))))
|
||||
(text "{% endblock %}")
|
|
@ -187,6 +187,13 @@ pub async fn create_request(
|
|||
}
|
||||
}
|
||||
|
||||
// check if we're fulfilling a coin transfer
|
||||
if props.transfer_id != 0 {
|
||||
if let Err(e) = data.apply_transfer(props.transfer_id).await {
|
||||
return Json(e.into());
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
Json(ApiReturn {
|
||||
ok: true,
|
||||
|
|
|
@ -4,16 +4,17 @@ pub mod auth;
|
|||
pub mod channels;
|
||||
pub mod communities;
|
||||
pub mod domains;
|
||||
pub mod economy;
|
||||
pub mod journals;
|
||||
pub mod letters;
|
||||
pub mod notes;
|
||||
pub mod notifications;
|
||||
pub mod products;
|
||||
pub mod reactions;
|
||||
pub mod reports;
|
||||
pub mod requests;
|
||||
pub mod services;
|
||||
pub mod stacks;
|
||||
pub mod transfers;
|
||||
pub mod uploads;
|
||||
pub mod util;
|
||||
|
||||
|
@ -30,6 +31,7 @@ use tetratto_core::model::{
|
|||
PollOption, PostContext,
|
||||
},
|
||||
communities_permissions::CommunityPermission,
|
||||
economy::ProductFulfillmentMethod,
|
||||
journals::JournalPrivacyPermission,
|
||||
littleweb::{DomainData, DomainTld, ServiceFsEntry},
|
||||
oauth::AppScope,
|
||||
|
@ -706,8 +708,27 @@ pub fn routes() -> Router {
|
|||
.route("/letters/{id}/read", post(letters::add_read_request))
|
||||
.route("/letters/sent", get(letters::list_sent_request))
|
||||
.route("/letters/received", get(letters::list_received_request))
|
||||
// economy
|
||||
.route("/transfers", post(economy::create_request))
|
||||
// transfers
|
||||
.route("/transfers", post(transfers::create_request))
|
||||
// products
|
||||
.route("/products", post(products::create_request))
|
||||
.route("/products/{id}", delete(products::delete_request))
|
||||
.route("/products/{id}/buy", post(products::buy_request))
|
||||
.route("/products/{id}/title", post(products::update_title_request))
|
||||
.route(
|
||||
"/products/{id}/description",
|
||||
post(products::update_description_request),
|
||||
)
|
||||
.route(
|
||||
"/products/{id}/on_sale",
|
||||
post(products::update_on_sale_request),
|
||||
)
|
||||
.route("/products/{id}/price", post(products::update_price_request))
|
||||
.route(
|
||||
"/products/{id}/method",
|
||||
post(products::update_method_request),
|
||||
)
|
||||
.route("/products/{id}/stock", post(products::update_stock_request))
|
||||
}
|
||||
|
||||
pub fn lw_routes() -> Router {
|
||||
|
@ -1214,6 +1235,8 @@ pub struct CreateLetter {
|
|||
pub subject: String,
|
||||
pub content: String,
|
||||
pub replying_to: String,
|
||||
#[serde(default)]
|
||||
pub transfer_id: usize,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -1221,3 +1244,39 @@ pub struct CreateCoinTransfer {
|
|||
pub receiver: usize,
|
||||
pub amount: i32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateProduct {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductTitle {
|
||||
pub title: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductDescription {
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductOnSale {
|
||||
pub on_sale: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductPrice {
|
||||
pub price: i32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductMethod {
|
||||
pub method: ProductFulfillmentMethod,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductStock {
|
||||
pub stock: i32,
|
||||
}
|
||||
|
|
225
crates/app/src/routes/api/v1/products.rs
Normal file
225
crates/app/src/routes/api/v1/products.rs
Normal file
|
@ -0,0 +1,225 @@
|
|||
use crate::{get_user_from_token, State, cookie::CookieJar};
|
||||
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||||
use tetratto_core::model::{economy::Product, oauth, ApiReturn, Error};
|
||||
use super::{
|
||||
CreateProduct, UpdateProductDescription, UpdateProductMethod, UpdateProductOnSale,
|
||||
UpdateProductPrice, UpdateProductStock, UpdateProductTitle,
|
||||
};
|
||||
|
||||
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.title, req.description))
|
||||
.await
|
||||
{
|
||||
Ok(s) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Product created".to_string(),
|
||||
payload: s.id.to_string(),
|
||||
}),
|
||||
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::UserCreateProducts) {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_title_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(mut req): Json<UpdateProductTitle>,
|
||||
) -> 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()),
|
||||
};
|
||||
|
||||
req.title = req.title.trim().to_string();
|
||||
if req.title.len() < 2 {
|
||||
return Json(Error::DataTooShort("title".to_string()).into());
|
||||
} else if req.title.len() > 128 {
|
||||
return Json(Error::DataTooLong("title".to_string()).into());
|
||||
}
|
||||
|
||||
match data.update_product_title(id, &user, &req.title).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(mut 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()),
|
||||
};
|
||||
|
||||
req.description = req.description.trim().to_string();
|
||||
if req.description.len() < 2 {
|
||||
return Json(Error::DataTooShort("description".to_string()).into());
|
||||
} else if req.description.len() > 1024 {
|
||||
return Json(Error::DataTooLong("description".to_string()).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_on_sale_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateProductOnSale>,
|
||||
) -> 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_on_sale(id, &user, if req.on_sale { 1 } else { 0 })
|
||||
.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 update_method_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateProductMethod>,
|
||||
) -> 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_method(id, &user, req.method).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Product updated".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_stock_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateProductStock>,
|
||||
) -> 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_stock(id, &user, req.stock).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Product updated".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn buy_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let mut user = match get_user_from_token!(jar, data, oauth::AppScope::UserSendCoins) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data.purchase_product(id, &mut user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Product purchased".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
use crate::{get_user_from_token, State, cookie::CookieJar};
|
||||
use axum::{response::IntoResponse, Extension, Json};
|
||||
use tetratto_core::model::{economy::CoinTransfer, oauth, ApiReturn, Error};
|
||||
use tetratto_core::model::{
|
||||
economy::{CoinTransfer, CoinTransferMethod},
|
||||
oauth, ApiReturn, Error,
|
||||
};
|
||||
use super::CreateCoinTransfer;
|
||||
|
||||
pub async fn create_request(
|
||||
|
@ -15,13 +18,21 @@ pub async fn create_request(
|
|||
};
|
||||
|
||||
match data
|
||||
.create_transfer(CoinTransfer::new(user.id, req.receiver, req.amount))
|
||||
.create_transfer(
|
||||
&mut CoinTransfer::new(
|
||||
user.id,
|
||||
req.receiver,
|
||||
req.amount,
|
||||
CoinTransferMethod::Transfer, // this endpoint is ONLY for regular transfers; products export a buy endpoint for the other method
|
||||
),
|
||||
true,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(s) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Stack created".to_string(),
|
||||
payload: s.id.to_string(),
|
||||
message: "Transfer created".to_string(),
|
||||
payload: s.to_string(),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
45
crates/app/src/routes/pages/economy.rs
Normal file
45
crates/app/src/routes/pages/economy.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use axum::{
|
||||
extract::{Query, Path},
|
||||
response::{Html, IntoResponse},
|
||||
Extension,
|
||||
};
|
||||
use crate::cookie::CookieJar;
|
||||
use tetratto_core::model::Error;
|
||||
use crate::{assets::initial_context, get_lang, get_user_from_token, State};
|
||||
use super::{render_error, PaginatedQuery};
|
||||
|
||||
/// `/wallet`
|
||||
pub async fn wallet_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.read().await;
|
||||
let user = match get_user_from_token!(jar, data.0) {
|
||||
Some(ua) => ua,
|
||||
None => {
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let list = match data.0.get_transfers_by_user(user.id, 12, props.page).await {
|
||||
Ok(x) => match data.0.fill_transfers(x).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
};
|
||||
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
||||
|
||||
context.insert("list", &list);
|
||||
context.insert("page", &props.page);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
data.1.render("economy/wallet.html", &context).unwrap(),
|
||||
))
|
||||
}
|
|
@ -2,6 +2,7 @@ pub mod auth;
|
|||
pub mod chats;
|
||||
pub mod communities;
|
||||
pub mod developer;
|
||||
pub mod economy;
|
||||
pub mod forge;
|
||||
pub mod journals;
|
||||
pub mod littleweb;
|
||||
|
@ -159,6 +160,8 @@ pub fn routes() -> Router {
|
|||
.route("/mail/sent", get(mail::sent_request))
|
||||
.route("/mail/compose", get(mail::compose_request))
|
||||
.route("/mail/letter/{id}", get(mail::letter_request))
|
||||
// economy
|
||||
.route("/wallet", get(economy::wallet_request))
|
||||
}
|
||||
|
||||
pub fn lw_routes() -> Router {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue