1use async_trait::async_trait;
2
3pub mod memory;
4pub mod postgres;
5
6pub trait DatabaseInternalError: std::fmt::Debug + std::error::Error + Send + Sync {}
7
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10 #[error("internal error: `{0}`")]
11 InternalError(Box<dyn DatabaseInternalError>),
12
13 #[error("Query did not modify anything")]
14 NotModified,
15
16 #[error("Row already exists")]
17 AlreadyExists,
18}
19
20impl<T: DatabaseInternalError + 'static> From<T> for Error {
21 fn from(v: T) -> Self {
22 Self::InternalError(Box::new(v))
23 }
24}
25
26use houseflow_types::{Device, DeviceID, Room, Structure, User, UserID, UserStructure};
27
28#[async_trait]
29pub trait Database: Send + Sync {
30 async fn add_structure(&self, structure: &Structure) -> Result<(), Error>;
31
32 async fn add_room(&self, room: &Room) -> Result<(), Error>;
33
34 async fn add_device(&self, device: &Device) -> Result<(), Error>;
35
36 async fn add_user_structure(&self, user_structure: &UserStructure) -> Result<(), Error>;
37
38 async fn add_user(&self, user: &User) -> Result<(), Error>;
39
40 async fn get_device(&self, device_id: &DeviceID) -> Result<Option<Device>, Error>;
41
42 async fn get_user_devices(&self, user_id: &UserID) -> Result<Vec<Device>, Error>;
43
44 async fn get_user(&self, user_id: &UserID) -> Result<Option<User>, Error>;
45
46 async fn get_user_by_email(&self, email: &str) -> Result<Option<User>, Error>;
47
48 async fn check_user_device_access(
49 &self,
50 user_id: &UserID,
51 device_id: &DeviceID,
52 ) -> Result<bool, Error>;
53
54 async fn check_user_device_manager_access(
55 &self,
56 user_id: &UserID,
57 device_id: &DeviceID,
58 ) -> Result<bool, Error>;
59
60 async fn check_user_admin(&self, user_id: &UserID) -> Result<bool, Error>;
61
62 async fn delete_user(&self, user_id: &UserID) -> Result<(), Error>;
63}