Jacob Schmidt 53bc8db7d0 Handle bank account sync in UI bridge
- Route bank sync payloads through the client bridge
- Refresh account state without rebuilding the full session
- Split CAD dispatcher UI into modular source files
2026-04-02 09:10:12 -05:00

135 lines
4.3 KiB
Rust

//! Locker repository implementation for item data persistence operations.
//!
//! This module provides the data access layer for locker management.
//! Each player's locker is stored as a single JSON string containing all their items.
use forge_models::{Item, Locker};
use forge_shared::RedisClient;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
/// Repository trait defining the contract for locker data operations.
pub trait LockerRepository: Send + Sync {
/// Creates a new locker for a player
fn create(&self, uid: &str, locker: &Locker) -> Result<(), String>;
/// Updates an existing locker with new item data
fn update(&self, uid: &str, locker: &Locker) -> Result<(), String>;
/// Retrieves a player's locker
fn get(&self, uid: &str) -> Result<Option<Locker>, String>;
/// Deletes a player's locker (all items)
fn delete(&self, uid: &str) -> Result<(), String>;
/// Checks if a player has a locker
fn exists(&self, uid: &str) -> Result<bool, String>;
}
pub trait LockerHotRepository: Send + Sync {
fn get(&self, uid: &str) -> Result<Option<Locker>, String>;
fn save(&self, locker: &Locker, uid: &str) -> Result<(), String>;
fn delete(&self, uid: &str) -> Result<(), String>;
}
#[derive(Clone, Debug, Default)]
pub struct InMemoryLockerHotRepository {
state: Arc<RwLock<HashMap<String, Locker>>>,
}
impl InMemoryLockerHotRepository {
pub fn new() -> Self {
Self::default()
}
}
impl LockerHotRepository for InMemoryLockerHotRepository {
fn get(&self, uid: &str) -> Result<Option<Locker>, String> {
self.state
.read()
.map(|state| state.get(uid).cloned())
.map_err(|_| "Locker hot state lock poisoned.".to_string())
}
fn save(&self, locker: &Locker, uid: &str) -> Result<(), String> {
self.state
.write()
.map_err(|_| "Locker hot state lock poisoned.".to_string())?
.insert(uid.to_string(), locker.clone());
Ok(())
}
fn delete(&self, uid: &str) -> Result<(), String> {
self.state
.write()
.map_err(|_| "Locker hot state lock poisoned.".to_string())?
.remove(uid);
Ok(())
}
}
/// Redis-based implementation of the LockerRepository trait.
///
/// Stores each player's locker as a single JSON string array with the key format `locker:{uid}`.
pub struct RedisLockerRepository<C: RedisClient> {
client: C,
}
impl<C: RedisClient> RedisLockerRepository<C> {
pub fn new(client: C) -> Self {
Self { client }
}
}
impl<C: RedisClient> LockerRepository for RedisLockerRepository<C> {
fn create(&self, uid: &str, locker: &Locker) -> Result<(), String> {
let redis_key = format!("locker:{}", uid);
// Serialize just the items array
let locker_json = serde_json::to_string(&locker.items)
.map_err(|e| format!("Failed to serialize locker: {}", e))?;
// Store as a simple string value
self.client.set_key(redis_key, locker_json)
}
fn update(&self, uid: &str, locker: &Locker) -> Result<(), String> {
let redis_key = format!("locker:{}", uid);
// Serialize just the items array
let locker_json = serde_json::to_string(&locker.items)
.map_err(|e| format!("Failed to serialize locker: {}", e))?;
// Update the existing locker
self.client.set_key(redis_key, locker_json)
}
fn get(&self, uid: &str) -> Result<Option<Locker>, String> {
let redis_key = format!("locker:{}", uid);
// Get the JSON string from Redis
let locker_string = self.client.get_key(redis_key)?;
// Return None if no data found
if locker_string.is_empty() {
return Ok(None);
}
// Deserialize the items data
match serde_json::from_str::<HashMap<String, Item>>(&locker_string) {
Ok(items) => Ok(Some(Locker { items })),
Err(e) => Err(format!("Failed to deserialize locker: {}", e)),
}
}
fn delete(&self, uid: &str) -> Result<(), String> {
let redis_key = format!("locker:{}", uid);
self.client.delete_key(redis_key)
}
fn exists(&self, uid: &str) -> Result<bool, String> {
let redis_key = format!("locker:{}", uid);
self.client.key_exists(redis_key)
}
}