0.5.0
This commit is contained in:
57
kb_lib/src/db/dtos/db_metadata.rs
Normal file
57
kb_lib/src/db/dtos/db_metadata.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
// file: kb_lib/src/db/dtos/db_metadata.rs
|
||||
|
||||
//! Metadata DTOs.
|
||||
|
||||
/// Metadata DTO used by the application layer.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbDbMetadataDto {
|
||||
/// Metadata key.
|
||||
pub key: std::string::String,
|
||||
/// Metadata value.
|
||||
pub value: std::string::String,
|
||||
/// Last update timestamp.
|
||||
pub updated_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl KbDbMetadataDto {
|
||||
/// Creates a new metadata DTO with the current UTC timestamp.
|
||||
pub fn new(key: std::string::String, value: std::string::String) -> Self {
|
||||
Self {
|
||||
key,
|
||||
value,
|
||||
updated_at: chrono::Utc::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<crate::KbDbMetadataEntity> for KbDbMetadataDto {
|
||||
type Error = crate::KbError;
|
||||
|
||||
fn try_from(entity: crate::KbDbMetadataEntity) -> Result<Self, Self::Error> {
|
||||
let parsed_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
|
||||
let parsed = match parsed_result {
|
||||
Ok(parsed) => parsed,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot parse db metadata timestamp '{}': {}",
|
||||
entity.updated_at, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
Ok(Self {
|
||||
key: entity.key,
|
||||
value: entity.value,
|
||||
updated_at: parsed.with_timezone(&chrono::Utc),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KbDbMetadataDto> for crate::KbDbMetadataEntity {
|
||||
fn from(dto: KbDbMetadataDto) -> Self {
|
||||
Self {
|
||||
key: dto.key,
|
||||
value: dto.value,
|
||||
updated_at: dto.updated_at.to_rfc3339(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user