forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_3_app_layers.patch
More file actions
569 lines (569 loc) · 17.2 KB
/
2_3_app_layers.patch
File metadata and controls
569 lines (569 loc) · 17.2 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
Index: src/main/java/ru/javawebinar/topjava/service/MealService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/service/MealService.java (date 1530739373367)
+++ src/main/java/ru/javawebinar/topjava/service/MealService.java (date 1530739373367)
@@ -0,0 +1,4 @@
+package ru.javawebinar.topjava.service;
+
+public interface MealService {
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/service/MealServiceImpl.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/service/MealServiceImpl.java (date 1530739373368)
+++ src/main/java/ru/javawebinar/topjava/service/MealServiceImpl.java (date 1530739373368)
@@ -0,0 +1,9 @@
+package ru.javawebinar.topjava.service;
+
+import ru.javawebinar.topjava.repository.MealRepository;
+
+public class MealServiceImpl implements MealService {
+
+ private MealRepository repository;
+
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/service/UserService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/service/UserService.java (date 1530739373370)
+++ src/main/java/ru/javawebinar/topjava/service/UserService.java (date 1530739373370)
@@ -0,0 +1,22 @@
+package ru.javawebinar.topjava.service;
+
+
+import ru.javawebinar.topjava.model.User;
+import ru.javawebinar.topjava.util.exception.NotFoundException;
+
+import java.util.List;
+
+public interface UserService {
+
+ User create(User user);
+
+ void delete(int id) throws NotFoundException;
+
+ User get(int id) throws NotFoundException;
+
+ User getByEmail(String email) throws NotFoundException;
+
+ void update(User user);
+
+ List<User> getAll();
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/web/meal/MealRestController.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/web/meal/MealRestController.java (date 1530739373423)
+++ src/main/java/ru/javawebinar/topjava/web/meal/MealRestController.java (date 1530739373423)
@@ -0,0 +1,8 @@
+package ru.javawebinar.topjava.web.meal;
+
+import ru.javawebinar.topjava.service.MealService;
+
+public class MealRestController {
+ private MealService service;
+
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/web/SecurityUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/web/SecurityUtil.java (date 1530741235278)
+++ src/main/java/ru/javawebinar/topjava/web/SecurityUtil.java (date 1530741235278)
@@ -0,0 +1,14 @@
+package ru.javawebinar.topjava.web;
+
+import static ru.javawebinar.topjava.util.MealsUtil.DEFAULT_CALORIES_PER_DAY;
+
+public class SecurityUtil {
+
+ public static int authUserId() {
+ return 1;
+ }
+
+ public static int authUserCaloriesPerDay() {
+ return DEFAULT_CALORIES_PER_DAY;
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/web/user/AdminRestController.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/web/user/AdminRestController.java (date 1530739482294)
+++ src/main/java/ru/javawebinar/topjava/web/user/AdminRestController.java (date 1530739482294)
@@ -0,0 +1,38 @@
+package ru.javawebinar.topjava.web.user;
+
+import ru.javawebinar.topjava.model.User;
+
+import java.util.List;
+
+public class AdminRestController extends AbstractUserController {
+
+ @Override
+ public List<User> getAll() {
+ return super.getAll();
+ }
+
+ @Override
+ public User get(int id) {
+ return super.get(id);
+ }
+
+ @Override
+ public User create(User user) {
+ return super.create(user);
+ }
+
+ @Override
+ public void delete(int id) {
+ super.delete(id);
+ }
+
+ @Override
+ public void update(User user, int id) {
+ super.update(user, id);
+ }
+
+ @Override
+ public User getByMail(String email) {
+ return super.getByMail(email);
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/web/user/ProfileRestController.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/web/user/ProfileRestController.java (date 1530741235263)
+++ src/main/java/ru/javawebinar/topjava/web/user/ProfileRestController.java (date 1530741235263)
@@ -0,0 +1,20 @@
+package ru.javawebinar.topjava.web.user;
+
+import ru.javawebinar.topjava.model.User;
+
+import static ru.javawebinar.topjava.web.SecurityUtil.authUserId;
+
+public class ProfileRestController extends AbstractUserController {
+
+ public User get() {
+ return super.get(authUserId());
+ }
+
+ public void delete() {
+ super.delete(authUserId());
+ }
+
+ public void update(User user) {
+ super.update(user, authUserId());
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/web/user/AbstractUserController.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/web/user/AbstractUserController.java (date 1530740790618)
+++ src/main/java/ru/javawebinar/topjava/web/user/AbstractUserController.java (date 1530740790618)
@@ -0,0 +1,49 @@
+package ru.javawebinar.topjava.web.user;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import ru.javawebinar.topjava.model.User;
+import ru.javawebinar.topjava.service.UserService;
+
+import java.util.List;
+
+import static ru.javawebinar.topjava.util.ValidationUtil.assureIdConsistent;
+import static ru.javawebinar.topjava.util.ValidationUtil.checkNew;
+
+public abstract class AbstractUserController {
+ protected final Logger log = LoggerFactory.getLogger(getClass());
+
+ private UserService service;
+
+ public List<User> getAll() {
+ log.info("getAll");
+ return service.getAll();
+ }
+
+ public User get(int id) {
+ log.info("get {}", id);
+ return service.get(id);
+ }
+
+ public User create(User user) {
+ log.info("create {}", user);
+ checkNew(user);
+ return service.create(user);
+ }
+
+ public void delete(int id) {
+ log.info("delete {}", id);
+ service.delete(id);
+ }
+
+ public void update(User user, int id) {
+ log.info("update {} with id={}", user, id);
+ assureIdConsistent(user, id);
+ service.update(user);
+ }
+
+ public User getByMail(String email) {
+ log.info("getByEmail {}", email);
+ return service.getByEmail(email);
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/model/Role.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/model/Role.java (date 1530739373358)
+++ src/main/java/ru/javawebinar/topjava/model/Role.java (date 1530739373358)
@@ -0,0 +1,6 @@
+package ru.javawebinar.topjava.model;
+
+public enum Role {
+ ROLE_USER,
+ ROLE_ADMIN
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/model/User.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/model/User.java (date 1530740790635)
+++ src/main/java/ru/javawebinar/topjava/model/User.java (date 1530740790635)
@@ -0,0 +1,91 @@
+package ru.javawebinar.topjava.model;
+
+import java.util.Date;
+import java.util.EnumSet;
+import java.util.Set;
+
+import static ru.javawebinar.topjava.util.MealsUtil.DEFAULT_CALORIES_PER_DAY;
+
+public class User extends AbstractNamedEntity {
+
+ private String email;
+
+ private String password;
+
+ private boolean enabled = true;
+
+ private Date registered = new Date();
+
+ private Set<Role> roles;
+
+ private int caloriesPerDay = DEFAULT_CALORIES_PER_DAY;
+
+ public User(Integer id, String name, String email, String password, Role role, Role... roles) {
+ this(id, name, email, password, DEFAULT_CALORIES_PER_DAY, true, EnumSet.of(role, roles));
+ }
+
+ public User(Integer id, String name, String email, String password, int caloriesPerDay, boolean enabled, Set<Role> roles) {
+ super(id, name);
+ this.email = email;
+ this.password = password;
+ this.caloriesPerDay = caloriesPerDay;
+ this.enabled = enabled;
+ this.roles = roles;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public Date getRegistered() {
+ return registered;
+ }
+
+ public void setRegistered(Date registered) {
+ this.registered = registered;
+ }
+
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ }
+
+ public int getCaloriesPerDay() {
+ return caloriesPerDay;
+ }
+
+ public void setCaloriesPerDay(int caloriesPerDay) {
+ this.caloriesPerDay = caloriesPerDay;
+ }
+
+ public boolean isEnabled() {
+ return enabled;
+ }
+
+ public Set<Role> getRoles() {
+ return roles;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public String toString() {
+ return "User (" +
+ "id=" + id +
+ ", email=" + email +
+ ", name=" + name +
+ ", enabled=" + enabled +
+ ", roles=" + roles +
+ ", caloriesPerDay=" + caloriesPerDay +
+ ')';
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java (date 1530739373354)
+++ src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java (date 1530739373354)
@@ -0,0 +1,24 @@
+package ru.javawebinar.topjava.model;
+
+public abstract class AbstractNamedEntity extends AbstractBaseEntity {
+
+ protected String name;
+
+ protected AbstractNamedEntity(Integer id, String name) {
+ super(id);
+ this.name = name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("Entity %s (%s, '%s')", getClass().getName(), id, name);
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/repository/UserRepository.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/repository/UserRepository.java (date 1530739373365)
+++ src/main/java/ru/javawebinar/topjava/repository/UserRepository.java (date 1530739373365)
@@ -0,0 +1,20 @@
+package ru.javawebinar.topjava.repository;
+
+import ru.javawebinar.topjava.model.User;
+
+import java.util.List;
+
+public interface UserRepository {
+ User save(User user);
+
+ // false if not found
+ boolean delete(int id);
+
+ // null if not found
+ User get(int id);
+
+ // null if not found
+ User getByEmail(String email);
+
+ List<User> getAll();
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/service/UserServiceImpl.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/service/UserServiceImpl.java (date 1530739482288)
+++ src/main/java/ru/javawebinar/topjava/service/UserServiceImpl.java (date 1530739482288)
@@ -0,0 +1,45 @@
+package ru.javawebinar.topjava.service;
+
+import ru.javawebinar.topjava.model.User;
+import ru.javawebinar.topjava.repository.UserRepository;
+import ru.javawebinar.topjava.util.exception.NotFoundException;
+
+import java.util.List;
+
+import static ru.javawebinar.topjava.util.ValidationUtil.checkNotFound;
+import static ru.javawebinar.topjava.util.ValidationUtil.checkNotFoundWithId;
+
+public class UserServiceImpl implements UserService {
+
+ private UserRepository repository;
+
+ @Override
+ public User create(User user) {
+ return repository.save(user);
+ }
+
+ @Override
+ public void delete(int id) throws NotFoundException {
+ checkNotFoundWithId(repository.delete(id), id);
+ }
+
+ @Override
+ public User get(int id) throws NotFoundException {
+ return checkNotFoundWithId(repository.get(id), id);
+ }
+
+ @Override
+ public User getByEmail(String email) throws NotFoundException {
+ return checkNotFound(repository.getByEmail(email), "email=" + email);
+ }
+
+ @Override
+ public List<User> getAll() {
+ return repository.getAll();
+ }
+
+ @Override
+ public void update(User user) {
+ checkNotFoundWithId(repository.save(user), user.getId());
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/util/exception/NotFoundException.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/util/exception/NotFoundException.java (date 1530739373421)
+++ src/main/java/ru/javawebinar/topjava/util/exception/NotFoundException.java (date 1530739373421)
@@ -0,0 +1,7 @@
+package ru.javawebinar.topjava.util.exception;
+
+public class NotFoundException extends RuntimeException {
+ public NotFoundException(String message) {
+ super(message);
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/util/ValidationUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/util/ValidationUtil.java (date 1530740790656)
+++ src/main/java/ru/javawebinar/topjava/util/ValidationUtil.java (date 1530740790656)
@@ -0,0 +1,42 @@
+package ru.javawebinar.topjava.util;
+
+
+import ru.javawebinar.topjava.model.AbstractBaseEntity;
+import ru.javawebinar.topjava.util.exception.NotFoundException;
+
+public class ValidationUtil {
+
+ public static <T> T checkNotFoundWithId(T object, int id) {
+ return checkNotFound(object, "id=" + id);
+ }
+
+ public static void checkNotFoundWithId(boolean found, int id) {
+ checkNotFound(found, "id=" + id);
+ }
+
+ public static <T> T checkNotFound(T object, String msg) {
+ checkNotFound(object != null, msg);
+ return object;
+ }
+
+ public static void checkNotFound(boolean found, String msg) {
+ if (!found) {
+ throw new NotFoundException("Not found entity with " + msg);
+ }
+ }
+
+ public static void checkNew(AbstractBaseEntity entity) {
+ if (!entity.isNew()) {
+ throw new IllegalArgumentException(entity + " must be new (id=null)");
+ }
+ }
+
+ public static void assureIdConsistent(AbstractBaseEntity entity, int id) {
+// http://stackoverflow.com/a/32728226/548473
+ if (entity.isNew()) {
+ entity.setId(id);
+ } else if (entity.getId() != id) {
+ throw new IllegalArgumentException(entity + " must be with id=" + id);
+ }
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java (date 1530739373352)
+++ src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java (date 1530739373352)
@@ -0,0 +1,26 @@
+package ru.javawebinar.topjava.model;
+
+public abstract class AbstractBaseEntity {
+ protected Integer id;
+
+ protected AbstractBaseEntity(Integer id) {
+ this.id = id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public boolean isNew() {
+ return this.id == null;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("Entity %s (%s)", getClass().getName(), id);
+ }
+}
\ No newline at end of file