58 lines
1.4 KiB
Rust
58 lines
1.4 KiB
Rust
use diesel::prelude::*;
|
|
|
|
use crate::schema::documents::dsl;
|
|
use uuid::Uuid;
|
|
|
|
use super::DbError;
|
|
|
|
#[derive(Queryable, Selectable, Debug, Clone)]
|
|
#[diesel(table_name = crate::schema::documents)]
|
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
|
pub struct Document {
|
|
pub id: String,
|
|
pub creator_id: String,
|
|
pub project_id: String,
|
|
pub title: String,
|
|
pub content: String,
|
|
pub created: chrono::NaiveDateTime,
|
|
pub updated: chrono::NaiveDateTime,
|
|
}
|
|
|
|
#[derive(Insertable)]
|
|
#[diesel(table_name = crate::schema::documents)]
|
|
pub struct NewDocument {
|
|
pub id: String,
|
|
pub creator_id: String,
|
|
pub project_id: String,
|
|
pub title: String,
|
|
pub content: String,
|
|
}
|
|
|
|
impl NewDocument {
|
|
pub fn new(creator_id: Uuid, project_id: Uuid, title: String, content: String) -> Self {
|
|
Self {
|
|
id: Uuid::new_v4().to_string(),
|
|
creator_id: creator_id.to_string(),
|
|
project_id: project_id.to_string(),
|
|
title,
|
|
content,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Query<'a> {
|
|
db: &'a mut SqliteConnection,
|
|
}
|
|
|
|
impl<'a> Query<'a> {
|
|
pub fn new(db: &'a mut SqliteConnection) -> Self {
|
|
Self { db }
|
|
}
|
|
|
|
pub fn for_user(&mut self, user_id: String) -> Result<Vec<Document>, DbError> {
|
|
let documents = dsl::documents
|
|
.filter(dsl::creator_id.eq(user_id.to_string()))
|
|
.load::<Document>(self.db)?;
|
|
Ok(documents)
|
|
}
|
|
}
|