2020-09-29 21:27:11 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2020-10-27 01:54:56 +00:00
|
|
|
use derive_builder::Builder;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-10-27 02:49:38 +00:00
|
|
|
use shrinkwraprs::Shrinkwrap;
|
2020-09-29 00:33:43 +00:00
|
|
|
use sqlx::{self, FromRow};
|
|
|
|
|
2020-10-27 02:49:38 +00:00
|
|
|
#[derive(Shrinkwrap, Copy, Clone)]
|
|
|
|
pub(crate) struct PageId(pub i64);
|
|
|
|
|
|
|
|
#[derive(Shrinkwrap, Copy, Clone)]
|
|
|
|
pub(crate) struct NoteId(pub i64);
|
|
|
|
|
|
|
|
#[derive(Shrinkwrap, Copy, Clone)]
|
|
|
|
pub(crate) struct ParentId(pub i64);
|
|
|
|
|
|
|
|
/// A RawPage is what this layer of the API returns when requesting a
|
|
|
|
/// page. Note that usually what you'll get back in the RawPage and a
|
|
|
|
/// Vec<RawNote>. It's the next level's responsibility to turn that
|
|
|
|
/// into a proper tree.
|
2020-10-27 01:54:56 +00:00
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub struct RawPage {
|
|
|
|
pub id: i64,
|
2020-10-06 15:01:25 +00:00
|
|
|
pub slug: String,
|
|
|
|
pub title: String,
|
2020-10-27 01:54:56 +00:00
|
|
|
pub note_id: i64,
|
|
|
|
pub creation_date: DateTime<Utc>,
|
|
|
|
pub updated_date: DateTime<Utc>,
|
|
|
|
pub lastview_date: DateTime<Utc>,
|
|
|
|
pub deleted_date: Option<DateTime<Utc>>,
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:49:38 +00:00
|
|
|
/// A RawNote is what this layer of the API returns
|
|
|
|
/// when requesting a note.
|
2020-10-27 01:54:56 +00:00
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub struct RawNote {
|
|
|
|
pub id: i64,
|
|
|
|
pub uuid: String,
|
|
|
|
pub parent_id: i64,
|
|
|
|
pub parent_uuid: String,
|
|
|
|
pub content: String,
|
|
|
|
pub position: i64,
|
|
|
|
pub notetype: String,
|
|
|
|
pub creation_date: DateTime<Utc>,
|
|
|
|
pub updated_date: DateTime<Utc>,
|
|
|
|
pub lastview_date: DateTime<Utc>,
|
|
|
|
pub deleted_date: Option<DateTime<Utc>>,
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:49:38 +00:00
|
|
|
/// The interface for passing a new page to the store.
|
2020-10-27 01:54:56 +00:00
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, Builder)]
|
|
|
|
pub struct NewPage {
|
|
|
|
pub slug: String,
|
|
|
|
pub title: String,
|
|
|
|
pub note_id: i64,
|
|
|
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
|
|
pub creation_date: DateTime<Utc>,
|
|
|
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
|
|
pub updated_date: DateTime<Utc>,
|
|
|
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
|
|
pub lastview_date: DateTime<Utc>,
|
|
|
|
#[builder(default = r#"None"#)]
|
|
|
|
pub deleted_date: Option<DateTime<Utc>>,
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:49:38 +00:00
|
|
|
/// The interface for passing a new note to the store.
|
2020-10-27 01:54:56 +00:00
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, Builder)]
|
|
|
|
pub struct NewNote {
|
|
|
|
#[builder(default = r#""".to_string()"#)]
|
|
|
|
pub uuid: String,
|
|
|
|
pub content: String,
|
|
|
|
#[builder(default = r#""note".to_string()"#)]
|
|
|
|
pub notetype: String,
|
|
|
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
|
|
pub creation_date: DateTime<Utc>,
|
|
|
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
|
|
pub updated_date: DateTime<Utc>,
|
|
|
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
|
|
pub lastview_date: DateTime<Utc>,
|
|
|
|
#[builder(default = r#"None"#)]
|
|
|
|
pub deleted_date: Option<DateTime<Utc>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub(crate) struct JustSlugs {
|
|
|
|
pub slug: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub(crate) struct JustTitles {
|
|
|
|
title: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub(crate) struct JustId {
|
|
|
|
pub id: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub(crate) struct PageTitles {
|
|
|
|
pub id: i64,
|
|
|
|
pub title: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub(crate) struct NoteRelationship {
|
|
|
|
pub parent_id: i64,
|
|
|
|
pub note_id: i64,
|
|
|
|
pub position: i64,
|
|
|
|
pub nature: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
|
|
pub(crate) struct RowCount {
|
|
|
|
pub count: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_build_new_note() {
|
|
|
|
let now = chrono::Utc::now();
|
|
|
|
let newnote = NewNoteBuilder::default()
|
|
|
|
.uuid("foo".to_string())
|
|
|
|
.content("bar".to_string())
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
assert!((newnote.creation_date - now).num_minutes() < 1);
|
|
|
|
assert!((newnote.updated_date - now).num_minutes() < 1);
|
|
|
|
assert!((newnote.lastview_date - now).num_minutes() < 1);
|
|
|
|
assert!(newnote.deleted_date.is_none());
|
|
|
|
}
|
2020-10-06 15:01:25 +00:00
|
|
|
}
|