|
|
|
@ -1,15 +1,10 @@
|
|
|
|
use chrono::{Duration, NaiveDate, NaiveDateTime, Utc};
|
|
|
|
use chrono::{Duration, NaiveDate, NaiveDateTime, Utc};
|
|
|
|
use rocket::http::Status;
|
|
|
|
use rocket::{http::Status, response::status::Custom};
|
|
|
|
use rocket::response::status::Custom;
|
|
|
|
use sea_orm::{
|
|
|
|
use sea_orm::prelude::*;
|
|
|
|
prelude::*, DbBackend, FromQueryResult, ItemsAndPagesNumber, JoinType, Order, QueryOrder,
|
|
|
|
use sea_orm::FromQueryResult;
|
|
|
|
QuerySelect, Statement,
|
|
|
|
use sea_orm::ItemsAndPagesNumber;
|
|
|
|
};
|
|
|
|
use sea_orm::JoinType;
|
|
|
|
use sea_orm_rocket::{rocket::serde::json::Json, Connection};
|
|
|
|
use sea_orm::Order;
|
|
|
|
|
|
|
|
use sea_orm::QueryOrder;
|
|
|
|
|
|
|
|
use sea_orm::QuerySelect;
|
|
|
|
|
|
|
|
use sea_orm_rocket::rocket::serde::json::Json;
|
|
|
|
|
|
|
|
use sea_orm_rocket::Connection;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::db::paginate::{paginate_also_related, PaginatedResponse};
|
|
|
|
use crate::db::paginate::{paginate_also_related, PaginatedResponse};
|
|
|
|
@ -105,6 +100,72 @@ pub async fn get_transactions(
|
|
|
|
Ok(Json(res))
|
|
|
|
Ok(Json(res))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(FromQueryResult, Serialize)]
|
|
|
|
|
|
|
|
pub struct LatestTransaction {
|
|
|
|
|
|
|
|
company_name: String,
|
|
|
|
|
|
|
|
slug: String,
|
|
|
|
|
|
|
|
nature: String,
|
|
|
|
|
|
|
|
total: f32,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/transaction/latest?<page>&<size>")]
|
|
|
|
|
|
|
|
pub async fn get_latest_transactions(
|
|
|
|
|
|
|
|
conn: Connection<'_, Db>,
|
|
|
|
|
|
|
|
page: Option<u64>,
|
|
|
|
|
|
|
|
size: Option<u64>,
|
|
|
|
|
|
|
|
) -> Result<Json<PaginatedResponse<LatestTransaction>>, Custom<String>> {
|
|
|
|
|
|
|
|
let db = conn.into_inner();
|
|
|
|
|
|
|
|
let s = size.unwrap_or(20).min(50);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let query_raw = "SELECT
|
|
|
|
|
|
|
|
company.name as company_name,
|
|
|
|
|
|
|
|
company.slug,
|
|
|
|
|
|
|
|
transaction.nature,
|
|
|
|
|
|
|
|
SUM(transaction.volume * transaction.unit_price) as total
|
|
|
|
|
|
|
|
FROM transaction
|
|
|
|
|
|
|
|
JOIN company ON transaction.company_id = company.id
|
|
|
|
|
|
|
|
WHERE DATE(created_at_utc) IN (SELECT DATE(MAX(created_at_utc)) FROM transaction)
|
|
|
|
|
|
|
|
GROUP BY company.name, transaction.nature
|
|
|
|
|
|
|
|
ORDER BY transaction.nature, total DESC"
|
|
|
|
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let query = model::transaction::Entity::find()
|
|
|
|
|
|
|
|
.from_raw_sql(Statement::from_string(
|
|
|
|
|
|
|
|
DbBackend::MySql,
|
|
|
|
|
|
|
|
query_raw.to_string(),
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.into_model::<LatestTransaction>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let pages = query.paginate(db, s);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let ItemsAndPagesNumber {
|
|
|
|
|
|
|
|
number_of_items: count,
|
|
|
|
|
|
|
|
number_of_pages: num_pages,
|
|
|
|
|
|
|
|
} = pages.num_items_and_pages().await.map_err(|e| {
|
|
|
|
|
|
|
|
Custom(
|
|
|
|
|
|
|
|
Status::InternalServerError,
|
|
|
|
|
|
|
|
format!("Database error: {}", e),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let p = page.unwrap_or(0).min(num_pages);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let list = pages.fetch_page(p).await.map_err(|e| {
|
|
|
|
|
|
|
|
Custom(
|
|
|
|
|
|
|
|
Status::InternalServerError,
|
|
|
|
|
|
|
|
format!("Database error: {}", e),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let res = PaginatedResponse {
|
|
|
|
|
|
|
|
count,
|
|
|
|
|
|
|
|
num_pages,
|
|
|
|
|
|
|
|
list,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Json(res))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/transaction/aggregated?<page>&<size>&<hours>")]
|
|
|
|
#[get("/transaction/aggregated?<page>&<size>&<hours>")]
|
|
|
|
pub async fn get_aggregated_transactions(
|
|
|
|
pub async fn get_aggregated_transactions(
|
|
|
|
conn: Connection<'_, Db>,
|
|
|
|
conn: Connection<'_, Db>,
|
|
|
|
|