Admin get all orders

This commit is contained in:
eraden 2022-05-01 18:25:27 +02:00
parent ad2b08f22b
commit 791d32d0d8
7 changed files with 80 additions and 4 deletions

View File

@ -33,6 +33,7 @@ pub(crate) async fn all_account_orders(
r#" r#"
SELECT id, buyer_id, status, order_id, order_ext_id SELECT id, buyer_id, status, order_id, order_ext_id
FROM account_orders FROM account_orders
ORDER BY id DESC
"#, "#,
) )
.fetch_all(&pool) .fetch_all(&pool)

View File

@ -28,6 +28,7 @@ pub(crate) async fn all_order_items(_msg: AllOrderItems, pool: PgPool) -> Result
r#" r#"
SELECT id, product_id, order_id, quantity, quantity_unit SELECT id, product_id, order_id, quantity, quantity_unit
FROM order_items FROM order_items
ORDER BY id DESC
"#, "#,
) )
.fetch_all(&pool) .fetch_all(&pool)

View File

@ -1,3 +1,5 @@
#![feature(drain_filter)]
use std::io::Write; use std::io::Write;
use actix::Actor; use actix::Actor;

View File

@ -1,3 +1,5 @@
pub mod api;
use std::fmt::Formatter; use std::fmt::Formatter;
use std::str::FromStr; use std::str::FromStr;
@ -503,12 +505,12 @@ pub struct Stock {
pub quantity_unit: QuantityUnit, pub quantity_unit: QuantityUnit,
} }
#[derive(sqlx::Type, Serialize, Deserialize, Copy, Clone, Debug, Display, Deref)] #[derive(sqlx::Type, Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Display, Deref)]
#[sqlx(transparent)] #[sqlx(transparent)]
#[serde(transparent)] #[serde(transparent)]
pub struct AccountOrderId(RecordId); pub struct AccountOrderId(RecordId);
#[derive(sqlx::Type, Serialize, Deserialize, Display, Deref)] #[derive(sqlx::Type, Serialize, Deserialize, Clone, Debug, PartialEq, Display, Deref)]
#[sqlx(transparent)] #[sqlx(transparent)]
#[serde(transparent)] #[serde(transparent)]
pub struct OrderId(String); pub struct OrderId(String);
@ -554,7 +556,7 @@ impl From<AccountOrder> for PublicAccountOrder {
#[serde(transparent)] #[serde(transparent)]
pub struct OrderItemId(pub RecordId); pub struct OrderItemId(pub RecordId);
#[derive(sqlx::FromRow, Serialize, Deserialize)] #[derive(sqlx::FromRow, Serialize, Deserialize, Debug)]
pub struct OrderItem { pub struct OrderItem {
pub id: OrderItemId, pub id: OrderItemId,
pub product_id: ProductId, pub product_id: ProductId,

44
api/src/model/api.rs Normal file
View File

@ -0,0 +1,44 @@
use serde::Serialize;
use crate::model;
use crate::model::{AccountId, AccountOrderId, OrderId, OrderItem, OrderStatus};
#[derive(Serialize, Debug)]
#[serde(transparent)]
pub struct AccountOrders(pub Vec<AccountOrder>);
impl From<(Vec<model::AccountOrder>, Vec<model::OrderItem>)> for AccountOrders {
fn from((orders, mut items): (Vec<model::AccountOrder>, Vec<model::OrderItem>)) -> Self {
Self(
orders
.into_iter()
.map(
|model::AccountOrder {
id,
buyer_id,
status,
order_id,
order_ext_id: _,
}| {
AccountOrder {
id,
buyer_id,
status,
order_id,
items: items.drain_filter(|item| item.order_id == id).collect(),
}
},
)
.collect(),
)
}
}
#[derive(Serialize, Debug)]
pub struct AccountOrder {
pub id: AccountOrderId,
pub buyer_id: AccountId,
pub status: OrderStatus,
pub order_id: Option<OrderId>,
pub items: Vec<OrderItem>,
}

View File

@ -1,4 +1,5 @@
mod accounts; mod accounts;
mod orders;
mod products; mod products;
mod stocks; mod stocks;
@ -9,6 +10,7 @@ pub fn configure(config: &mut ServiceConfig) {
scope("/api/v1") scope("/api/v1")
.configure(products::configure) .configure(products::configure)
.configure(stocks::configure) .configure(stocks::configure)
.configure(accounts::configure), .configure(accounts::configure)
.configure(orders::configure),
); );
} }

View File

@ -0,0 +1,24 @@
use actix::Addr;
use actix_session::Session;
use actix_web::web::{Data, Json, ServiceConfig};
use actix_web::{delete, get, patch, post};
use crate::database::Database;
use crate::model::api::AccountOrders;
use crate::routes::admin::Error;
use crate::routes::RequireLogin;
use crate::{admin_send_db, database, model, routes};
#[get("/orders")]
async fn orders(session: Session, db: Data<Addr<Database>>) -> routes::Result<Json<AccountOrders>> {
session.require_admin()?;
let orders: Vec<model::AccountOrder> = admin_send_db!(&db, database::AllAccountOrders);
let items: Vec<model::OrderItem> = admin_send_db!(db, database::AllOrderItems);
Ok(Json((orders, items).into()))
}
pub fn configure(config: &mut ServiceConfig) {
config.service(orders);
}