summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-10-24 12:05:54 +0200
committerMatthias Beyer <mail@beyermatthias.de>2021-10-24 12:05:54 +0200
commit37ba57833b789573b9ae473ab281fa97022838de (patch)
treeab41970a1bcac9d2b2900d3b782ed7d6688ea12a
parentda8ebeea878bfd46b07e7027e7315a114289782e (diff)
model: Address: Add getters
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--service-person/src/model/address.rs24
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)
+ }
}