summaryrefslogtreecommitdiff
path: root/dichroism/src/photo_repo.rs
blob: 64390c853d83017d0b92a3838aee8fb38b9ab4b7 (plain) (blame)
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
37
38
39
40
41
use super::models::*;
use diesel::prelude::*;
use diesel::result::Error;

type DBConn = SqliteConnection;

pub fn read_photos(conn: &DBConn) -> Result<Vec<Photo>, Error> {
    use crate::schema::photos::dsl::*;
    let results = photos.load::<Photo>(conn)?;
    Ok(results)
}

fn read_photos_by_path(conn: &DBConn, path: &str) -> Result<Vec<Photo>, Error> {
    use crate::schema::photos::dsl::*;
    let results = photos.filter(path.eq(path)).load::<Photo>(conn)?;
    Ok(results)
}

pub fn read_photo_by_path(conn: &DBConn, path: &str) -> Result<Option<Photo>, Error> {
    use crate::schema::photos::dsl::*;
    let results = photos.filter(path.eq(path)).limit(1).load::<Photo>(conn)?;
    Ok(results.first().cloned())
}

pub fn read_photo_by_id(conn: &DBConn, id: i32) -> Result<Option<Photo>, Error> {
    use crate::schema::photos::dsl::*;
    let results = photos.filter(id.eq(id)).limit(1).load::<Photo>(conn)?;
    Ok(results.first().cloned())
}

pub fn create_photo(conn: &DBConn, new_photo: NewPhoto) -> Result<Option<Photo>, Error> {
    use super::schema::photos;
    diesel::insert_into(photos::table)
        .values(&new_photo)
        .execute(conn)?;
    read_photo_by_path(conn, &new_photo.path)
}

pub fn update_photo() {
    todo!()
}