Skip to content

Commit 79e3f0f

Browse files
committed
2 3 app layers
1 parent 8454e97 commit 79e3f0f

16 files changed

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

0 commit comments

Comments
 (0)