projectlib/jsonb/
comment.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use chrono::{NaiveDateTime};
use common::utils::serialize_option_naive_date;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::dto::comment_request::CommentRequest;
use crate::dto::comment_response::CommentResponse;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Comment {
    pub id: Uuid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<Uuid>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(with = "serialize_option_naive_date")]
    pub due_date: Option<NaiveDateTime>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub replyto: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

impl Comment {
    pub fn from_request(comment: &CommentRequest) -> Comment {
        Comment {
            id: if comment.id.is_none() {
                Uuid::new_v4()
            } else {
                comment.id.unwrap()
            },
            task: comment.task.clone(),
            author: comment.author.clone(),
            due_date: comment.due_date.clone(),
            replyto: comment.replyto.clone(),
            text: comment.text.clone(),
        }
    }
    pub fn to_response(&self) -> CommentResponse {
        CommentResponse {
            id: self.id,
            task: self.task,
            author: self.author,
            due_date: self.due_date,
            replyto: self.replyto,
            text: self.text.clone(),
        }
    }

    pub fn to_response_list(comments: Vec<Comment>) -> Vec<CommentResponse> {
        let mut res: Vec<CommentResponse> = vec![];
        if comments.is_empty() {
            return res;
        }
        for comment in comments.iter() {
            res.push(Comment::to_response(&comment.clone()));
        }
        res
    }
}