Serve priced open interest for active unarchived market listings
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use crate::MarketDataError;
|
||||
use anyhow::{ensure, Context, Result};
|
||||
use rusqlite::{Connection, Transaction};
|
||||
use serde_json::json;
|
||||
pub(crate) fn identifier(id: &str) -> Result<()> {
|
||||
ensure!(
|
||||
!id.trim().is_empty() && id.trim().chars().count() <= 256,
|
||||
@@ -81,3 +83,34 @@ pub(crate) fn directory_selector(
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub(crate) fn condition_filter(
|
||||
raw: Option<&str>,
|
||||
db: &Connection,
|
||||
) -> Result<Option<String>, MarketDataError> {
|
||||
let Some(raw) = raw else {
|
||||
return Ok(None);
|
||||
};
|
||||
let selector = IdSelector::Many(
|
||||
raw.split(',')
|
||||
.map(str::trim)
|
||||
.map(|id| {
|
||||
if hex_id(id, 64) {
|
||||
id.to_ascii_lowercase()
|
||||
} else {
|
||||
id.to_owned()
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
let ids = selector
|
||||
.ids()
|
||||
.map_err(|e| MarketDataError::Invalid(e.to_string()))?;
|
||||
let mut exists = db.prepare_cached("SELECT EXISTS(SELECT 1 FROM markets WHERE id=?)")?;
|
||||
for id in &ids {
|
||||
if !hex_id(id, 64) && !exists.query_row([id], |r| r.get::<_, bool>(0))? {
|
||||
return Err(MarketDataError::Invalid(format!("invalid condition: {id}")));
|
||||
}
|
||||
}
|
||||
Ok(Some(json!(ids).to_string()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user