summaryrefslogblamecommitdiff
path: root/dichroism/src/image_repo.rs
blob: e0f420bb2e08fab7836fbde027da80cd26f109d3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
                                               










                                                                     


























                                                                                                   

















                                                                      
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(())
    }
}