koprogo_api/application/ports/
local_exchange_repository.rs

1use crate::domain::entities::LocalExchange;
2use async_trait::async_trait;
3use uuid::Uuid;
4
5/// Repository trait for LocalExchange entity (SEL - Local Exchange Trading System)
6#[async_trait]
7pub trait LocalExchangeRepository: Send + Sync {
8    /// Create a new exchange offer
9    async fn create(&self, exchange: &LocalExchange) -> Result<LocalExchange, String>;
10
11    /// Find an exchange by ID
12    async fn find_by_id(&self, id: Uuid) -> Result<Option<LocalExchange>, String>;
13
14    /// Find all exchanges for a building
15    async fn find_by_building(&self, building_id: Uuid) -> Result<Vec<LocalExchange>, String>;
16
17    /// Find exchanges by building and status
18    async fn find_by_building_and_status(
19        &self,
20        building_id: Uuid,
21        status: &str,
22    ) -> Result<Vec<LocalExchange>, String>;
23
24    /// Find exchanges by provider
25    async fn find_by_provider(&self, provider_id: Uuid) -> Result<Vec<LocalExchange>, String>;
26
27    /// Find exchanges by requester
28    async fn find_by_requester(&self, requester_id: Uuid) -> Result<Vec<LocalExchange>, String>;
29
30    /// Find exchanges by owner (as provider OR requester)
31    async fn find_by_owner(&self, owner_id: Uuid) -> Result<Vec<LocalExchange>, String>;
32
33    /// Find active exchanges (Offered, Requested, InProgress)
34    async fn find_active_by_building(
35        &self,
36        building_id: Uuid,
37    ) -> Result<Vec<LocalExchange>, String>;
38
39    /// Find available exchanges (status = Offered)
40    async fn find_available_by_building(
41        &self,
42        building_id: Uuid,
43    ) -> Result<Vec<LocalExchange>, String>;
44
45    /// Find exchanges by type
46    async fn find_by_type(
47        &self,
48        building_id: Uuid,
49        exchange_type: &str,
50    ) -> Result<Vec<LocalExchange>, String>;
51
52    /// Update an exchange
53    async fn update(&self, exchange: &LocalExchange) -> Result<LocalExchange, String>;
54
55    /// Delete an exchange
56    async fn delete(&self, id: Uuid) -> Result<bool, String>;
57
58    /// Count exchanges by building
59    async fn count_by_building(&self, building_id: Uuid) -> Result<i64, String>;
60
61    /// Count exchanges by building and status
62    async fn count_by_building_and_status(
63        &self,
64        building_id: Uuid,
65        status: &str,
66    ) -> Result<i64, String>;
67
68    /// Get total credits exchanged in a building (sum of completed exchanges)
69    async fn get_total_credits_exchanged(&self, building_id: Uuid) -> Result<i32, String>;
70
71    /// Get average exchange rating for a building
72    async fn get_average_exchange_rating(&self, building_id: Uuid) -> Result<Option<f32>, String>;
73}