summaryrefslogtreecommitdiff
path: root/dichroism/src/image_repo.rs
blob: e0f420bb2e08fab7836fbde027da80cd26f109d3 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use super::models::{NewProductImg, ProductImg};
use diesel::prelude::*;
use diesel::result::Error;

type DBConn = SqliteConnection;

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

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

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

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

pub fn create_image(conn: &DBConn, new_image: NewProductImg) -> Result<Option<ProductImg>, Error> {
    use super::schema::images;
    diesel::insert_into(images::table)
        .values(&new_image)
        .execute(conn)?;
    read_image_by_path(conn, &new_image.path)
}

pub fn update_image() {
    todo!()
}

pub fn delete_image() {
    todo!()
}

#[cfg(test)]
mod tests {

    #[test]
    fn test() -> std::result::Result<(), Box<dyn std::error::Error>> {
        Ok(())
    }
}