Get rid of unnecessary New* structs.

This commit is contained in:
Rasmus Kaj 2018-02-19 23:02:31 +01:00
parent 6e49da943c
commit 240b3bada2
4 changed files with 25 additions and 120 deletions

View File

@ -64,13 +64,12 @@ fn save_photo(
}
} else {
info!("Position for {} is {} {}", file_path, lat, long);
use models::NewPosition;
insert_into(positions)
.values(&NewPosition {
photo_id: photo.id,
latitude: (lat * 1e6) as i32,
longitude: (long * 1e6) as i32,
})
.values((
photo_id.eq(photo.id),
latitude.eq((lat * 1e6) as i32),
longitude.eq((long * 1e6) as i32),
))
.execute(db)
.expect("Insert image position");
}

View File

@ -29,12 +29,8 @@ pub fn passwd(db: &PgConnection, uname: &str) -> Result<(), Error> {
println!("Updated password for {:?} to {:?}", uname, pword);
}
0 => {
use models::NewUser;
insert_into(users)
.values(&NewUser {
username: uname,
password: &hashword,
})
.values((username.eq(uname), password.eq(&hashword)))
.execute(db)?;
println!("Created user {:?} with password {:?}", uname, pword);
}

View File

@ -18,14 +18,6 @@ pub struct Photo {
}
use schema::photos;
#[derive(Debug, Clone, Insertable)]
#[table_name = "photos"]
pub struct NewPhoto<'a> {
pub path: &'a str,
pub date: Option<NaiveDateTime>,
pub rotation: i16,
pub camera_id: Option<i32>,
}
#[derive(Debug)]
pub enum Modification<T> {
@ -127,12 +119,12 @@ impl Photo {
Ok(result)
} else {
let pic = diesel::insert_into(photos)
.values(&NewPhoto {
path: file_path,
date: exifdate,
rotation: exifrotation,
camera_id: camera.map(|c| c.id),
})
.values((
path.eq(file_path),
date.eq(exifdate),
rotation.eq(exifrotation),
camera_id.eq(camera.map(|c| c.id)),
))
.get_result::<Photo>(db)?;
Ok(Modification::Created(pic))
}
@ -217,22 +209,6 @@ pub struct PhotoTag {
pub tag_id: i32,
}
use super::schema::tags;
#[derive(Debug, Clone, Insertable)]
#[table_name = "tags"]
pub struct NewTag<'a> {
pub tag_name: &'a str,
pub slug: &'a str,
}
use super::schema::photo_tags;
#[derive(Debug, Clone, Insertable)]
#[table_name = "photo_tags"]
pub struct NewPhotoTag {
pub photo_id: i32,
pub tag_id: i32,
}
#[derive(Debug, Clone, Queryable)]
pub struct Person {
pub id: i32,
@ -247,22 +223,6 @@ pub struct PhotoPerson {
pub person_id: i32,
}
use super::schema::people;
#[derive(Debug, Clone, Insertable)]
#[table_name = "people"]
pub struct NewPerson<'a> {
pub person_name: &'a str,
pub slug: &'a str,
}
use super::schema::photo_people;
#[derive(Debug, Clone, Insertable)]
#[table_name = "photo_people"]
pub struct NewPhotoPerson {
pub photo_id: i32,
pub person_id: i32,
}
#[derive(Debug, Clone, Queryable)]
pub struct Place {
pub id: i32,
@ -277,52 +237,13 @@ pub struct PhotoPlace {
pub place_id: i32,
}
use super::schema::places;
#[derive(Debug, Clone, Insertable)]
#[table_name = "places"]
pub struct NewPlace<'a> {
pub slug: &'a str,
pub place_name: &'a str,
}
use super::schema::photo_places;
#[derive(Debug, Clone, Insertable)]
#[table_name = "photo_places"]
pub struct NewPhotoPlace {
pub photo_id: i32,
pub place_id: i32,
}
use super::schema::positions;
#[derive(Debug, Clone, Insertable)]
#[table_name = "positions"]
pub struct NewPosition {
pub photo_id: i32,
pub latitude: i32,
pub longitude: i32,
}
use super::schema::users;
#[derive(Insertable)]
#[table_name = "users"]
pub struct NewUser<'a> {
pub username: &'a str,
pub password: &'a str,
}
use super::schema::cameras;
#[derive(Debug, Clone, Identifiable, Queryable)]
pub struct Camera {
pub id: i32,
pub manufacturer: String,
pub model: String,
}
use super::schema::cameras;
#[derive(Debug, Clone, Insertable)]
#[table_name = "cameras"]
pub struct NewCamera {
pub manufacturer: String,
pub model: String,
}
impl Camera {
pub fn get_or_create(
@ -341,11 +262,9 @@ impl Camera {
{
Ok(camera)
} else {
let camera = NewCamera {
manufacturer: make.to_string(),
model: modl.to_string(),
};
diesel::insert_into(cameras).values(&camera).get_result(db)
diesel::insert_into(cameras)
.values((manufacturer.eq(make), model.eq(modl)))
.get_result(db)
}
}
}

View File

@ -62,17 +62,14 @@ pub fn set_tag<'mw>(
if let (Some(image), Some(tag)) = try_with!(res, tag_params(req)) {
let c: &PgConnection = &req.db_conn();
use diesel;
use models::{NewPhotoTag, NewTag, PhotoTag, Tag};
use models::{PhotoTag, Tag};
let tag = {
use schema::tags::dsl::*;
tags.filter(tag_name.ilike(&tag))
.first::<Tag>(c)
.or_else(|_| {
diesel::insert_into(tags)
.values(&NewTag {
tag_name: &tag,
slug: &slugify(&tag),
})
.values((tag_name.eq(&tag), slug.eq(&slugify(&tag))))
.get_result::<Tag>(c)
})
.expect("Find or create tag")
@ -86,10 +83,7 @@ pub fn set_tag<'mw>(
} else {
info!("Add {:?} on photo #{}!", tag, image);
diesel::insert_into(photo_tags)
.values(&NewPhotoTag {
photo_id: image,
tag_id: tag.id,
})
.values((photo_id.eq(image), tag_id.eq(tag.id)))
.execute(c)
.expect("Tag a photo");
}
@ -117,7 +111,7 @@ pub fn set_person<'mw>(
if let (Some(image), Some(name)) = try_with!(res, person_params(req)) {
let c: &PgConnection = &req.db_conn();
use diesel;
use models::{NewPerson, NewPhotoPerson, Person, PhotoPerson};
use models::{Person, PhotoPerson};
let person = {
use schema::people::dsl::*;
people
@ -125,10 +119,10 @@ pub fn set_person<'mw>(
.first::<Person>(c)
.or_else(|_| {
diesel::insert_into(people)
.values(&NewPerson {
person_name: &name,
slug: &slugify(&name),
})
.values((
person_name.eq(&name),
slug.eq(&slugify(&name)),
))
.get_result::<Person>(c)
})
.expect("Find or create tag")
@ -142,10 +136,7 @@ pub fn set_person<'mw>(
} else {
info!("Add {:?} on photo #{}!", person, image);
diesel::insert_into(photo_people)
.values(&NewPhotoPerson {
photo_id: image,
person_id: person.id,
})
.values((photo_id.eq(image), person_id.eq(person.id)))
.execute(c)
.expect("Name person in photo");
}