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() } }