diff options
-rw-r--r-- | service-person/src/model/address.rs | 24 |
1 files 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<Self> { - 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<Country> { + schema::countries::table + .filter(schema::countries::id.eq(self.country_id)) + .first::<Country>(&db.get()?) + .map_err(Error::from) + } + + pub fn city(&self, db: &DbPool) -> Result<City> { + schema::cities::table + .filter(schema::cities::id.eq(self.city_id)) + .first::<City>(&db.get()?) + .map_err(Error::from) + } + + pub fn street(&self, db: &DbPool) -> Result<Street> { + schema::streets::table + .filter(schema::streets::id.eq(self.street_id)) + .first::<Street>(&db.get()?) + .map_err(Error::from) + } } |