Skip to content

Commit f270b79

Browse files
committed
Refactor storage interfaces
Ref. #75
1 parent 2ab2c05 commit f270b79

File tree

5 files changed

+121
-49
lines changed

5 files changed

+121
-49
lines changed

crates/domain/src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,51 @@ use thiserror::Error;
1717
use uuid::Uuid;
1818

1919
#[allow(async_fn_in_trait)]
20-
pub trait Repository {
20+
pub trait SessionRepository {
2121
async fn request_session(&self, user_id: UserID) -> Result<User, String>;
2222
async fn initialize_session(&self) -> Result<User, String>;
2323
async fn delete_session(&self) -> Result<(), String>;
24+
}
2425

26+
#[allow(async_fn_in_trait)]
27+
pub trait VersionRepository {
2528
async fn read_version(&self) -> Result<String, String>;
29+
}
2630

31+
#[allow(async_fn_in_trait)]
32+
pub trait UserRepository {
2733
async fn read_users(&self) -> Result<Vec<User>, String>;
2834
async fn create_user(&self, name: Name, sex: Sex) -> Result<User, String>;
2935
async fn replace_user(&self, user: User) -> Result<User, String>;
3036
async fn delete_user(&self, id: UserID) -> Result<UserID, String>;
37+
}
3138

39+
#[allow(async_fn_in_trait)]
40+
pub trait BodyWeightRepository {
3241
async fn read_body_weight(&self) -> Result<Vec<BodyWeight>, String>;
3342
async fn create_body_weight(&self, body_weight: BodyWeight) -> Result<BodyWeight, String>;
3443
async fn replace_body_weight(&self, body_weight: BodyWeight) -> Result<BodyWeight, String>;
3544
async fn delete_body_weight(&self, date: NaiveDate) -> Result<NaiveDate, String>;
45+
}
3646

47+
#[allow(async_fn_in_trait)]
48+
pub trait BodyFatRepository {
3749
async fn read_body_fat(&self) -> Result<Vec<BodyFat>, String>;
3850
async fn create_body_fat(&self, body_fat: BodyFat) -> Result<BodyFat, String>;
3951
async fn replace_body_fat(&self, body_fat: BodyFat) -> Result<BodyFat, String>;
4052
async fn delete_body_fat(&self, date: NaiveDate) -> Result<NaiveDate, String>;
53+
}
4154

55+
#[allow(async_fn_in_trait)]
56+
pub trait PeriodRepository {
4257
async fn read_period(&self) -> Result<Vec<Period>, String>;
4358
async fn create_period(&self, period: Period) -> Result<Period, String>;
4459
async fn replace_period(&self, period: Period) -> Result<Period, String>;
4560
async fn delete_period(&self, date: NaiveDate) -> Result<NaiveDate, String>;
61+
}
4662

63+
#[allow(async_fn_in_trait)]
64+
pub trait ExerciseRepository {
4765
async fn read_exercises(&self) -> Result<Vec<Exercise>, String>;
4866
async fn create_exercise(
4967
&self,
@@ -52,7 +70,10 @@ pub trait Repository {
5270
) -> Result<Exercise, String>;
5371
async fn replace_exercise(&self, exercise: Exercise) -> Result<Exercise, String>;
5472
async fn delete_exercise(&self, id: ExerciseID) -> Result<ExerciseID, String>;
73+
}
5574

75+
#[allow(async_fn_in_trait)]
76+
pub trait RoutineRepository {
5677
async fn read_routines(&self) -> Result<Vec<Routine>, String>;
5778
async fn create_routine(
5879
&self,
@@ -67,7 +88,10 @@ pub trait Repository {
6788
sections: Option<Vec<RoutinePart>>,
6889
) -> Result<Routine, String>;
6990
async fn delete_routine(&self, id: RoutineID) -> Result<RoutineID, String>;
91+
}
7092

93+
#[allow(async_fn_in_trait)]
94+
pub trait TrainingSessionRepository {
7195
async fn read_training_sessions(&self) -> Result<Vec<TrainingSession>, String>;
7296
async fn create_training_session(
7397
&self,

crates/storage/src/local_storage.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use std::collections::VecDeque;
22

33
use gloo_storage::Storage as GlooStorage;
4-
use valens_web_app::{OngoingTrainingSession, Repository, Settings, log};
4+
use valens_web_app::{
5+
OngoingTrainingSession, OngoingTrainingSessionRepository, Settings, SettingsRepository, log,
6+
};
57

68
#[derive(Clone)]
7-
pub struct UI;
9+
pub struct LocalStorage;
810

911
const KEY_SETTINGS: &str = "settings";
1012
const KEY_ONGOING_TRAINING_SESSION: &str = "ongoing training session";
1113

12-
impl Repository for UI {
14+
impl SettingsRepository for LocalStorage {
1315
async fn read_settings(&self) -> Result<Settings, String> {
1416
match gloo_storage::LocalStorage::get(KEY_SETTINGS) {
1517
Ok(entries) => Ok(entries),
@@ -24,7 +26,9 @@ impl Repository for UI {
2426
async fn write_settings(&self, settings: Settings) -> Result<(), String> {
2527
gloo_storage::LocalStorage::set(KEY_SETTINGS, settings).map_err(|err| err.to_string())
2628
}
29+
}
2730

31+
impl OngoingTrainingSessionRepository for LocalStorage {
2832
async fn read_ongoing_training_session(
2933
&self,
3034
) -> Result<Option<OngoingTrainingSession>, String> {

crates/storage/src/rest.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use thiserror::Error;
55
use valens_domain as domain;
66

77
#[derive(Clone)]
8-
pub struct Storage;
8+
pub struct REST;
99

10-
impl domain::Repository for Storage {
10+
impl domain::SessionRepository for REST {
1111
async fn request_session(&self, user_id: domain::UserID) -> Result<domain::User, String> {
1212
let r: User = fetch(
1313
Request::post("api/session")
@@ -28,11 +28,15 @@ impl domain::Repository for Storage {
2828
async fn delete_session(&self) -> Result<(), String> {
2929
fetch_no_content(Request::delete("api/session").build().unwrap(), ()).await
3030
}
31+
}
3132

33+
impl domain::VersionRepository for REST {
3234
async fn read_version(&self) -> Result<String, String> {
3335
fetch(Request::get("api/version").build().unwrap()).await
3436
}
37+
}
3538

39+
impl domain::UserRepository for REST {
3640
async fn read_users(&self) -> Result<Vec<domain::User>, String> {
3741
let r: Vec<User> = fetch(Request::get("api/users").build().unwrap()).await?;
3842
r.into_iter()
@@ -41,6 +45,7 @@ impl domain::Repository for Storage {
4145
})
4246
.collect::<Result<Vec<domain::User>, String>>()
4347
}
48+
4449
async fn create_user(
4550
&self,
4651
name: domain::Name,
@@ -58,6 +63,7 @@ impl domain::Repository for Storage {
5863
r.try_into()
5964
.map_err(|err: domain::NameError| err.to_string())
6065
}
66+
6167
async fn replace_user(&self, user: domain::User) -> Result<domain::User, String> {
6268
let r: User = fetch(
6369
Request::put(&format!("api/users/{}", user.id.as_u128()))
@@ -68,6 +74,7 @@ impl domain::Repository for Storage {
6874
r.try_into()
6975
.map_err(|err: domain::NameError| err.to_string())
7076
}
77+
7178
async fn delete_user(&self, id: domain::UserID) -> Result<domain::UserID, String> {
7279
fetch_no_content(
7380
Request::delete(&format!("api/users/{}", id.as_u128()))
@@ -77,11 +84,14 @@ impl domain::Repository for Storage {
7784
)
7885
.await
7986
}
87+
}
8088

89+
impl domain::BodyWeightRepository for REST {
8190
async fn read_body_weight(&self) -> Result<Vec<domain::BodyWeight>, String> {
8291
let r: Vec<BodyWeight> = fetch(Request::get("api/body_weight").build().unwrap()).await?;
8392
Ok(r.into_iter().map(domain::BodyWeight::from).collect())
8493
}
94+
8595
async fn create_body_weight(
8696
&self,
8797
body_weight: domain::BodyWeight,
@@ -94,6 +104,7 @@ impl domain::Repository for Storage {
94104
.await?;
95105
Ok(r.into())
96106
}
107+
97108
async fn replace_body_weight(
98109
&self,
99110
body_weight: domain::BodyWeight,
@@ -106,6 +117,7 @@ impl domain::Repository for Storage {
106117
.await?;
107118
Ok(r.into())
108119
}
120+
109121
async fn delete_body_weight(&self, date: NaiveDate) -> Result<NaiveDate, String> {
110122
fetch_no_content(
111123
Request::delete(&format!("api/body_weight/{date}"))
@@ -115,11 +127,14 @@ impl domain::Repository for Storage {
115127
)
116128
.await
117129
}
130+
}
118131

132+
impl domain::BodyFatRepository for REST {
119133
async fn read_body_fat(&self) -> Result<Vec<domain::BodyFat>, String> {
120134
let r: Vec<BodyFat> = fetch(Request::get("api/body_fat").build().unwrap()).await?;
121135
Ok(r.into_iter().map(domain::BodyFat::from).collect())
122136
}
137+
123138
async fn create_body_fat(&self, body_fat: domain::BodyFat) -> Result<domain::BodyFat, String> {
124139
let r: BodyFat = fetch(
125140
Request::post("api/body_fat")
@@ -129,6 +144,7 @@ impl domain::Repository for Storage {
129144
.await?;
130145
Ok(r.into())
131146
}
147+
132148
async fn replace_body_fat(&self, body_fat: domain::BodyFat) -> Result<domain::BodyFat, String> {
133149
let r: BodyFat = fetch(
134150
Request::put(&format!("api/body_fat/{}", body_fat.date))
@@ -138,6 +154,7 @@ impl domain::Repository for Storage {
138154
.await?;
139155
Ok(r.into())
140156
}
157+
141158
async fn delete_body_fat(&self, date: NaiveDate) -> Result<NaiveDate, String> {
142159
fetch_no_content(
143160
Request::delete(&format!("api/body_fat/{date}"))
@@ -147,7 +164,9 @@ impl domain::Repository for Storage {
147164
)
148165
.await
149166
}
167+
}
150168

169+
impl domain::PeriodRepository for REST {
151170
async fn read_period(&self) -> Result<Vec<domain::Period>, String> {
152171
let r: Vec<Period> = fetch(Request::get("api/period").build().unwrap()).await?;
153172
r.into_iter()
@@ -156,6 +175,7 @@ impl domain::Repository for Storage {
156175
})
157176
.collect::<Result<Vec<domain::Period>, String>>()
158177
}
178+
159179
async fn create_period(&self, period: domain::Period) -> Result<domain::Period, String> {
160180
let r: Period = fetch(
161181
Request::post("api/period")
@@ -166,6 +186,7 @@ impl domain::Repository for Storage {
166186
r.try_into()
167187
.map_err(|err: domain::IntensityError| err.to_string())
168188
}
189+
169190
async fn replace_period(&self, period: domain::Period) -> Result<domain::Period, String> {
170191
let r: Period = fetch(
171192
Request::put(&format!("api/period/{}", period.date))
@@ -176,6 +197,7 @@ impl domain::Repository for Storage {
176197
r.try_into()
177198
.map_err(|err: domain::IntensityError| err.to_string())
178199
}
200+
179201
async fn delete_period(&self, date: NaiveDate) -> Result<NaiveDate, String> {
180202
fetch_no_content(
181203
Request::delete(&format!("api/period/{date}"))
@@ -185,7 +207,9 @@ impl domain::Repository for Storage {
185207
)
186208
.await
187209
}
210+
}
188211

212+
impl domain::ExerciseRepository for REST {
189213
async fn read_exercises(&self) -> Result<Vec<domain::Exercise>, String> {
190214
let r: Vec<Exercise> = fetch(Request::get("api/exercises").build().unwrap()).await?;
191215
r.into_iter()
@@ -194,6 +218,7 @@ impl domain::Repository for Storage {
194218
})
195219
.collect::<Result<Vec<domain::Exercise>, String>>()
196220
}
221+
197222
async fn create_exercise(
198223
&self,
199224
name: domain::Name,
@@ -210,6 +235,7 @@ impl domain::Repository for Storage {
210235
.await?;
211236
r.try_into().map_err(|err: ExerciseError| err.to_string())
212237
}
238+
213239
async fn replace_exercise(
214240
&self,
215241
exercise: domain::Exercise,
@@ -222,6 +248,7 @@ impl domain::Repository for Storage {
222248
.await?;
223249
r.try_into().map_err(|err: ExerciseError| err.to_string())
224250
}
251+
225252
async fn delete_exercise(&self, id: domain::ExerciseID) -> Result<domain::ExerciseID, String> {
226253
fetch_no_content(
227254
Request::delete(&format!("api/exercises/{}", id.as_u128()))
@@ -231,7 +258,9 @@ impl domain::Repository for Storage {
231258
)
232259
.await
233260
}
261+
}
234262

263+
impl domain::RoutineRepository for REST {
235264
async fn read_routines(&self) -> Result<Vec<domain::Routine>, String> {
236265
let r: Vec<Routine> = fetch(Request::get("api/routines").build().unwrap()).await?;
237266
r.into_iter()
@@ -240,6 +269,7 @@ impl domain::Repository for Storage {
240269
})
241270
.collect::<Result<Vec<domain::Routine>, String>>()
242271
}
272+
243273
async fn create_routine(
244274
&self,
245275
name: domain::Name,
@@ -259,6 +289,7 @@ impl domain::Repository for Storage {
259289
r.try_into()
260290
.map_err(|err: domain::NameError| err.to_string())
261291
}
292+
262293
async fn modify_routine(
263294
&self,
264295
id: domain::RoutineID,
@@ -293,6 +324,7 @@ impl domain::Repository for Storage {
293324
r.try_into()
294325
.map_err(|err: domain::NameError| err.to_string())
295326
}
327+
296328
async fn delete_routine(&self, id: domain::RoutineID) -> Result<domain::RoutineID, String> {
297329
fetch_no_content(
298330
Request::delete(&format!("api/routines/{}", id.as_u128()))
@@ -302,11 +334,14 @@ impl domain::Repository for Storage {
302334
)
303335
.await
304336
}
337+
}
305338

339+
impl domain::TrainingSessionRepository for REST {
306340
async fn read_training_sessions(&self) -> Result<Vec<domain::TrainingSession>, String> {
307341
let r: Vec<TrainingSession> = fetch(Request::get("api/workouts").build().unwrap()).await?;
308342
Ok(r.into_iter().map(domain::TrainingSession::from).collect())
309343
}
344+
310345
async fn create_training_session(
311346
&self,
312347
routine_id: domain::RoutineID,
@@ -334,6 +369,7 @@ impl domain::Repository for Storage {
334369
.await?;
335370
Ok(r.into())
336371
}
372+
337373
async fn modify_training_session(
338374
&self,
339375
id: domain::TrainingSessionID,
@@ -363,6 +399,7 @@ impl domain::Repository for Storage {
363399
.await?;
364400
Ok(r.into())
365401
}
402+
366403
async fn delete_training_session(
367404
&self,
368405
id: domain::TrainingSessionID,

0 commit comments

Comments
 (0)