koprogo_api/application/ports/
poll_repository.rs

1use crate::application::dto::{PageRequest, PollFilters};
2use crate::domain::entities::Poll;
3use async_trait::async_trait;
4use serde::Serialize;
5use uuid::Uuid;
6
7#[async_trait]
8pub trait PollRepository: Send + Sync {
9    async fn create(&self, poll: &Poll) -> Result<Poll, String>;
10    async fn find_by_id(&self, id: Uuid) -> Result<Option<Poll>, String>;
11    async fn find_by_building(&self, building_id: Uuid) -> Result<Vec<Poll>, String>;
12    async fn find_by_created_by(&self, created_by: Uuid) -> Result<Vec<Poll>, String>;
13
14    /// Find all polls with pagination and filters
15    /// Returns tuple of (polls, total_count)
16    async fn find_all_paginated(
17        &self,
18        page_request: &PageRequest,
19        filters: &PollFilters,
20    ) -> Result<(Vec<Poll>, i64), String>;
21
22    /// Find active polls (status = active and within time range)
23    async fn find_active(&self, building_id: Uuid) -> Result<Vec<Poll>, String>;
24
25    /// Find polls by status
26    async fn find_by_status(&self, building_id: Uuid, status: &str) -> Result<Vec<Poll>, String>;
27
28    /// Find expired polls that should be auto-closed
29    async fn find_expired_active(&self) -> Result<Vec<Poll>, String>;
30
31    async fn update(&self, poll: &Poll) -> Result<Poll, String>;
32    async fn delete(&self, id: Uuid) -> Result<bool, String>;
33
34    /// Get poll statistics for a building
35    async fn get_building_statistics(&self, building_id: Uuid) -> Result<PollStatistics, String>;
36}
37
38#[derive(Debug, Clone, Serialize)]
39pub struct PollStatistics {
40    pub total_polls: i64,
41    pub active_polls: i64,
42    pub closed_polls: i64,
43    pub average_participation_rate: f64,
44}