projectlib/jsonb/
comment.rs
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
}
}