Skip to content

Commit 55d753b

Browse files
committed
2 3 app layers
1 parent b7c3f43 commit 55d753b

File tree

12 files changed

+294
-0
lines changed

12 files changed

+294
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.javawebinar.topjava;
2+
3+
import ru.javawebinar.topjava.util.MealsUtil;
4+
5+
/**
6+
* Created by dimas on 03.12.16.
7+
*/
8+
public class AuthorizedUser {
9+
10+
public static int id() {
11+
return 1;
12+
}
13+
14+
public static int getCaloriesPerDay() {
15+
return MealsUtil.DEFAULT_CALORIES_PER_DAY;
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
/**
4+
* Created by dimas on 03.12.16.
5+
*/
6+
public class BaseEntity {
7+
protected Integer id;
8+
9+
public BaseEntity() {
10+
}
11+
12+
protected BaseEntity(Integer id) {
13+
this.id = id;
14+
}
15+
16+
public void setId(Integer id) {
17+
this.id = id;
18+
}
19+
20+
public Integer getId() {
21+
return id;
22+
}
23+
24+
public boolean isNew() {
25+
return (this.id == null);
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
/**
4+
* Created by dimas on 03.12.16.
5+
*/
6+
public class NamedEntity extends BaseEntity {
7+
8+
protected String name;
9+
10+
public NamedEntity() {
11+
}
12+
13+
protected NamedEntity(Integer id, String name) {
14+
super(id);
15+
this.name = name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public String getName() {
23+
return this.name;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return name;
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
/**
4+
* Created by dimas on 03.12.16.
5+
*/
6+
public enum Role {
7+
ROLE_USER,
8+
ROLE_ADMIN
9+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
import ru.javawebinar.topjava.util.MealsUtil;
4+
5+
import java.util.Date;
6+
import java.util.EnumSet;
7+
import java.util.Set;
8+
9+
/**
10+
* Created by dimas on 03.12.16.
11+
*/
12+
public class User extends NamedEntity {
13+
14+
private String email;
15+
16+
private String password;
17+
18+
private boolean enabled = true;
19+
20+
private Date registered = new Date();
21+
22+
private Set<Role> roles;
23+
24+
private int caloriesPerDay = MealsUtil.DEFAULT_CALORIES_PER_DAY;
25+
26+
public User() {
27+
}
28+
29+
public User(Integer id, String name, String email, String password, Role role, Role... roles) {
30+
this(id, name, email, password, MealsUtil.DEFAULT_CALORIES_PER_DAY, true, EnumSet.of(role, roles));
31+
}
32+
33+
public User(Integer id, String name, String email, String password, int caloriesPerDay, boolean enabled, Set<Role> roles) {
34+
super(id, name);
35+
this.email = email;
36+
this.password = password;
37+
this.caloriesPerDay = caloriesPerDay;
38+
this.enabled = enabled;
39+
this.roles = roles;
40+
}
41+
42+
public String getEmail() {
43+
return email;
44+
}
45+
46+
public void setEmail(String email) {
47+
this.email = email;
48+
}
49+
50+
public void setPassword(String password) {
51+
this.password = password;
52+
}
53+
54+
public Date getRegistered() {
55+
return registered;
56+
}
57+
58+
public void setRegistered(Date registered) {
59+
this.registered = registered;
60+
}
61+
62+
public void setEnabled(boolean enabled) {
63+
this.enabled = enabled;
64+
}
65+
66+
public int getCaloriesPerDay() {
67+
return caloriesPerDay;
68+
}
69+
70+
public void setCaloriesPerDay(int caloriesPerDay) {
71+
this.caloriesPerDay = caloriesPerDay;
72+
}
73+
74+
public boolean isEnabled() {
75+
return enabled;
76+
}
77+
78+
public Set<Role> getRoles() {
79+
return roles;
80+
}
81+
82+
public String getPassword() {
83+
return password;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return "User (" +
89+
"id=" + id +
90+
", email=" + email +
91+
", name=" + name +
92+
", enabled=" + enabled +
93+
", roles=" + roles +
94+
", caloriesPerDay=" + caloriesPerDay +
95+
')';
96+
}
97+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ru.javawebinar.topjava.repository;
2+
3+
import ru.javawebinar.topjava.model.User;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by dimas on 03.12.16.
9+
*/
10+
public interface UserRepository {
11+
User save(User user);
12+
13+
// false if not found
14+
boolean delete(int id);
15+
16+
// null if not found
17+
User get(int id);
18+
19+
// null if not found
20+
User getByEmail(String email);
21+
22+
List<User> getAll();
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.javawebinar.topjava.service;
2+
3+
/**
4+
* Created by dimas on 03.12.16.
5+
*/
6+
public interface MealService {
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.javawebinar.topjava.service;
2+
3+
import ru.javawebinar.topjava.repository.MealRepository;
4+
5+
/**
6+
* Created by dimas on 03.12.16.
7+
*/
8+
public class MealServiceImpl implements MealService {
9+
10+
private MealRepository repository;
11+
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.javawebinar.topjava.service;
2+
3+
4+
import ru.javawebinar.topjava.model.User;
5+
import ru.javawebinar.topjava.util.exception.NotFoundException;
6+
7+
import java.util.List;
8+
9+
/**
10+
* Created by dimas on 03.12.16.
11+
*/
12+
public interface UserService {
13+
14+
User save(User user);
15+
16+
void delete(int id) throws NotFoundException;
17+
18+
User get(int id) throws NotFoundException;
19+
20+
User getByEmail(String email) throws NotFoundException;
21+
22+
List<User> getAll();
23+
24+
void update(User user);
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.javawebinar.topjava.util;
2+
3+
4+
import ru.javawebinar.topjava.util.exception.NotFoundException;
5+
6+
/**
7+
* Created by dimas on 03.12.16.
8+
*/
9+
public class ValidationUtil {
10+
public static void checkNotFoundWithId(boolean found, int id) {
11+
checkNotFound(found, "id=" + id);
12+
}
13+
14+
public static <T> T checkNotFoundWithId(T object, int id) {
15+
return checkNotFound(object, "id=" + id);
16+
}
17+
18+
public static <T> T checkNotFound(T object, String msg) {
19+
checkNotFound(object != null, msg);
20+
return object;
21+
}
22+
23+
public static void checkNotFound(boolean found, String msg) {
24+
if (!found) throw new NotFoundException("Not found entity with " + msg);
25+
}
26+
}

0 commit comments

Comments
 (0)