| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use std::path::PathBuf; |
| 9 |
|
| 10 |
use goingson_core::UserId; |
| 11 |
use goingson_db_sqlite::{SqliteProjectRepository, SqliteTaskRepository}; |
| 12 |
use sqlx::SqlitePool; |
| 13 |
use uuid::Uuid; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
pub const DESKTOP_USER_ID: UserId = UserId::from_uuid(Uuid::from_u128(1)); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
pub struct Ctx { |
| 23 |
pub pool: SqlitePool, |
| 24 |
pub user_id: UserId, |
| 25 |
} |
| 26 |
|
| 27 |
impl Ctx { |
| 28 |
pub fn new(pool: SqlitePool) -> Self { |
| 29 |
Self { |
| 30 |
pool, |
| 31 |
user_id: DESKTOP_USER_ID, |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
pub fn tasks(&self) -> SqliteTaskRepository { |
| 36 |
SqliteTaskRepository::new(self.pool.clone()) |
| 37 |
} |
| 38 |
|
| 39 |
pub fn projects(&self) -> SqliteProjectRepository { |
| 40 |
SqliteProjectRepository::new(self.pool.clone()) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub fn default_db_path() -> Option<PathBuf> { |
| 48 |
let home = std::env::var_os("HOME")?; |
| 49 |
let home = PathBuf::from(home); |
| 50 |
|
| 51 |
#[cfg(target_os = "macos")] |
| 52 |
let base = home.join("Library/Application Support"); |
| 53 |
|
| 54 |
#[cfg(not(target_os = "macos"))] |
| 55 |
let base = std::env::var_os("XDG_DATA_HOME") |
| 56 |
.map(PathBuf::from) |
| 57 |
.unwrap_or_else(|| home.join(".local/share")); |
| 58 |
|
| 59 |
Some(base.join("com.goingson.app").join("goingson.db")) |
| 60 |
} |
| 61 |
|