use crate::api_client::CimaClient;
use crate::models::{Presentation, PresentationSummary};
use anyhow::{Context, Result};
#[derive(Debug, Default, Clone)]
pub struct SearchPresentationsParams {
pub national_code: Option<String>,
pub registration_number: Option<String>,
pub vmp: Option<String>,
pub vmpp: Option<String>,
pub active_ingredient_id: Option<i32>,
pub commercialized: Option<u8>,
pub narcotic: Option<u8>,
pub psychotropic: Option<u8>,
pub narcotic_or_psychotropic: Option<u8>,
pub page: Option<u32>,
}
impl SearchPresentationsParams {
pub fn new() -> Self {
Self::default()
}
pub(crate) fn to_query_params(&self) -> Vec<(&str, String)> {
let mut params = Vec::new();
if let Some(ref v) = self.national_code {
params.push(("cn", v.clone()));
}
if let Some(ref v) = self.registration_number {
params.push(("nregistro", v.clone()));
}
if let Some(ref v) = self.vmp {
params.push(("vmp", v.clone()));
}
if let Some(ref v) = self.vmpp {
params.push(("vmpp", v.clone()));
}
if let Some(v) = self.active_ingredient_id {
params.push(("idpractiv1", v.to_string()));
}
if let Some(v) = self.commercialized {
params.push(("comerc", v.to_string()));
}
if let Some(v) = self.narcotic {
params.push(("estupefaciente", v.to_string()));
}
if let Some(v) = self.psychotropic {
params.push(("psicotropo", v.to_string()));
}
if let Some(v) = self.narcotic_or_psychotropic {
params.push(("estuopsico", v.to_string()));
}
if let Some(v) = self.page {
params.push(("pagina", v.to_string()));
}
params
}
}
impl CimaClient {
pub async fn get_presentation(&self, national_code: &str) -> Result<Presentation> {
let endpoint = format!("presentacion/{}", national_code);
self.get(&endpoint)
.await
.context("Failed to get presentation")
}
pub async fn search_presentations(
&self,
params: &SearchPresentationsParams,
) -> Result<crate::models::PaginatedResponse<PresentationSummary>> {
let query_params = params.to_query_params();
self.get_with_params("presentaciones", &query_params)
.await
.context("Failed to search presentations")
}
}