Skip to content

Commit 24ddbf6

Browse files
3_03_HW2_meal_layers.patch
1 parent cae69bb commit 24ddbf6

8 files changed

Lines changed: 143 additions & 21 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
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.MealTo;
8+
import ru.javawebinar.topjava.web.meal.MealRestController;
79
import ru.javawebinar.topjava.web.user.AdminRestController;
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
public class SpringMain {
1218
public static void main(String[] args) {
@@ -15,6 +21,16 @@ public static void main(String[] args) {
1521
System.out.println("Bean definition names: " + Arrays.toString(appCtx.getBeanDefinitionNames()));
1622
AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
1723
adminUserController.create(new User(null, "userName", "email@mail.ru", "password", Role.ADMIN));
24+
System.out.println();
25+
26+
MealRestController mealController = appCtx.getBean(MealRestController.class);
27+
List<MealTo> filteredMealsWithExcess =
28+
mealController.getBetween(
29+
LocalDate.of(2020, Month.JANUARY, 30), LocalTime.of(7, 0),
30+
LocalDate.of(2020, Month.JANUARY, 31), LocalTime.of(11, 0));
31+
filteredMealsWithExcess.forEach(System.out::println);
32+
System.out.println();
33+
System.out.println(mealController.getBetween(null, null, null, null));
1834
}
1935
}
2036
}

‎src/main/java/ru/javawebinar/topjava/model/Meal.java‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import java.time.LocalDateTime;
55
import java.time.LocalTime;
66

7-
public class Meal {
8-
private Integer id;
9-
7+
public class Meal extends AbstractBaseEntity {
108
private final LocalDateTime dateTime;
119

1210
private final String description;
@@ -18,20 +16,12 @@ public Meal(LocalDateTime dateTime, String description, int calories) {
1816
}
1917

2018
public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
21-
this.id = id;
19+
super(id);
2220
this.dateTime = dateTime;
2321
this.description = description;
2422
this.calories = calories;
2523
}
2624

27-
public Integer getId() {
28-
return id;
29-
}
30-
31-
public void setId(Integer id) {
32-
this.id = id;
33-
}
34-
3525
public LocalDateTime getDateTime() {
3626
return dateTime;
3727
}
@@ -52,10 +42,6 @@ public LocalTime getTime() {
5242
return dateTime.toLocalTime();
5343
}
5444

55-
public boolean isNew() {
56-
return id == null;
57-
}
58-
5945
@Override
6046
public String toString() {
6147
return "Meal{" +
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,47 @@
11
package ru.javawebinar.topjava.service;
22

3+
import org.springframework.lang.Nullable;
4+
import org.springframework.stereotype.Service;
5+
import ru.javawebinar.topjava.model.Meal;
36
import ru.javawebinar.topjava.repository.MealRepository;
47

8+
import java.time.LocalDate;
9+
import java.util.List;
10+
11+
import static ru.javawebinar.topjava.util.DateTimeUtil.atStartOfDayOrMin;
12+
import static ru.javawebinar.topjava.util.DateTimeUtil.atStartOfNextDayOrMax;
13+
import static ru.javawebinar.topjava.util.ValidationUtil.checkNotFoundWithId;
14+
15+
@Service
516
public class MealService {
617

7-
private MealRepository repository;
18+
private final MealRepository repository;
19+
20+
public MealService(MealRepository repository) {
21+
this.repository = repository;
22+
}
23+
24+
public Meal get(int id, int userId) {
25+
return checkNotFoundWithId(repository.get(id, userId), id);
26+
}
27+
28+
public void delete(int id, int userId) {
29+
checkNotFoundWithId(repository.delete(id, userId), id);
30+
}
31+
32+
public List<Meal> getBetweenInclusive(@Nullable LocalDate startDate, @Nullable LocalDate endDate, int userId) {
33+
return repository.getBetweenHalfOpen(atStartOfDayOrMin(startDate), atStartOfNextDayOrMax(endDate), userId);
34+
}
35+
36+
public List<Meal> getAll(int userId) {
37+
return repository.getAll(userId);
38+
}
39+
40+
public void update(Meal meal, int userId) {
41+
checkNotFoundWithId(repository.save(meal, userId), meal.getId());
42+
}
843

44+
public Meal create(Meal meal, int userId) {
45+
return repository.save(meal, userId);
46+
}
947
}

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

Lines changed: 1 addition & 1 deletion
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

‎src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package ru.javawebinar.topjava.util;
22

3+
import java.time.LocalDate;
34
import java.time.LocalDateTime;
45
import java.time.format.DateTimeFormatter;
6+
import java.time.temporal.ChronoUnit;
57

68
public class DateTimeUtil {
79
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
810

11+
// DB doesn't support LocalDate.MIN/MAX
12+
private static final LocalDateTime MIN_DATE = LocalDateTime.of(1, 1, 1, 0, 0);
13+
private static final LocalDateTime MAX_DATE = LocalDateTime.of(3000, 1, 1, 0, 0);
14+
15+
public static LocalDateTime atStartOfDayOrMin(LocalDate localDate) {
16+
return localDate != null ? localDate.atStartOfDay() : MIN_DATE;
17+
}
18+
19+
public static LocalDateTime atStartOfNextDayOrMax(LocalDate localDate) {
20+
return localDate != null ? localDate.plus(1, ChronoUnit.DAYS).atStartOfDay() : MAX_DATE;
21+
}
22+
923
public static String toString(LocalDateTime ldt) {
1024
return ldt == null ? "" : ldt.format(DATE_TIME_FORMATTER);
1125
}

‎src/main/java/ru/javawebinar/topjava/util/MealsUtil.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ru.javawebinar.topjava.util;
22

33
import ru.javawebinar.topjava.model.Meal;
4-
import ru.javawebinar.topjava.model.MealTo;
4+
import ru.javawebinar.topjava.to.MealTo;
55

66
import java.time.LocalDate;
77
import java.time.LocalDateTime;
Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,76 @@
11
package ru.javawebinar.topjava.web.meal;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.lang.Nullable;
6+
import org.springframework.stereotype.Controller;
7+
import ru.javawebinar.topjava.model.Meal;
38
import ru.javawebinar.topjava.service.MealService;
9+
import ru.javawebinar.topjava.to.MealTo;
10+
import ru.javawebinar.topjava.util.MealsUtil;
11+
import ru.javawebinar.topjava.web.SecurityUtil;
412

13+
import java.time.LocalDate;
14+
import java.time.LocalTime;
15+
import java.util.List;
16+
17+
import static ru.javawebinar.topjava.util.ValidationUtil.assureIdConsistent;
18+
import static ru.javawebinar.topjava.util.ValidationUtil.checkNew;
19+
20+
@Controller
521
public class MealRestController {
6-
private MealService service;
22+
private static final Logger log = LoggerFactory.getLogger(MealRestController.class);
23+
24+
private final MealService service;
25+
26+
public MealRestController(MealService service) {
27+
this.service = service;
28+
}
29+
30+
public Meal get(int id) {
31+
int userId = SecurityUtil.authUserId();
32+
log.info("get meal {} for user {}", id, userId);
33+
return service.get(id, userId);
34+
}
35+
36+
public void delete(int id) {
37+
int userId = SecurityUtil.authUserId();
38+
log.info("delete meal {} for user {}", id, userId);
39+
service.delete(id, userId);
40+
}
41+
42+
public List<MealTo> getAll() {
43+
int userId = SecurityUtil.authUserId();
44+
log.info("getAll for user {}", userId);
45+
return MealsUtil.getTos(service.getAll(userId), SecurityUtil.authUserCaloriesPerDay());
46+
}
47+
48+
public Meal create(Meal meal) {
49+
int userId = SecurityUtil.authUserId();
50+
checkNew(meal);
51+
log.info("create {} for user {}", meal, userId);
52+
return service.create(meal, userId);
53+
}
54+
55+
public void update(Meal meal, int id) {
56+
int userId = SecurityUtil.authUserId();
57+
assureIdConsistent(meal, id);
58+
log.info("update {} for user {}", meal, userId);
59+
service.update(meal, userId);
60+
}
61+
62+
/**
63+
* <ol>Filter separately
64+
* <li>by date</li>
65+
* <li>by time for every date</li>
66+
* </ol>
67+
*/
68+
public List<MealTo> getBetween(@Nullable LocalDate startDate, @Nullable LocalTime startTime,
69+
@Nullable LocalDate endDate, @Nullable LocalTime endTime) {
70+
int userId = SecurityUtil.authUserId();
71+
log.info("getBetween dates({} - {}) time({} - {}) for user {}", startDate, endDate, startTime, endTime, userId);
772

73+
List<Meal> mealsDateFiltered = service.getBetweenInclusive(startDate, endDate, userId);
74+
return MealsUtil.getFilteredTos(mealsDateFiltered, SecurityUtil.authUserCaloriesPerDay(), startTime, endTime);
75+
}
876
}

‎src/main/webapp/meals.jsp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</tr>
3535
</thead>
3636
<c:forEach items="${requestScope.meals}" var="meal">
37-
<jsp:useBean id="meal" type="ru.javawebinar.topjava.model.MealTo"/>
37+
<jsp:useBean id="meal" type="ru.javawebinar.topjava.to.MealTo"/>
3838
<tr class="${meal.excess ? 'excess' : 'normal'}">
3939
<td>
4040
<%--${meal.dateTime.toLocalDate()} ${meal.dateTime.toLocalTime()}--%>

0 commit comments

Comments
 (0)