feat: impl add_product_to_store FTS adapter for ordering
This commit is contained in:
parent
85f50124f8
commit
501c6a4968
4 changed files with 123 additions and 0 deletions
|
@ -0,0 +1,98 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use derive_builder::Builder;
|
||||
use derive_getters::Getters;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::OrderingFTSMeili;
|
||||
use crate::ordering::application::port::output::full_text_search::{
|
||||
add_product_to_store::*, errors::*,
|
||||
};
|
||||
use crate::ordering::domain::{category_aggregate::*, product_aggregate::*};
|
||||
use crate::types::currency::*;
|
||||
//use super::errors::*;
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
|
||||
)]
|
||||
pub struct MeiliProduct {
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
image: Option<String>, // string = file_name
|
||||
price: Price,
|
||||
category_name: String,
|
||||
product_id: Uuid,
|
||||
}
|
||||
|
||||
impl MeiliProduct {
|
||||
pub fn new(product: &Product, category: &Category) -> Self {
|
||||
Self {
|
||||
name: product.name().into(),
|
||||
description: product.description().clone(),
|
||||
image: product.image().clone(),
|
||||
price: product.price().clone(),
|
||||
category_name: category.name().into(),
|
||||
product_id: *product.product_id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl AddProductToStoreFTSPort for OrderingFTSMeili {
|
||||
async fn add_product_to_store(
|
||||
&self,
|
||||
product: &Product,
|
||||
category: &Category,
|
||||
) -> OrderingFTSResult<()> {
|
||||
let store_index = self.client.index(format!("ordering-{}",category.store_id()));
|
||||
let meili_product = MeiliProduct::new(product, category);
|
||||
store_index
|
||||
.add_documents(&[meili_product], Some("product_id"))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::types::quantity::Quantity;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_meili() {
|
||||
let settings = crate::settings::tests::get_settings().await;
|
||||
let fts = OrderingFTSMeili::new(&settings.meili.url, &settings.meili.api_key);
|
||||
|
||||
let category = Category::default();
|
||||
let product = ProductBuilder::default()
|
||||
.name("test_meili_product".into())
|
||||
.description(Some("this is a test product".into()))
|
||||
.image(None)
|
||||
.price(
|
||||
PriceBuilder::default()
|
||||
.major(100)
|
||||
.minor(0)
|
||||
.currency(Currency::INR)
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
.quantity(Quantity::default())
|
||||
.sku_able(false)
|
||||
.deleted(false)
|
||||
.category_id(*category.category_id())
|
||||
.product_id(Uuid::new_v4())
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
fts.add_product_to_store(&product, &category).await.unwrap();
|
||||
}
|
||||
}
|
19
src/ordering/adapters/output/full_text_search/meili/mod.rs
Normal file
19
src/ordering/adapters/output/full_text_search/meili/mod.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use meilisearch_sdk::client::*;
|
||||
|
||||
mod add_product_to_store;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OrderingFTSMeili {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl OrderingFTSMeili {
|
||||
pub fn new(meili_url: &str, api_key: &str) -> Self {
|
||||
let client = Client::new(meili_url, Some(api_key)).unwrap();
|
||||
Self { client }
|
||||
}
|
||||
}
|
5
src/ordering/adapters/output/full_text_search/mod.rs
Normal file
5
src/ordering/adapters/output/full_text_search/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
pub mod meili;
|
|
@ -3,3 +3,4 @@
|
|||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
pub mod db;
|
||||
pub mod full_text_search;
|
||||
|
|
Loading…
Reference in a new issue