add: products ui

This commit is contained in:
trisua 2025-08-08 02:17:06 -04:00
parent 8f76578f1b
commit fd529d3847
31 changed files with 1041 additions and 49 deletions

View file

@ -3,7 +3,7 @@ use axum::{
response::IntoResponse,
Extension, Json,
};
use tetratto_core::model::{auth::Notification, mail::Letter, oauth, ApiReturn, Error};
use tetratto_core::model::{mail::Letter, oauth, ApiReturn, Error};
use crate::{cookie::CookieJar, get_user_from_token, routes::pages::PaginatedQuery, State};
use super::CreateLetter;
@ -170,30 +170,19 @@ pub async fn create_request(
.await
{
Ok(l) => {
// send notifications
for x in &l.receivers {
// check if we're fulfilling a coin transfer
if !props.transfer_id.is_empty() && props.transfer_id != "0" {
if let Err(e) = data
.create_notification(Notification::new(
"You've got mail!".to_string(),
format!(
"[@{}](/api/v1/auth/user/find/{}) has sent you a [letter](/mail/letter/{}).",
user.username, user.id, l.id
),
*x,
))
.apply_transfer(match props.transfer_id.parse() {
Ok(x) => x,
Err(_) => return Json(Error::Unknown.into()),
})
.await
{
return Json(e.into());
}
}
// 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,

View file

@ -710,6 +710,7 @@ pub fn routes() -> Router {
.route("/letters/received", get(letters::list_received_request))
// transfers
.route("/transfers", post(transfers::create_request))
.route("/transfers/ask", post(transfers::ask_request))
// products
.route("/products", post(products::create_request))
.route("/products/{id}", delete(products::delete_request))
@ -1236,12 +1237,12 @@ pub struct CreateLetter {
pub content: String,
pub replying_to: String,
#[serde(default)]
pub transfer_id: usize,
pub transfer_id: String,
}
#[derive(Deserialize)]
pub struct CreateCoinTransfer {
pub receiver: usize,
pub receiver: String,
pub amount: i32,
}

View file

@ -149,6 +149,15 @@ pub async fn update_price_request(
None => return Json(Error::NotAllowed.into()),
};
if req.price < 25 {
return Json(
Error::MiscError(
"Price is too low, please a price of use 25 coins or more".to_string(),
)
.into(),
);
}
match data.update_product_price(id, &user, req.price).await {
Ok(_) => Json(ApiReturn {
ok: true,

View file

@ -2,7 +2,9 @@ use crate::{get_user_from_token, State, cookie::CookieJar};
use axum::{response::IntoResponse, Extension, Json};
use tetratto_core::model::{
economy::{CoinTransfer, CoinTransferMethod},
oauth, ApiReturn, Error,
oauth,
requests::{ActionData, ActionRequest, ActionType},
ApiReturn, Error,
};
use super::CreateCoinTransfer;
@ -21,7 +23,10 @@ pub async fn create_request(
.create_transfer(
&mut CoinTransfer::new(
user.id,
req.receiver,
match req.receiver.parse() {
Ok(x) => x,
Err(_) => return Json(Error::Unknown.into()),
},
req.amount,
CoinTransferMethod::Transfer, // this endpoint is ONLY for regular transfers; products export a buy endpoint for the other method
),
@ -37,3 +42,35 @@ pub async fn create_request(
Err(e) => Json(e.into()),
}
}
pub async fn ask_request(
jar: CookieJar,
Extension(data): Extension<State>,
Json(req): Json<CreateCoinTransfer>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserSendCoins) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data
.create_request(ActionRequest::new(
match req.receiver.parse() {
Ok(x) => x,
Err(_) => return Json(Error::Unknown.into()),
},
ActionType::Transfer,
user.id,
Some(ActionData::Int32(req.amount)),
))
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Asked user for transfer".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}