-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.rs
111 lines (102 loc) · 4.26 KB
/
repo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::{models::resource_data::Resource, schema::resources, PgPool};
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use petompp_web_models::{
error::{Error, ResourceDataValidationError, ValidationError},
models::country::Country,
};
use rocket::{async_trait, http::Status, outcome::Outcome, request::FromRequest, Request};
pub trait ResourcesRepo: Send + Sync {
fn get(&self, key: &str, lang: &Country) -> Result<(Country, String), Error>;
fn get_all(&self) -> Result<Vec<Resource>, Error>;
fn create(&self, data: &Resource) -> Result<Resource, Error>;
fn update(&self, data: &Resource) -> Result<Resource, Error>;
fn delete(&self, key: &str) -> Result<(), Error>;
fn delete_lang(&self, key: &str, lang: &Country) -> Result<(), Error>;
}
#[async_trait]
impl<'r> FromRequest<'r> for &'r dyn ResourcesRepo {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, (Status, Self::Error), ()> {
request
.guard::<&rocket::State<&dyn ResourcesRepo>>()
.await
.map(|pool| *pool.inner())
}
}
impl ResourcesRepo for PgPool {
fn get(&self, key: &str, lang: &Country) -> Result<(Country, String), Error> {
let mut conn = self.get()?;
let q = resources::dsl::resources.filter(resources::key.eq(key));
Ok(match lang {
Country::Poland => q
.select((resources::pl, resources::en))
.get_result::<(Option<String>, String)>(&mut conn)
.map(|(pl, en)| match pl {
Some(pl) => (Country::Poland, pl),
None => (Country::UnitedKingdom, en),
})?,
_ => q
.select(resources::en)
.get_result::<String>(&mut conn)
.map(|en| (Country::UnitedKingdom, en))?,
})
}
fn get_all(&self) -> Result<Vec<Resource>, Error> {
let mut conn = self.get()?;
let res = resources::dsl::resources.load::<Resource>(&mut conn)?;
Ok(res)
}
fn create(&self, data: &Resource) -> Result<Resource, Error> {
let mut conn = self.get()?;
let res = diesel::insert_into(resources::dsl::resources)
.values(data)
.get_result::<Resource>(&mut conn)?;
Ok(res)
}
fn update(&self, data: &Resource) -> Result<Resource, Error> {
let mut conn = self.get()?;
let key = data
.key
.clone()
.ok_or(Error::Validation(ValidationError::ResourceData(
ResourceDataValidationError::KeyMissing,
)))?;
let res = match (&data.en, &data.pl) {
(Some(en), Some(pl)) => diesel::update(resources::dsl::resources)
.filter(resources::dsl::key.eq(key))
.set((resources::dsl::en.eq(en), resources::dsl::pl.eq(pl)))
.get_result::<Resource>(&mut conn)?,
(Some(en), None) => diesel::update(resources::dsl::resources)
.filter(resources::dsl::key.eq(key))
.set(resources::dsl::en.eq(en))
.get_result::<Resource>(&mut conn)?,
(None, Some(pl)) => diesel::update(resources::dsl::resources)
.filter(resources::dsl::key.eq(key))
.set(resources::dsl::pl.eq(pl))
.get_result::<Resource>(&mut conn)?,
_ => {
return Err(Error::Validation(ValidationError::ResourceData(
ResourceDataValidationError::ValueMissing,
)))
}
};
Ok(res)
}
fn delete(&self, key: &str) -> Result<(), Error> {
let mut conn = self.get()?;
diesel::delete(resources::dsl::resources.filter(resources::dsl::key.eq(key)))
.execute(&mut conn)?;
Ok(())
}
fn delete_lang(&self, key: &str, lang: &Country) -> Result<(), Error> {
let mut conn = self.get()?;
match lang {
Country::UnitedKingdom => return Err(Error::Validation(ValidationError::Country)),
Country::Poland => diesel::update(resources::dsl::resources)
.filter(resources::dsl::key.eq(key))
.set(resources::dsl::pl.eq::<Option<String>>(None))
.execute(&mut conn)?,
};
Ok(())
}
}