backbone-bucket 0.2.1

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! AccessLog Projection (Read Model)
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! This is a CQRS projection - an optimized read model for queries.
//! It is updated asynchronously from domain events.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;

// ============================================================================
// PROJECTION (READ MODEL)
// ============================================================================

/// AccessLog read model optimized for queries
///
/// This projection is denormalized for fast reads.
/// It is updated when domain events are published.
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AccessLogProjection {
    pub id: Uuid,
    pub file_id: Uuid,
    pub bucket_id: Option<Uuid>,
    pub action: AccessAction,
    pub user_id: Option<Uuid>,
    pub share_id: Option<Uuid>,
    pub is_owner: bool,
    pub is_shared: bool,
    pub is_public: bool,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub referer: Option<String>,
    pub country_code: Option<String>,
    pub city: Option<String>,
    pub bytes_transferred: Option<i64>,
    pub duration_ms: Option<i32>,
    pub success: bool,
    pub error_message: Option<String>,
    pub accessed_at: DateTime<Utc>,
    pub metadata: serde_json::Value,

    // Projection metadata
    /// Version of the projection (for optimistic locking)
    pub projection_version: i64,
    /// Last event sequence number applied
    pub last_event_sequence: i64,
    /// When this projection was last updated
    pub projection_updated_at: DateTime<Utc>,
}

impl AccessLogProjection {
    /// Create a new projection from entity data
    pub fn new(
        id: Uuid,
        file_id: Uuid,
        bucket_id: Option<Uuid>,
        action: AccessAction,
        user_id: Option<Uuid>,
        share_id: Option<Uuid>,
        is_owner: bool,
        is_shared: bool,
        is_public: bool,
        ip_address: Option<String>,
        user_agent: Option<String>,
        referer: Option<String>,
        country_code: Option<String>,
        city: Option<String>,
        bytes_transferred: Option<i64>,
        duration_ms: Option<i32>,
        success: bool,
        error_message: Option<String>,
        accessed_at: DateTime<Utc>,
        metadata: serde_json::Value
    ) -> Self {
        Self {
            id,
            file_id,
            bucket_id,
            action,
            user_id,
            share_id,
            is_owner,
            is_shared,
            is_public,
            ip_address,
            user_agent,
            referer,
            country_code,
            city,
            bytes_transferred,
            duration_ms,
            success,
            error_message,
            accessed_at,
            metadata,
            projection_version: 1,
            last_event_sequence: 0,
            projection_updated_at: Utc::now(),
        }
    }

    /// Get the projection ID
    pub fn id(&self) -> &Uuid {
        &self.id
    }

    /// Update projection version after applying event
    pub fn apply_event(&mut self, event_sequence: i64) {
        self.projection_version += 1;
        self.last_event_sequence = event_sequence;
        self.projection_updated_at = Utc::now();
    }
}

// ============================================================================
// SUMMARY PROJECTION (LIST VIEW)
// ============================================================================

/// Summary view of AccessLog for list displays
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AccessLogSummary {
    pub id: Uuid,
}

// ============================================================================
// CUSTOM PROJECTIONS
// ============================================================================
// <<< CUSTOM PROJECTIONS START >>>

// <<< CUSTOM PROJECTIONS START >>>
// Add custom read models here for specific query use cases
// Example:
//
// #[derive(Debug, Clone, Serialize, Deserialize)]
// pub struct AccessLogDashboard {
//     pub id: Uuid,
//     pub key_metric: i64,
// }
// <<< CUSTOM PROJECTIONS END >>>