use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct FileVersionProjection {
pub id: Uuid,
pub file_id: Uuid,
pub version_number: i32,
pub version_type: VersionType,
pub storage_key: String,
pub storage_backend: String,
pub name: String,
pub mime_type: String,
pub checksum_md5: Option<String>,
pub checksum_sha256: Option<String>,
pub created_by_id: Uuid,
pub change_summary: Option<String>,
pub is_current: bool,
pub restored_from_id: Option<Uuid>,
pub expires_at: Option<DateTime<Utc>>,
pub status: FileVersionStatus,
pub deleted_at: Option<DateTime<Utc>>,
pub size_bytes: i64,
pub metadata: serde_json::Value,
pub projection_version: i64,
pub last_event_sequence: i64,
pub projection_updated_at: DateTime<Utc>,
}
impl FileVersionProjection {
pub fn new(
id: Uuid,
file_id: Uuid,
version_number: i32,
version_type: VersionType,
storage_key: String,
storage_backend: String,
name: String,
mime_type: String,
checksum_md5: Option<String>,
checksum_sha256: Option<String>,
created_by_id: Uuid,
change_summary: Option<String>,
is_current: bool,
restored_from_id: Option<Uuid>,
expires_at: Option<DateTime<Utc>>,
status: FileVersionStatus,
deleted_at: Option<DateTime<Utc>>,
size_bytes: i64,
metadata: serde_json::Value
) -> Self {
Self {
id,
file_id,
version_number,
version_type,
storage_key,
storage_backend,
name,
mime_type,
checksum_md5,
checksum_sha256,
created_by_id,
change_summary,
is_current,
restored_from_id,
expires_at,
status,
deleted_at,
size_bytes,
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 FileVersionSummary {
pub id: Uuid,
pub name: String,
}