From 5ffc9b007ccbd8a4510b58de72aaee53291d7973 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 11 Jun 2025 17:11:39 -0600 Subject: refactor: apply SOLID principles --- src/repositories/sqlite.rs | 164 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 src/repositories/sqlite.rs (limited to 'src/repositories/sqlite.rs') diff --git a/src/repositories/sqlite.rs b/src/repositories/sqlite.rs new file mode 100644 index 0000000..79e6025 --- /dev/null +++ b/src/repositories/sqlite.rs @@ -0,0 +1,164 @@ +use super::*; +use crate::database::{Database, DbAccessToken, DbAuditLog, DbAuthCode, DbOAuthClient}; +use anyhow::Result; +use std::sync::{Arc, Mutex}; + +/// SQLite implementation of ClientRepository +pub struct SqliteClientRepository { + database: Arc>, +} + +impl SqliteClientRepository { + pub fn new(database: Arc>) -> Self { + Self { database } + } +} + +impl ClientRepository for SqliteClientRepository { + fn get_client(&self, client_id: &str) -> Result> { + let db = self.database.lock().unwrap(); + db.get_oauth_client(client_id) + } + + fn create_client(&self, client: &DbOAuthClient) -> Result<()> { + let db = self.database.lock().unwrap(); + db.create_oauth_client(client).map(|_| ()) + } + + fn update_client(&self, client: &DbOAuthClient) -> Result<()> { + let db = self.database.lock().unwrap(); + db.update_oauth_client(client) + } + + fn delete_client(&self, client_id: &str) -> Result<()> { + let db = self.database.lock().unwrap(); + db.delete_oauth_client(client_id) + } + + fn list_clients(&self) -> Result> { + let db = self.database.lock().unwrap(); + db.list_oauth_clients() + } +} + +/// SQLite implementation of AuthCodeRepository +pub struct SqliteAuthCodeRepository { + database: Arc>, +} + +impl SqliteAuthCodeRepository { + pub fn new(database: Arc>) -> Self { + Self { database } + } +} + +impl AuthCodeRepository for SqliteAuthCodeRepository { + fn create_auth_code(&self, code: &DbAuthCode) -> Result<()> { + let db = self.database.lock().unwrap(); + db.create_auth_code(code).map(|_| ()) + } + + fn get_auth_code(&self, code: &str) -> Result> { + let db = self.database.lock().unwrap(); + db.get_auth_code(code) + } + + fn mark_auth_code_used(&self, code: &str) -> Result<()> { + let db = self.database.lock().unwrap(); + db.mark_auth_code_used(code) + } + + fn cleanup_expired_codes(&self) -> Result<()> { + let db = self.database.lock().unwrap(); + db.cleanup_expired_codes().map(|_| ()) + } +} + +/// SQLite implementation of TokenRepository +pub struct SqliteTokenRepository { + database: Arc>, +} + +impl SqliteTokenRepository { + pub fn new(database: Arc>) -> Self { + Self { database } + } +} + +impl TokenRepository for SqliteTokenRepository { + fn create_access_token(&self, token: &DbAccessToken) -> Result<()> { + let db = self.database.lock().unwrap(); + db.create_access_token(token).map(|_| ()) + } + + fn get_access_token(&self, token_hash: &str) -> Result> { + let db = self.database.lock().unwrap(); + db.get_access_token(token_hash) + } + + fn revoke_access_token(&self, token_hash: &str) -> Result<()> { + let db = self.database.lock().unwrap(); + db.revoke_access_token(token_hash) + } + + fn cleanup_expired_tokens(&self) -> Result<()> { + let db = self.database.lock().unwrap(); + db.cleanup_expired_tokens().map(|_| ()) + } +} + +/// SQLite implementation of AuditRepository +pub struct SqliteAuditRepository { + database: Arc>, +} + +impl SqliteAuditRepository { + pub fn new(database: Arc>) -> Self { + Self { database } + } +} + +impl AuditRepository for SqliteAuditRepository { + fn create_audit_log(&self, log: &DbAuditLog) -> Result<()> { + let db = self.database.lock().unwrap(); + db.create_audit_log(log).map(|_| ()) + } + + fn get_audit_logs(&self, limit: Option) -> Result> { + let db = self.database.lock().unwrap(); + db.get_audit_logs(limit.unwrap_or(100)) + } + + fn cleanup_old_audit_logs(&self, days: i32) -> Result<()> { + let db = self.database.lock().unwrap(); + db.cleanup_old_audit_logs(days).map(|_| ()) + } +} + +/// SQLite implementation of RateRepository +pub struct SqliteRateRepository { + database: Arc>, +} + +impl SqliteRateRepository { + pub fn new(database: Arc>) -> Self { + Self { database } + } +} + +impl RateRepository for SqliteRateRepository { + fn increment_rate_limit( + &self, + identifier: &str, + endpoint: &str, + window_size: i32, + ) -> Result { + let db = self.database.lock().unwrap(); + db.increment_rate_limit(identifier, endpoint, window_size) + } + + fn cleanup_old_rate_limits(&self) -> Result<()> { + let db = self.database.lock().unwrap(); + db.cleanup_old_rate_limits() + } +} -- cgit v1.2.3