comment_app_backend 0.1.0

Serves comments through Restful APIs
Documentation
// reply.rs
// replay specific functions are implemented here

use serde_derive::{Serialize, Deserialize};
use chrono::prelude::*;

use crate::utils;
use crate::models::status::Status;


#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Reply {

    pub message: String,

    #[serde(default = "default_anonym")]
    pub anonymous: bool,

    #[serde(default = "Status::pending")]
    pub status: Status,

    #[serde(default = "default_remark")]
    pub remarks: String,

    #[serde(default = "String::new")]
    pub unique_id: String, 

    #[serde(default = "String::new")]
    pub parent_id: String,

    #[serde(default = "String::new")]
    pub comment_id: String,

    pub level: u32, // hierarchy level; first reply is at level 0, reply of reply is 1, so on

    #[serde(default = "String::new")]
    pub user_id: String,

    #[serde(default = "String::new")]
    pub created_on: String,

    #[serde(default = "String::new")]
    pub updated_on: String,
    
    #[serde(default = "String::new")]
    pub deleted_on: String,
}

fn default_anonym() -> bool {
    false
}

fn default_remark() -> String {
    "New Reply".to_string()
}

impl PartialEq for Reply {
    fn eq(&self, other: &Self) -> bool {
        (self.message == other.message) &&
        (self.status == other.status) &&
        (self.parent_id == other.parent_id) &&
        (self.comment_id == other.comment_id) &&
        (self.level == other.level) &&
        (self.user_id == other.user_id)
    }
}

impl Eq for Reply {}   // no methods implemented


pub fn assign_uid(reply: &mut Reply) {
    reply.unique_id = utils::unique_id();
}
pub fn assign_created_date(reply: &mut Reply) {
    reply.created_on = Local::now().to_string();
}
pub fn assign_updated_date(reply: &mut Reply) {
    reply.updated_on = Local::now().to_string();
}
pub fn assign_deleted_date(reply: &mut Reply) {
    reply.deleted_on = Local::now().to_string();
}