fish_lib/traits/
repository.rs

1use crate::database::DatabaseInterface;
2use crate::game::errors::database::GameDatabaseError;
3use crate::game::errors::repository::GameRepositoryError;
4use crate::traits::model::Model;
5use diesel::r2d2::{ConnectionManager, PooledConnection};
6use diesel::PgConnection;
7use std::sync::{Arc, RwLock};
8
9pub trait Repository<T: Model>: Send + Sync {
10    fn get_db(&self) -> Arc<RwLock<dyn DatabaseInterface>>;
11    fn create(&self, new_entity: T::InsertType) -> Result<T, GameRepositoryError>;
12    fn find(&self, id: T::PrimaryKeyType) -> Result<Option<T>, GameRepositoryError>;
13    fn save(&self, entity: T) -> Result<T, GameRepositoryError>;
14    fn delete(&self, entity: T) -> Result<bool, GameRepositoryError>;
15    fn get_connection(
16        &self,
17    ) -> Result<PooledConnection<ConnectionManager<PgConnection>>, GameDatabaseError> {
18        self.get_db()
19            .read()
20            .expect("Failed to get read lock on DB")
21            .get_connection()
22    }
23}