reorg: separate db::records from insertable models
This commit is contained in:
@@ -1,49 +1,6 @@
|
||||
use diesel::prelude::*;
|
||||
use chrono::NaiveDateTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::{
|
||||
participant::Participant,
|
||||
};
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)]
|
||||
#[diesel(table_name = crate::schema::conversations)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct DbConversation {
|
||||
pub id: String,
|
||||
pub unread_count: i64,
|
||||
pub display_name: Option<String>,
|
||||
pub last_message_preview: Option<String>,
|
||||
pub date: NaiveDateTime,
|
||||
}
|
||||
|
||||
impl From<Conversation> for DbConversation {
|
||||
fn from(conversation: Conversation) -> Self {
|
||||
Self {
|
||||
id: conversation.guid,
|
||||
unread_count: conversation.unread_count as i64,
|
||||
display_name: conversation.display_name,
|
||||
last_message_preview: conversation.last_message_preview,
|
||||
date: conversation.date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Conversation> for (DbConversation, Vec<Participant>) {
|
||||
fn from(conversation: Conversation) -> Self {
|
||||
(
|
||||
DbConversation {
|
||||
id: conversation.guid,
|
||||
unread_count: conversation.unread_count as i64,
|
||||
display_name: conversation.display_name,
|
||||
last_message_preview: conversation.last_message_preview,
|
||||
date: conversation.date,
|
||||
},
|
||||
|
||||
conversation.participants
|
||||
)
|
||||
}
|
||||
}
|
||||
use crate::models::participant::Participant;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Conversation {
|
||||
@@ -72,19 +29,6 @@ impl Conversation {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DbConversation> for Conversation {
|
||||
fn from(db_conversation: DbConversation) -> Self {
|
||||
Self {
|
||||
guid: db_conversation.id,
|
||||
unread_count: db_conversation.unread_count as u16,
|
||||
display_name: db_conversation.display_name,
|
||||
last_message_preview: db_conversation.last_message_preview,
|
||||
date: db_conversation.date,
|
||||
participants: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ConversationBuilder {
|
||||
guid: Option<String>,
|
||||
|
||||
52
kordophone-db/src/models/db/conversation.rs
Normal file
52
kordophone-db/src/models/db/conversation.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use diesel::prelude::*;
|
||||
use chrono::NaiveDateTime;
|
||||
use crate::models::{Conversation, Participant};
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)]
|
||||
#[diesel(table_name = crate::schema::conversations)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Record {
|
||||
pub id: String,
|
||||
pub unread_count: i64,
|
||||
pub display_name: Option<String>,
|
||||
pub last_message_preview: Option<String>,
|
||||
pub date: NaiveDateTime,
|
||||
}
|
||||
|
||||
impl From<Conversation> for Record {
|
||||
fn from(conversation: Conversation) -> Self {
|
||||
Self {
|
||||
id: conversation.guid,
|
||||
unread_count: conversation.unread_count as i64,
|
||||
display_name: conversation.display_name,
|
||||
last_message_preview: conversation.last_message_preview,
|
||||
date: conversation.date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This implementation returns the insertable data types for the conversation and participants
|
||||
impl From<Conversation> for (Record, Vec<Participant>) {
|
||||
fn from(conversation: Conversation) -> Self {
|
||||
(
|
||||
Record::from(conversation.clone()),
|
||||
|
||||
// Keep in mind, db::participant::Record is the selectable data type for the
|
||||
// participants table, whereas Participant is the insertable model type.
|
||||
conversation.participants
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Record> for Conversation {
|
||||
fn from(record: Record) -> Self {
|
||||
Self {
|
||||
guid: record.id,
|
||||
unread_count: record.unread_count as u16,
|
||||
display_name: record.display_name,
|
||||
last_message_preview: record.last_message_preview,
|
||||
date: record.date,
|
||||
participants: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
2
kordophone-db/src/models/db/mod.rs
Normal file
2
kordophone-db/src/models/db/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod conversation;
|
||||
pub mod participant;
|
||||
37
kordophone-db/src/models/db/participant.rs
Normal file
37
kordophone-db/src/models/db/participant.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use diesel::prelude::*;
|
||||
use crate::models::Participant;
|
||||
use crate::schema::conversation_participants;
|
||||
|
||||
#[derive(Queryable, Selectable, AsChangeset, Clone, PartialEq, Debug, Identifiable)]
|
||||
#[diesel(table_name = crate::schema::participants)]
|
||||
pub struct Record {
|
||||
pub id: i32,
|
||||
pub display_name: String
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Selectable, Queryable, Associations, Debug)]
|
||||
#[diesel(belongs_to(super::conversation::Record, foreign_key = conversation_id))]
|
||||
#[diesel(belongs_to(Record, foreign_key = participant_id))]
|
||||
#[diesel(table_name = conversation_participants)]
|
||||
#[diesel(primary_key(conversation_id, participant_id))]
|
||||
pub struct ConversationParticipant {
|
||||
pub conversation_id: String,
|
||||
pub participant_id: i32,
|
||||
}
|
||||
|
||||
impl From<Record> for Participant {
|
||||
fn from(record: Record) -> Self {
|
||||
Participant {
|
||||
display_name: record.display_name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Participant> for Record {
|
||||
fn from(participant: Participant) -> Self {
|
||||
Record {
|
||||
id: 0, // This will be set by the database
|
||||
display_name: participant.display_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
pub mod conversation;
|
||||
pub mod participant;
|
||||
pub mod date;
|
||||
pub mod participant;
|
||||
pub mod db;
|
||||
|
||||
// Re-export the public types
|
||||
pub use conversation::Conversation;
|
||||
pub use participant::Participant;
|
||||
@@ -1,5 +1,4 @@
|
||||
use diesel::prelude::*;
|
||||
use crate::{models::conversation::DbConversation, schema::conversation_participants};
|
||||
use diesel::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Insertable)]
|
||||
#[diesel(table_name = crate::schema::participants)]
|
||||
@@ -7,37 +6,14 @@ pub struct Participant {
|
||||
pub display_name: String,
|
||||
}
|
||||
|
||||
impl From<DbParticipant> for Participant {
|
||||
fn from(participant: DbParticipant) -> Self {
|
||||
Participant { display_name: participant.display_name }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, PartialEq, Debug, Identifiable)]
|
||||
#[diesel(table_name = crate::schema::participants)]
|
||||
pub struct DbParticipant {
|
||||
pub id: i32,
|
||||
pub display_name: String
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Selectable, Queryable, Associations, Debug)]
|
||||
#[diesel(belongs_to(DbConversation, foreign_key = conversation_id))]
|
||||
#[diesel(belongs_to(DbParticipant, foreign_key = participant_id))]
|
||||
#[diesel(table_name = conversation_participants)]
|
||||
#[diesel(primary_key(conversation_id, participant_id))]
|
||||
pub struct ConversationParticipant {
|
||||
pub conversation_id: String,
|
||||
pub participant_id: i32,
|
||||
}
|
||||
|
||||
impl Into<Participant> for String {
|
||||
fn into(self) -> Participant {
|
||||
Participant { display_name: self }
|
||||
impl From<String> for Participant {
|
||||
fn from(display_name: String) -> Self {
|
||||
Participant { display_name }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Participant {
|
||||
fn from(s: &str) -> Self {
|
||||
Participant { display_name: s.into() }
|
||||
fn from(display_name: &str) -> Self {
|
||||
Participant { display_name: display_name.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user