|
|
|
@ -1,4 +1,4 @@
|
|
|
|
use chrono::{NaiveDate, NaiveDateTime};
|
|
|
|
use chrono::{Duration, NaiveDate, NaiveDateTime, Utc};
|
|
|
|
use rocket::http::Status;
|
|
|
|
use rocket::http::Status;
|
|
|
|
use rocket::response::status::Custom;
|
|
|
|
use rocket::response::status::Custom;
|
|
|
|
use sea_orm::{ColumnTrait, Order};
|
|
|
|
use sea_orm::{ColumnTrait, Order};
|
|
|
|
@ -9,71 +9,45 @@ use serde::{Deserialize, Serialize};
|
|
|
|
use crate::db::paginate::{paginate_also_related, PaginatedResponse};
|
|
|
|
use crate::db::paginate::{paginate_also_related, PaginatedResponse};
|
|
|
|
use crate::{db::Db, model};
|
|
|
|
use crate::{db::Db, model};
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/transaction?<page>&<size>")]
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub async fn get_all(
|
|
|
|
pub struct TransactionCompany {
|
|
|
|
conn: Connection<'_, Db>,
|
|
|
|
pub id: i32,
|
|
|
|
page: Option<u64>,
|
|
|
|
pub foreign_id: String,
|
|
|
|
size: Option<u64>,
|
|
|
|
pub date_published: NaiveDate,
|
|
|
|
) -> Result<Json<PaginatedResponse<TransactionCompany>>, Custom<String>> {
|
|
|
|
pub date_executed: NaiveDate,
|
|
|
|
let res = paginate_also_related::<
|
|
|
|
pub person: String,
|
|
|
|
model::transaction::Entity,
|
|
|
|
pub exchange: String,
|
|
|
|
model::company::Entity,
|
|
|
|
pub nature: String,
|
|
|
|
model::transaction::Model,
|
|
|
|
pub isin: Option<String>,
|
|
|
|
model::company::Model,
|
|
|
|
pub instrument: String,
|
|
|
|
model::transaction::Column,
|
|
|
|
pub volume: i32,
|
|
|
|
>(
|
|
|
|
pub unit_price: f32,
|
|
|
|
conn,
|
|
|
|
pub created_at_utc: NaiveDateTime,
|
|
|
|
page,
|
|
|
|
pub company: Option<model::company::Model>,
|
|
|
|
size,
|
|
|
|
|
|
|
|
Some(model::transaction::Column::DatePublished),
|
|
|
|
|
|
|
|
Some(Order::Desc),
|
|
|
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.await
|
|
|
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
|
|
|
Custom(
|
|
|
|
|
|
|
|
Status::InternalServerError,
|
|
|
|
|
|
|
|
format!("Database error: {}", e),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let list = res
|
|
|
|
|
|
|
|
.list
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.map(|t| TransactionCompany {
|
|
|
|
|
|
|
|
id: t.0.id,
|
|
|
|
|
|
|
|
foreign_id: t.0.foreign_id.to_owned(),
|
|
|
|
|
|
|
|
date_published: t.0.date_published,
|
|
|
|
|
|
|
|
date_executed: t.0.date_executed,
|
|
|
|
|
|
|
|
person: t.0.person.to_owned(),
|
|
|
|
|
|
|
|
exchange: t.0.exchange.to_owned(),
|
|
|
|
|
|
|
|
nature: t.0.nature.to_owned(),
|
|
|
|
|
|
|
|
isin: t.0.isin.clone(),
|
|
|
|
|
|
|
|
instrument: t.0.instrument.to_owned(),
|
|
|
|
|
|
|
|
volume: t.0.volume,
|
|
|
|
|
|
|
|
unit_price: t.0.unit_price,
|
|
|
|
|
|
|
|
created_at: t.0.created_at,
|
|
|
|
|
|
|
|
company: t.1.to_owned(),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let res = PaginatedResponse {
|
|
|
|
|
|
|
|
count: res.count,
|
|
|
|
|
|
|
|
num_pages: res.num_pages,
|
|
|
|
|
|
|
|
list,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Json(res))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/transaction/<company_slug>?<page>&<size>")]
|
|
|
|
#[get("/transaction?<company_slug>&<page>&<size>&<hours>")]
|
|
|
|
pub async fn get_by_company_id(
|
|
|
|
pub async fn get_transactions(
|
|
|
|
conn: Connection<'_, Db>,
|
|
|
|
conn: Connection<'_, Db>,
|
|
|
|
company_slug: String,
|
|
|
|
company_slug: Option<String>,
|
|
|
|
|
|
|
|
hours: Option<i64>,
|
|
|
|
page: Option<u64>,
|
|
|
|
page: Option<u64>,
|
|
|
|
size: Option<u64>,
|
|
|
|
size: Option<u64>,
|
|
|
|
) -> Result<Json<PaginatedResponse<TransactionCompany>>, Custom<String>> {
|
|
|
|
) -> Result<Json<PaginatedResponse<TransactionCompany>>, Custom<String>> {
|
|
|
|
let filter = model::company::Column::Slug.eq(company_slug);
|
|
|
|
let mut filters = vec![];
|
|
|
|
|
|
|
|
if let Some(c) = company_slug {
|
|
|
|
|
|
|
|
filters.push(model::company::Column::Slug.eq(c))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(h) = hours {
|
|
|
|
|
|
|
|
filters.push(
|
|
|
|
|
|
|
|
model::transaction::Column::CreatedAtUtc.gte(
|
|
|
|
|
|
|
|
Utc::now()
|
|
|
|
|
|
|
|
.naive_utc()
|
|
|
|
|
|
|
|
.checked_sub_signed(Duration::hours(h)),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let res = paginate_also_related::<
|
|
|
|
let res = paginate_also_related::<
|
|
|
|
model::transaction::Entity,
|
|
|
|
model::transaction::Entity,
|
|
|
|
model::company::Entity,
|
|
|
|
model::company::Entity,
|
|
|
|
@ -86,7 +60,7 @@ pub async fn get_by_company_id(
|
|
|
|
size,
|
|
|
|
size,
|
|
|
|
Some(model::transaction::Column::DatePublished),
|
|
|
|
Some(model::transaction::Column::DatePublished),
|
|
|
|
Some(Order::Desc),
|
|
|
|
Some(Order::Desc),
|
|
|
|
Some(filter),
|
|
|
|
Some(filters),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
.map_err(|e| {
|
|
|
|
@ -111,7 +85,7 @@ pub async fn get_by_company_id(
|
|
|
|
instrument: t.0.instrument.to_owned(),
|
|
|
|
instrument: t.0.instrument.to_owned(),
|
|
|
|
volume: t.0.volume,
|
|
|
|
volume: t.0.volume,
|
|
|
|
unit_price: t.0.unit_price,
|
|
|
|
unit_price: t.0.unit_price,
|
|
|
|
created_at: t.0.created_at,
|
|
|
|
created_at_utc: t.0.created_at_utc,
|
|
|
|
company: t.1.to_owned(),
|
|
|
|
company: t.1.to_owned(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
.collect();
|
|
|
|
@ -124,20 +98,3 @@ pub async fn get_by_company_id(
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Json(res))
|
|
|
|
Ok(Json(res))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
|
|
|
pub struct TransactionCompany {
|
|
|
|
|
|
|
|
pub id: i32,
|
|
|
|
|
|
|
|
pub foreign_id: String,
|
|
|
|
|
|
|
|
pub date_published: NaiveDate,
|
|
|
|
|
|
|
|
pub date_executed: NaiveDate,
|
|
|
|
|
|
|
|
pub person: String,
|
|
|
|
|
|
|
|
pub exchange: String,
|
|
|
|
|
|
|
|
pub nature: String,
|
|
|
|
|
|
|
|
pub isin: Option<String>,
|
|
|
|
|
|
|
|
pub instrument: String,
|
|
|
|
|
|
|
|
pub volume: i32,
|
|
|
|
|
|
|
|
pub unit_price: f32,
|
|
|
|
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
|
|
|
|
pub company: Option<model::company::Model>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|