feat: add image models
This commit is contained in:
parent
3568365c6f
commit
c10b9baa95
7 changed files with 95 additions and 2 deletions
33
src/db/models/image.rs
Normal file
33
src/db/models/image.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use diesel::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use crate::db::{schema::*, DbPool, DbResult};
|
||||
|
||||
#[derive(Serialize, Queryable, Selectable)]
|
||||
#[diesel(table_name = images)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Image {
|
||||
id: i32,
|
||||
plant_id: i32,
|
||||
date_taken: NaiveDate,
|
||||
note: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Insertable)]
|
||||
#[diesel(table_name = images)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct NewImage {
|
||||
plant_id: i32,
|
||||
date_taken: NaiveDate,
|
||||
note: Option<String>,
|
||||
}
|
||||
|
||||
impl NewImage {
|
||||
pub fn insert(self, pool: &DbPool) -> DbResult<Image> {
|
||||
Ok(self
|
||||
.insert_into(images::table)
|
||||
.returning(Image::as_returning())
|
||||
.get_result(&mut pool.get()?)?)
|
||||
}
|
||||
}
|
||||
|
|
@ -2,3 +2,4 @@ pub mod event;
|
|||
pub mod plant;
|
||||
pub mod session;
|
||||
pub mod user;
|
||||
pub mod image;
|
||||
|
|
|
|||
|
|
@ -10,6 +10,15 @@ diesel::table! {
|
|||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
images (id) {
|
||||
id -> Integer,
|
||||
plant_id -> Integer,
|
||||
date_taken -> Date,
|
||||
note -> Nullable<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
plants (id) {
|
||||
id -> Integer,
|
||||
|
|
@ -36,6 +45,13 @@ diesel::table! {
|
|||
}
|
||||
|
||||
diesel::joinable!(events -> plants (plant_id));
|
||||
diesel::joinable!(images -> plants (plant_id));
|
||||
diesel::joinable!(sessions -> users (user_id));
|
||||
|
||||
diesel::allow_tables_to_appear_in_same_query!(events, plants, sessions, users,);
|
||||
diesel::allow_tables_to_appear_in_same_query!(
|
||||
events,
|
||||
images,
|
||||
plants,
|
||||
sessions,
|
||||
users,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue