Skip to content

Commit 63ec599

Browse files
committed
1_HW02 patch
1 parent 6627656 commit 63ec599

12 files changed

Lines changed: 267 additions & 44 deletions
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package ru.javawebinar.topjava;
22

3-
import ru.javawebinar.topjava.model.Role;
4-
5-
import java.util.Set;
6-
73
/**
84
* GKislin
95
* 06.03.2015.
@@ -13,4 +9,8 @@ public class LoggedUser {
139
public static int id() {
1410
return 1;
1511
}
12+
13+
public static int getCaloriesPerDay() {
14+
return 2000;
15+
}
1616
}

src/main/java/ru/javawebinar/topjava/SpringMain.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
import org.springframework.context.support.ClassPathXmlApplicationContext;
55
import ru.javawebinar.topjava.model.Role;
66
import ru.javawebinar.topjava.model.User;
7+
import ru.javawebinar.topjava.to.UserMealWithExceed;
8+
import ru.javawebinar.topjava.web.meal.UserMealRestController;
79
import ru.javawebinar.topjava.web.user.AdminUserRestController;
810

11+
import java.time.LocalDate;
12+
import java.time.LocalTime;
13+
import java.time.Month;
914
import java.util.Arrays;
15+
import java.util.List;
1016

1117
/**
1218
* User: gkislin
@@ -15,10 +21,17 @@
1521
public class SpringMain {
1622
public static void main(String[] args) {
1723
// java 7 Automatic resource management
18-
try(ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring-app.xml")) {
24+
try (ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring-app.xml")) {
1925
System.out.println(Arrays.toString(appCtx.getBeanDefinitionNames()));
2026
AdminUserRestController adminUserController = appCtx.getBean(AdminUserRestController.class);
2127
System.out.println(adminUserController.create(new User(1, "userName", "email", "password", Role.ROLE_ADMIN)));
28+
System.out.println();
29+
UserMealRestController mealController = appCtx.getBean(UserMealRestController.class);
30+
List<UserMealWithExceed> filteredMealsWithExceeded =
31+
mealController.getBetween(
32+
LocalDate.of(2015, Month.MAY, 30), LocalTime.of(7, 0),
33+
LocalDate.of(2015, Month.MAY, 31), LocalTime.of(11, 0));
34+
filteredMealsWithExceeded.forEach(System.out::println);
2235
}
2336
}
2437
}

src/main/java/ru/javawebinar/topjava/model/UserMeal.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
* GKislin
77
* 11.01.2015.
88
*/
9-
public class UserMeal {
10-
protected final LocalDateTime dateTime;
9+
public class UserMeal extends BaseEntity{
10+
protected LocalDateTime dateTime;
1111

12-
protected final String description;
12+
protected String description;
1313

14-
protected final int calories;
14+
protected int calories;
1515

16-
public UserMeal(LocalDateTime dateTime, String description, int calories) {
16+
private User user;
17+
18+
public UserMeal() {
19+
}
20+
21+
public UserMeal(Integer id, LocalDateTime dateTime, String description, int calories) {
22+
super(id);
1723
this.dateTime = dateTime;
1824
this.description = description;
1925
this.calories = calories;
@@ -30,4 +36,17 @@ public String getDescription() {
3036
public int getCalories() {
3137
return calories;
3238
}
33-
}
39+
40+
public User getUser() {
41+
return user;
42+
}
43+
44+
public void setUser(User user) {
45+
this.user = user;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Meal(" + id + ", " + dateTime + ", '" + description + "', calories:" + calories + ')';
51+
}
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
package ru.javawebinar.topjava.repository;
22

3+
import ru.javawebinar.topjava.model.UserMeal;
4+
5+
import ru.javawebinar.topjava.model.UserMeal;
6+
7+
import java.time.LocalDateTime;
8+
import java.util.List;
9+
310
/**
411
* GKislin
512
* 06.03.2015.
613
*/
714
public interface UserMealRepository {
15+
// null if updated meal do not belong to userId
16+
UserMeal save(UserMeal userMeal, int userId);
17+
18+
// false if meal do not belong to userId
19+
boolean delete(int id, int userId);
20+
21+
// null if meal do not belong to userId
22+
UserMeal get(int id, int userId);
23+
24+
// ORDERED DATE, TIME
25+
List<UserMeal> getAll(int userId);
26+
27+
void deleteAll(int userId);
28+
29+
List<UserMeal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId);
30+
31+
32+
833
}
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
11
package ru.javawebinar.topjava.repository.mock;
22

3+
import org.springframework.stereotype.Repository;
4+
import ru.javawebinar.topjava.LoggerWrapper;
5+
import ru.javawebinar.topjava.model.UserMeal;
6+
import ru.javawebinar.topjava.repository.UserMealRepository;
7+
8+
import java.time.LocalDateTime;
9+
import java.time.Month;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
313
/**
414
* GKislin
515
* 15.06.2015.
616
*/
7-
public class MockUserMealRepositoryImpl {
17+
@Repository
18+
public class MockUserMealRepositoryImpl implements UserMealRepository {
19+
private static final LoggerWrapper LOG = LoggerWrapper.get(MockUserRepositoryImpl.class);
20+
private static final List<UserMeal> mealList = Arrays.asList(
21+
new UserMeal(1, LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500),
22+
new UserMeal(2, LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000),
23+
new UserMeal(3, LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500),
24+
new UserMeal(4, LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 500),
25+
new UserMeal(5, LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 1000),
26+
new UserMeal(6, LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510)
27+
);
28+
29+
@Override
30+
public boolean delete(int id, int userId) {
31+
LOG.info("delete {} for User {}", id, userId);
32+
return true;
33+
}
34+
35+
@Override
36+
public UserMeal save(UserMeal meal, int userId) {
37+
LOG.info("save {} for User {}", meal, userId);
38+
return meal;
39+
}
40+
41+
@Override
42+
public UserMeal get(int id, int userId) {
43+
LOG.info("get {} for User {}", id, userId);
44+
return null;
45+
}
46+
47+
@Override
48+
public List<UserMeal> getAll(int userId) {
49+
LOG.info("getAll for User {}", userId);
50+
return mealList;
51+
}
52+
53+
@Override
54+
public void deleteAll(int userId) {
55+
LOG.info("deleteAll for User {}", userId);
56+
}
57+
58+
@Override
59+
public List<UserMeal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId) {
60+
LOG.info("getBetween {} - {} for User {}", startDate, endDate, userId);
61+
return mealList;
62+
}
863
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
package ru.javawebinar.topjava.service;
22

3+
import ru.javawebinar.topjava.model.UserMeal;
4+
5+
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
7+
import java.time.LocalTime;
8+
import java.util.List;
9+
310
/**
411
* GKislin
512
* 15.06.2015.
613
*/
714
public interface UserMealService {
15+
UserMeal get(int id, int userId);
16+
17+
void delete(int id, int userId);
18+
19+
default List<UserMeal> getBetweenDates(LocalDate startDate, LocalDate endDate, int userId) {
20+
return getBetweenDateTimes(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX), userId);
21+
}
22+
23+
List<UserMeal> getBetweenDateTimes(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId);
24+
25+
List<UserMeal> getAll(int userId);
26+
27+
void deleteAll(int userId);
28+
29+
UserMeal update(UserMeal meal, int userId);
30+
31+
UserMeal save(UserMeal meal, int userId);
832
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,56 @@
11
package ru.javawebinar.topjava.service;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
import ru.javawebinar.topjava.model.UserMeal;
36
import ru.javawebinar.topjava.repository.UserMealRepository;
7+
import ru.javawebinar.topjava.util.exception.ExceptionUtil;
8+
9+
import java.time.LocalDateTime;
10+
import java.util.List;
411

512
/**
613
* GKislin
714
* 06.03.2015.
815
*/
16+
@Service
917
public class UserMealServiceImpl implements UserMealService {
1018

19+
@Autowired
1120
private UserMealRepository repository;
1221

22+
@Override
23+
public UserMeal get(int id, int userId) {
24+
return ExceptionUtil.check(repository.get(id, userId), id);
25+
}
26+
27+
@Override
28+
public void delete(int id, int userId) {
29+
ExceptionUtil.check(repository.delete(id, userId), id);
30+
}
31+
32+
@Override
33+
public List<UserMeal> getBetweenDateTimes(LocalDateTime startDate, LocalDateTime endDate, int userId) {
34+
return repository.getBetween(startDate, endDate, userId);
35+
}
36+
37+
@Override
38+
public List<UserMeal> getAll(int userId) {
39+
return repository.getAll(userId);
40+
}
41+
42+
@Override
43+
public void deleteAll(int userId) {
44+
repository.deleteAll(userId);
45+
}
46+
47+
@Override
48+
public UserMeal update(UserMeal meal, int userId) {
49+
return ExceptionUtil.check(repository.save(meal, userId), meal.getId());
50+
}
51+
52+
@Override
53+
public UserMeal save(UserMeal meal, int userId) {
54+
return repository.save(meal, userId);
55+
}
1356
}

src/main/java/ru/javawebinar/topjava/service/UserService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
*/
1313
public interface UserService {
1414

15-
public User save(User user);
15+
User save(User user);
1616

17-
public void delete(int id) throws NotFoundException;
17+
void delete(int id) throws NotFoundException;
1818

19-
public User get(int id) throws NotFoundException;
19+
User get(int id) throws NotFoundException;
2020

21-
public User getByEmail(String email) throws NotFoundException;
21+
User getByEmail(String email) throws NotFoundException;
2222

23-
public List<User> getAll();
23+
List<User> getAll();
2424

25-
public void update(User user) throws NotFoundException;
25+
void update(User user) throws NotFoundException;
2626
}

src/main/java/ru/javawebinar/topjava/service/UserServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ public class UserServiceImpl implements UserService {
1919
@Autowired
2020
private UserRepository repository;
2121

22-
public void setRepository(UserRepository repository) {
23-
this.repository = repository;
24-
}
25-
2622
public User save(User user) {
2723
return repository.save(user);
2824
}

src/main/java/ru/javawebinar/topjava/model/UserMealWithExceed.java renamed to src/main/java/ru/javawebinar/topjava/to/UserMealWithExceed.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.javawebinar.topjava.model;
1+
package ru.javawebinar.topjava.to;
22

33
import java.time.LocalDateTime;
44

@@ -7,6 +7,8 @@
77
* 11.01.2015.
88
*/
99
public class UserMealWithExceed {
10+
protected final Integer id;
11+
1012
protected final LocalDateTime dateTime;
1113

1214
protected final String description;
@@ -15,7 +17,8 @@ public class UserMealWithExceed {
1517

1618
protected final boolean exceed;
1719

18-
public UserMealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
20+
public UserMealWithExceed(Integer id, LocalDateTime dateTime, String description, int calories, boolean exceed) {
21+
this.id = id;
1922
this.dateTime = dateTime;
2023
this.description = description;
2124
this.calories = calories;

0 commit comments

Comments
 (0)