Skip to content

Commit

Permalink
impl Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
caipira113 committed Oct 29, 2023
1 parent 81aa452 commit 3f78a8e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::error::Error;

use chrono::{DateTime, Utc};
use tokio_postgres::{Client, NoTls};

use crate::config::config;
Expand Down Expand Up @@ -60,34 +59,32 @@ pub async fn query_notes(db: &Client) -> Result<Vec<Notes>, Box<dyn Error>> {

if config.option.idtype.is_none() {
for row in rows {
let created_at: DateTime<Utc> = 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<Utc> = 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);
}
Expand Down
26 changes: 26 additions & 0 deletions src/struct.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -12,3 +13,28 @@ pub struct Notes {
pub text: String,
pub tags: Vec<String>,
}

impl Notes {
pub fn new(
id: String,
created_at: DateTime<Utc>,
user_id: String,
user_host: Option<String>,
channel_id: Option<String>,
cw: Option<String>,
text: String,
tags: Vec<String>,
) -> 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,
}
}
}

0 comments on commit 3f78a8e

Please sign in to comment.