2018-02-14 23:53:11 +00:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-02-10 00:00:55 +00:00
|
|
|
use serde_json::Value as JsonValue;
|
|
|
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
use super::User;
|
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
2018-02-10 00:00:55 +00:00
|
|
|
#[table_name = "folders"]
|
2018-02-14 23:40:34 +00:00
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
2018-02-10 00:00:55 +00:00
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct Folder {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Folder {
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn new(user_uuid: String, name: String) -> Self {
|
2018-02-10 00:00:55 +00:00
|
|
|
let now = Utc::now().naive_utc();
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
Self {
|
2018-02-10 00:00:55 +00:00
|
|
|
uuid: Uuid::new_v4().to_string(),
|
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_json(&self) -> JsonValue {
|
|
|
|
use util::format_date;
|
|
|
|
|
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
|
|
|
"RevisionDate": format_date(&self.updated_at),
|
|
|
|
"Name": self.name,
|
|
|
|
"Object": "folder",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use db::DbConn;
|
|
|
|
use db::schema::folders;
|
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl Folder {
|
2018-02-15 00:07:57 +00:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> bool {
|
|
|
|
self.updated_at = Utc::now().naive_utc();
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
match diesel::replace_into(folders::table)
|
2018-02-15 00:07:57 +00:00
|
|
|
.values(&*self)
|
2018-02-10 00:00:55 +00:00
|
|
|
.execute(&**conn) {
|
|
|
|
Ok(1) => true, // One row inserted
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(self, conn: &DbConn) -> bool {
|
|
|
|
match diesel::delete(folders::table.filter(
|
|
|
|
folders::uuid.eq(self.uuid)))
|
|
|
|
.execute(&**conn) {
|
|
|
|
Ok(1) => true, // One row deleted
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
2018-02-10 00:00:55 +00:00
|
|
|
folders::table
|
|
|
|
.filter(folders::uuid.eq(uuid))
|
2018-02-14 23:40:34 +00:00
|
|
|
.first::<Self>(&**conn).ok()
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-02-10 00:00:55 +00:00
|
|
|
folders::table
|
|
|
|
.filter(folders::user_uuid.eq(user_uuid))
|
2018-02-14 23:40:34 +00:00
|
|
|
.load::<Self>(&**conn).expect("Error loading folders")
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
}
|