1use anyhow::Result;
2use chrono::Utc;
3use rusqlite::params;
4
5use super::{parse_datetime, Database, MAX_COMMENT_LEN};
6use crate::models::Comment;
7
8impl Database {
9 pub fn add_comment(&self, issue_id: i64, content: &str, kind: &str) -> Result<i64> {
10 if content.len() > MAX_COMMENT_LEN {
11 anyhow::bail!(
12 "Comment exceeds maximum length of {} bytes",
13 MAX_COMMENT_LEN
14 );
15 }
16 let now = Utc::now().to_rfc3339();
17 self.conn.execute(
18 "INSERT INTO comments (issue_id, content, created_at, kind) VALUES (?1, ?2, ?3, ?4)",
19 params![issue_id, content, now, kind],
20 )?;
21 Ok(self.conn.last_insert_rowid())
22 }
23
24 pub fn get_comments(&self, issue_id: i64) -> Result<Vec<Comment>> {
25 let mut stmt = self.conn.prepare(
26 "SELECT id, issue_id, content, created_at, kind FROM comments WHERE issue_id = ?1 ORDER BY created_at",
27 )?;
28 let comments = stmt
29 .query_map([issue_id], |row| {
30 Ok(Comment {
31 id: row.get(0)?,
32 issue_id: row.get(1)?,
33 content: row.get(2)?,
34 created_at: parse_datetime(row.get::<_, String>(3)?),
35 kind: row
36 .get::<_, Option<String>>(4)?
37 .unwrap_or_else(|| "note".to_string()),
38 })
39 })?
40 .collect::<std::result::Result<Vec<_>, _>>()?;
41 Ok(comments)
42 }
43}