From 37ba57833b789573b9ae473ab281fa97022838de Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 24 Oct 2021 12:05:54 +0200 Subject: model: Address: Add getters Signed-off-by: Matthias Beyer --- service-person/src/model/address.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/service-person/src/model/address.rs b/service-person/src/model/address.rs index 707545f..dcce3ce 100644 --- a/service-person/src/model/address.rs +++ b/service-person/src/model/address.rs @@ -12,6 +12,7 @@ use crate::model::City; use crate::model::Country; use crate::model::Street; use crate::schema::address; +use crate::schema; #[derive(Debug, Deserialize, diesel::Associations, diesel::Queryable, getset::CopyGetters)] #[belongs_to(Country)] @@ -40,8 +41,6 @@ struct NewAddress { impl Address { pub fn create_or_fetch(db: &DbPool, country: &Country, city: &City, street: &Street, number: i32) -> Result { - use crate::schema; - let conn = db.get()?; conn.transaction::<_, Error, _>(|| { @@ -68,4 +67,25 @@ impl Address { .map_err(Error::from) }) } + + pub fn country(&self, db: &DbPool) -> Result { + schema::countries::table + .filter(schema::countries::id.eq(self.country_id)) + .first::(&db.get()?) + .map_err(Error::from) + } + + pub fn city(&self, db: &DbPool) -> Result { + schema::cities::table + .filter(schema::cities::id.eq(self.city_id)) + .first::(&db.get()?) + .map_err(Error::from) + } + + pub fn street(&self, db: &DbPool) -> Result { + schema::streets::table + .filter(schema::streets::id.eq(self.street_id)) + .first::(&db.get()?) + .map_err(Error::from) + } } -- cgit v1.2.3