use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct FileShareProjection {
pub id: Uuid,
pub file_id: Uuid,
pub owner_id: Uuid,
pub token: String,
pub share_type: ShareType,
pub permission: SharePermission,
pub shared_with: Vec<Uuid>,
pub password_hash: Option<String>,
pub max_downloads: Option<i32>,
pub download_count: i32,
pub expires_at: Option<DateTime<Utc>>,
pub status: ShareStatus,
pub revoked_at: Option<DateTime<Utc>>,
pub revoked_by: Option<Uuid>,
pub message: Option<String>,
pub metadata: serde_json::Value,
pub projection_version: i64,
pub last_event_sequence: i64,
pub projection_updated_at: DateTime<Utc>,
}
impl FileShareProjection {
pub fn new(
id: Uuid,
file_id: Uuid,
owner_id: Uuid,
token: String,
share_type: ShareType,
permission: SharePermission,
shared_with: Vec<Uuid>,
password_hash: Option<String>,
max_downloads: Option<i32>,
download_count: i32,
expires_at: Option<DateTime<Utc>>,
status: ShareStatus,
revoked_at: Option<DateTime<Utc>>,
revoked_by: Option<Uuid>,
message: Option<String>,
metadata: serde_json::Value
) -> Self {
Self {
id,
file_id,
owner_id,
token,
share_type,
permission,
shared_with,
password_hash,
max_downloads,
download_count,
expires_at,
status,
revoked_at,
revoked_by,
message,
metadata,
projection_version: 1,
last_event_sequence: 0,
projection_updated_at: Utc::now(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
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();
}
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct FileShareSummary {
pub id: Uuid,
}