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
use sqlite::Connection;

use crate::{config::DEFAULT_PATH, provider::Provider};

pub struct Sqlite {
    connection: Connection,
}

impl Sqlite {
    pub fn new() -> anyhow::Result<Self> {
        let path = DEFAULT_PATH.join("avatars.sqlite");
        let connection = sqlite::open(path)?;

        // Create the table if it doesn't exist
        let query = "CREATE TABLE avatars (id TEXT PRIMARY KEY)";
        let _ = connection.execute(query);

        Ok(Self { connection })
    }
}

impl Provider for Sqlite {
    fn send_avatar_id(&self, avatar_id: &str) -> anyhow::Result<bool> {
        let query = "INSERT INTO avatars VALUES (?)";
        let mut statement = self.connection.prepare(query)?;
        statement.bind((1, avatar_id))?;

        Ok(statement.next().is_ok())
    }
}