Skip to content

Commit

Permalink
feat(api): return a list of users with orders
Browse files Browse the repository at this point in the history
  • Loading branch information
i007c committed May 11, 2024
1 parent cce17de commit 61e70ed
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/admin/order.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_web::web::{Data, Json, Query};
use actix_web::{get, post, HttpResponse, Scope};
use serde::Deserialize;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use utoipa::{OpenApi, ToSchema};

use crate::docs::UpdatePaths;
Expand All @@ -13,12 +14,18 @@ use crate::AppState;
#[openapi(
tags((name = "admin::order")),
paths(order_list, order_get, order_update),
components(schemas(UpdateOrder)),
components(schemas(UpdateOrder, OrderList)),
servers((url = "/orders")),
modifiers(&UpdatePaths)
)]
pub struct ApiDoc;

#[derive(Serialize, ToSchema)]
struct OrderList {
orders: Vec<Order>,
users: Vec<User>,
}

#[utoipa::path(
get,
params(("page" = u32, Query, example = 0)),
Expand All @@ -28,7 +35,7 @@ pub struct ApiDoc;
#[get("/")]
async fn order_list(
_: Admin, query: Query<ListInput>, state: Data<AppState>,
) -> Response<Vec<Order>> {
) -> Response<OrderList> {
let offset = query.page * 32;

let orders = sqlx::query_as! {
Expand All @@ -37,7 +44,17 @@ async fn order_list(
.fetch_all(&state.sql)
.await?;

Ok(Json(orders))
let user_ids = orders.iter().map(|o| o.user).unique().join(", ");

let mut users = sqlx::query_as! {
User, "select * from users where id in (?)", user_ids
}
.fetch_all(&state.sql)
.await?;

users.iter_mut().for_each(|user| user.token = String::new());

Ok(Json(OrderList { orders, users }))
}

#[utoipa::path(
Expand Down

0 comments on commit 61e70ed

Please sign in to comment.