Started to use db pool.

This commit is contained in:
didier amyot committed 2023-03-09 14:04:12 -05:00
1 parent eebe848e4b
commit 5df6d5afd1
6 files changed
+66 -31

No files matched your search

+15 -19
View File
@@ -1,31 +1,27 @@
use std::path::PathBuf;
use r2d2_sqlite::SqliteConnectionManager;
use crate::services::{SessionService};
use crate::util::{SQLiteConnectionFactory};
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
pub type DbPool = r2d2::Pool<SqliteConnectionManager>;
pub fn creat_db_pool(path: & PathBuf) -> DbPool {
let manager = SqliteConnectionManager::file(path);
r2d2::Pool::new(manager).unwrap()
}
struct ServicesCache {
session_service: SessionService,
}
struct Services {
conn_fact: SQLiteConnectionFactory,
cache: ServicesCache,
}
pub struct Services {}
impl Services {
pub fn new(conn_fact: SQLiteConnectionFactory) -> Services {
let mut tmp_conn = conn_fact.connection().unwrap();
let session_service = SessionService::from_conn(&mut tmp_conn).unwrap();
let cache = ServicesCache { session_service };
Services { conn_fact, cache }
pub fn get_authentication_service(dbPool: & DbPool) -> CustomResult<SessionService> {
let mut conn = dbPool.get().unwrap();
SessionService::from_conn(&mut conn)
}
pub fn get_authentication_service(&self) -> &SessionService {
&self.cache.session_service
}
pub fn get_user_service(&self) -> i32 {
// let mut conn = self.conn_fact.readonly().unwrap(); // todo; to proper 500
// let service_factory = ReadOnlyServiceFactory::new(&mut conn);
// service_factory.get_user_service()
88
}
}