summaryrefslogtreecommitdiff
path: root/src/index/app.rs
blob: 4f234ee6a0d2ab3f68518a341718d61cdd182a4f (plain)
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
31
32
33
34
35
36
use sqlx::sqlite::SqlitePool;

use crate::repo::{
    channel::{self, Channel, Provider as _},
    error::NotFound as _,
};

pub struct Index<'a> {
    db: &'a SqlitePool,
}

impl<'a> Index<'a> {
    pub const fn new(db: &'a SqlitePool) -> Self {
        Self { db }
    }

    pub async fn channel(&self, channel: &channel::Id) -> Result<Channel, Error> {
        let mut tx = self.db.begin().await?;
        let channel = tx
            .channels()
            .by_id(channel)
            .await
            .not_found(|| Error::ChannelNotFound(channel.clone()))?;
        tx.commit().await?;

        Ok(channel)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("channel {0} not found")]
    ChannelNotFound(channel::Id),
    #[error(transparent)]
    DatabaseError(#[from] sqlx::Error),
}