adds kordophone-db
This commit is contained in:
113
kordophone-db/src/models/conversation.rs
Normal file
113
kordophone-db/src/models/conversation.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use microrm::prelude::*;
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use time::OffsetDateTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::date::Date;
|
||||
|
||||
#[derive(Entity, Clone)]
|
||||
pub struct Conversation {
|
||||
#[unique]
|
||||
pub guid: String,
|
||||
|
||||
pub unread_count: i64,
|
||||
pub display_name: Option<String>,
|
||||
pub last_message_preview: Option<String>,
|
||||
pub date: OffsetDateTime,
|
||||
pub participant_display_names: microrm::RelationMap<Participant>,
|
||||
}
|
||||
|
||||
#[derive(Entity, Clone)]
|
||||
pub struct Participant {
|
||||
#[unique]
|
||||
pub display_name: String
|
||||
}
|
||||
|
||||
impl Into<Participant> for String {
|
||||
fn into(self) -> Participant {
|
||||
Participant { display_name: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl Conversation {
|
||||
pub fn builder() -> ConversationBuilder {
|
||||
ConversationBuilder::new()
|
||||
}
|
||||
|
||||
pub fn into_builder(&self) -> ConversationBuilder {
|
||||
ConversationBuilder {
|
||||
guid: Some(self.guid.clone()),
|
||||
date: Date::new(self.date),
|
||||
participant_display_names: None,
|
||||
unread_count: Some(self.unread_count),
|
||||
last_message_preview: self.last_message_preview.clone(),
|
||||
display_name: self.display_name.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ConversationBuilder {
|
||||
guid: Option<String>,
|
||||
date: Date,
|
||||
unread_count: Option<i64>,
|
||||
last_message_preview: Option<String>,
|
||||
participant_display_names: Option<Vec<String>>,
|
||||
display_name: Option<String>,
|
||||
}
|
||||
|
||||
impl ConversationBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn guid(mut self, guid: &str) -> Self {
|
||||
self.guid = Some(guid.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn date(mut self, date: Date) -> Self {
|
||||
self.date = date;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn unread_count(mut self, unread_count: i64) -> Self {
|
||||
self.unread_count = Some(unread_count);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn last_message_preview(mut self, last_message_preview: &str) -> Self {
|
||||
self.last_message_preview = Some(last_message_preview.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn participant_display_names(mut self, participant_display_names: Vec<String>) -> Self {
|
||||
self.participant_display_names = Some(participant_display_names);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn display_name(mut self, display_name: &str) -> Self {
|
||||
self.display_name = Some(display_name.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Conversation {
|
||||
let result = Conversation {
|
||||
guid: self.guid.unwrap_or(Uuid::new_v4().to_string()),
|
||||
unread_count: self.unread_count.unwrap_or(0),
|
||||
last_message_preview: self.last_message_preview,
|
||||
display_name: self.display_name,
|
||||
date: self.date.dt,
|
||||
participant_display_names: Default::default(),
|
||||
};
|
||||
|
||||
// TODO: this isn't right... this is crashing the test.
|
||||
if let Some(participants) = self.participant_display_names {
|
||||
for participant in participants {
|
||||
result.participant_display_names.insert(participant.into()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
18
kordophone-db/src/models/date.rs
Normal file
18
kordophone-db/src/models/date.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
pub struct Date {
|
||||
pub dt: OffsetDateTime,
|
||||
}
|
||||
|
||||
impl Date {
|
||||
pub fn new(dt: OffsetDateTime) -> Self {
|
||||
Self { dt }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Date {
|
||||
fn default() -> Self {
|
||||
Self { dt: OffsetDateTime::now_utc() }
|
||||
}
|
||||
}
|
||||
2
kordophone-db/src/models/mod.rs
Normal file
2
kordophone-db/src/models/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod conversation;
|
||||
pub mod date;
|
||||
Reference in New Issue
Block a user