Add managed Alpaca pool
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
use crate::database;
|
||||
use crate::database::assets::update_asset_trading;
|
||||
use crate::pool::AlpacaPool;
|
||||
use crate::pool::alpaca::AlpacaPool;
|
||||
use crate::pool::postgres::PostgresPool;
|
||||
use crate::types::{Asset, Class, Exchange};
|
||||
use apca::api::v2::asset::{self, Symbol};
|
||||
use axum::{extract::Path, http::StatusCode, Extension, Json};
|
||||
use serde::Deserialize;
|
||||
use sqlx::{
|
||||
types::{time::OffsetDateTime, Uuid},
|
||||
PgPool,
|
||||
};
|
||||
use sqlx::types::time::OffsetDateTime;
|
||||
|
||||
pub async fn get_assets(
|
||||
Extension(database_pool): Extension<PgPool>,
|
||||
Extension(postgres_pool): Extension<PostgresPool>,
|
||||
) -> Result<(StatusCode, Json<Vec<Asset>>), StatusCode> {
|
||||
let assets = database::assets::get_assets(&database_pool)
|
||||
let assets = database::assets::get_assets(&postgres_pool)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
@@ -21,10 +19,10 @@ pub async fn get_assets(
|
||||
}
|
||||
|
||||
pub async fn get_asset(
|
||||
Extension(database_pool): Extension<PgPool>,
|
||||
Extension(postgres_pool): Extension<PostgresPool>,
|
||||
Path(symbol): Path<String>,
|
||||
) -> Result<(StatusCode, Json<Asset>), StatusCode> {
|
||||
let asset = database::assets::get_asset(&database_pool, &symbol)
|
||||
let asset = database::assets::get_asset(&postgres_pool, &symbol)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
@@ -42,11 +40,11 @@ pub struct AddAssetRequest {
|
||||
}
|
||||
|
||||
pub async fn add_asset(
|
||||
Extension(database_pool): Extension<PgPool>,
|
||||
Extension(postgres_pool): Extension<PostgresPool>,
|
||||
Extension(alpaca_pool): Extension<AlpacaPool>,
|
||||
Json(request): Json<AddAssetRequest>,
|
||||
) -> Result<(StatusCode, Json<Asset>), StatusCode> {
|
||||
if database::assets::get_asset(&database_pool, &request.symbol)
|
||||
if database::assets::get_asset(&postgres_pool, &request.symbol)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.is_some()
|
||||
@@ -60,11 +58,9 @@ pub async fn add_asset(
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.issue::<asset::Get>(&Symbol::Sym(request.symbol))
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
.map_err(|_| StatusCode::NOT_FOUND)?;
|
||||
|
||||
let asset = Asset {
|
||||
id: Uuid::parse_str(&asset.id.to_string())
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
|
||||
symbol: asset.symbol,
|
||||
class: Class::from(asset.class) as Class,
|
||||
exchange: Exchange::from(asset.exchange) as Exchange,
|
||||
@@ -72,7 +68,7 @@ pub async fn add_asset(
|
||||
date_added: OffsetDateTime::now_utc(),
|
||||
};
|
||||
|
||||
let asset = database::assets::add_asset(&database_pool, asset)
|
||||
let asset = database::assets::add_asset(&postgres_pool, asset)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
@@ -86,11 +82,11 @@ pub struct UpdateAssetRequest {
|
||||
}
|
||||
|
||||
pub async fn update_asset(
|
||||
Extension(database_pool): Extension<PgPool>,
|
||||
Extension(postgres_pool): Extension<PostgresPool>,
|
||||
Path(symbol): Path<String>,
|
||||
Json(request): Json<UpdateAssetRequest>,
|
||||
) -> Result<(StatusCode, Json<Asset>), StatusCode> {
|
||||
let asset = update_asset_trading(&database_pool, &symbol, request.trading)
|
||||
let asset = update_asset_trading(&postgres_pool, &symbol, request.trading)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
@@ -101,10 +97,10 @@ pub async fn update_asset(
|
||||
}
|
||||
|
||||
pub async fn delete_asset(
|
||||
Extension(database_pool): Extension<PgPool>,
|
||||
Extension(postgres_pool): Extension<PostgresPool>,
|
||||
Path(symbol): Path<String>,
|
||||
) -> Result<(StatusCode, Json<Asset>), StatusCode> {
|
||||
let asset = database::assets::delete_asset(&database_pool, &symbol)
|
||||
let asset = database::assets::delete_asset(&postgres_pool, &symbol)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
|
Reference in New Issue
Block a user