From 3f78a8ef4733040a7908ad4696ec7fc063695c07 Mon Sep 17 00:00:00 2001 From: caipira113 Date: Mon, 30 Oct 2023 08:38:30 +0900 Subject: [PATCH] impl Notes --- src/database.rs | 43 ++++++++++++++++++++----------------------- src/struct.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/database.rs b/src/database.rs index 0c8fecc..944ff99 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,5 @@ use std::error::Error; -use chrono::{DateTime, Utc}; use tokio_postgres::{Client, NoTls}; use crate::config::config; @@ -60,34 +59,32 @@ pub async fn query_notes(db: &Client) -> Result, Box> { if config.option.idtype.is_none() { for row in rows { - let created_at: DateTime = row.get("createdAt"); - let notes = Notes { - id: row.get("id"), - created_at: created_at.timestamp() * 1000 + created_at.timestamp_subsec_millis() as i64, - user_id: row.get("userId"), - user_host: row.get("userHost"), - channel_id: row.get("channelId"), - cw: row.get("cw"), - text: row.get("text"), - tags: row.get("tags"), - }; + let notes = Notes::new( + row.get("id"), + row.get("createdAt"), + row.get("userId"), + row.get("userHost"), + row.get("channelId"), + row.get("cw"), + row.get("text"), + row.get("tags"), + ); data_vec.push(notes); } } else if let Some(idtype) = config.option.idtype.as_ref() { if idtype == "aid" || idtype == "aidx" { for row in rows { - let created_at: DateTime = aid_series::parse(row.get("id")); - let notes = Notes { - id: row.get("id"), - created_at: created_at.timestamp() * 1000 + created_at.timestamp_subsec_millis() as i64, - user_id: row.get("userId"), - user_host: row.get("userHost"), - channel_id: row.get("channelId"), - cw: row.get("cw"), - text: row.get("text"), - tags: row.get("tags"), - }; + let notes = Notes::new( + row.get("id"), + aid_series::parse(row.get("id")), + row.get("userId"), + row.get("userHost"), + row.get("channelId"), + row.get("cw"), + row.get("text"), + row.get("tags"), + ); data_vec.push(notes); } diff --git a/src/struct.rs b/src/struct.rs index c5244a4..87addf9 100644 --- a/src/struct.rs +++ b/src/struct.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize, Serialize)] @@ -12,3 +13,28 @@ pub struct Notes { pub text: String, pub tags: Vec, } + +impl Notes { + pub fn new( + id: String, + created_at: DateTime, + user_id: String, + user_host: Option, + channel_id: Option, + cw: Option, + text: String, + tags: Vec, + ) -> Self { + let created_at = created_at.timestamp() * 1000 + created_at.timestamp_subsec_millis() as i64; + Self { + id, + created_at, + user_id, + user_host, + channel_id, + cw, + text, + tags, + } + } +} \ No newline at end of file