comment_app_backend 0.1.0

Serves comments through Restful APIs
Documentation
// comment.rs
// comment 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 Comment {

    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 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 Comment".to_string()
}

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

impl Eq for Comment {}   // no methods implemented

pub fn assign_uid(comment: &mut Comment) {
    comment.unique_id = utils::unique_id();
}
pub fn assign_created_date(comment: &mut Comment) {
    comment.created_on = Local::now().to_string();
}
pub fn assign_updated_date(comment: &mut Comment) {
    comment.updated_on = Local::now().to_string();
}
pub fn assign_deleted_date(comment: &mut Comment) {
    comment.deleted_on = Local::now().to_string();
}
pub fn timestamp_now() -> String {
    Local::now().to_string()
}