Skip to content

Commit fe1e093

Browse files
committed
3 02 HW2 meal layers
1 parent fcadc29 commit fe1e093

10 files changed

Lines changed: 146 additions & 17 deletions

File tree

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

Lines changed: 14 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.MealWithExceed;
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
/**
1218
* User: gkislin
@@ -19,6 +25,14 @@ public static void main(String[] args) {
1925
System.out.println("Bean definition names: " + Arrays.toString(appCtx.getBeanDefinitionNames()));
2026
AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
2127
adminUserController.create(new User(1, "userName", "email", "password", Role.ROLE_ADMIN));
28+
System.out.println();
29+
30+
MealRestController mealController = appCtx.getBean(MealRestController.class);
31+
List<MealWithExceed> filteredMealsWithExceeded =
32+
mealController.getBetween(
33+
LocalDate.of(2015, Month.MAY, 30), LocalTime.of(7, 0),
34+
LocalDate.of(2015, Month.MAY, 31), LocalTime.of(11, 0));
35+
filteredMealsWithExceeded.forEach(System.out::println);
2236
}
2337
}
2438
}

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* GKislin
99
* 11.01.2015.
1010
*/
11-
public class Meal {
11+
public class Meal extends BaseEntity {
1212
private Integer id;
1313

1414
private final LocalDateTime dateTime;
@@ -22,20 +22,12 @@ public Meal(LocalDateTime dateTime, String description, int calories) {
2222
}
2323

2424
public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
25-
this.id = id;
25+
super(id);
2626
this.dateTime = dateTime;
2727
this.description = description;
2828
this.calories = calories;
2929
}
3030

31-
public Integer getId() {
32-
return id;
33-
}
34-
35-
public void setId(Integer id) {
36-
this.id = id;
37-
}
38-
3931
public LocalDateTime getDateTime() {
4032
return dateTime;
4133
}
@@ -56,10 +48,6 @@ public LocalTime getTime() {
5648
return dateTime.toLocalTime();
5749
}
5850

59-
public boolean isNew() {
60-
return id == null;
61-
}
62-
6351
@Override
6452
public String toString() {
6553
return "Meal{" +

‎src/main/java/ru/javawebinar/topjava/repository/mock/InMemoryMealRepositoryImpl.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.javawebinar.topjava.repository.mock;
22

3+
import org.springframework.stereotype.Repository;
34
import ru.javawebinar.topjava.model.Meal;
45
import ru.javawebinar.topjava.repository.MealRepository;
56
import ru.javawebinar.topjava.util.MealsUtil;
@@ -19,6 +20,7 @@
1920
* GKislin
2021
* 15.09.2015.
2122
*/
23+
@Repository
2224
public class InMemoryMealRepositoryImpl implements MealRepository {
2325

2426
private static final Comparator<Meal> MEAL_COMPARATOR = Comparator.comparing(Meal::getDateTime).reversed();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
package ru.javawebinar.topjava.service;
22

3+
import ru.javawebinar.topjava.model.Meal;
4+
import ru.javawebinar.topjava.util.exception.NotFoundException;
5+
6+
import java.time.LocalDate;
7+
import java.time.LocalDateTime;
8+
import java.time.LocalTime;
9+
import java.util.Collection;
10+
311
/**
412
* GKislin
513
* 15.06.2015.
614
*/
715
public interface MealService {
16+
Meal get(int id, int userId) throws NotFoundException;
17+
18+
void delete(int id, int userId) throws NotFoundException;
19+
20+
default Collection<Meal> getBetweenDates(LocalDate startDate, LocalDate endDate, int userId) {
21+
return getBetweenDateTimes(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX), userId);
22+
}
23+
24+
Collection<Meal> getBetweenDateTimes(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId);
25+
26+
Collection<Meal> getAll(int userId);
27+
28+
Meal update(Meal meal, int userId) throws NotFoundException;
29+
30+
Meal save(Meal meal, int userId);
831
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
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.Meal;
36
import ru.javawebinar.topjava.repository.MealRepository;
7+
import ru.javawebinar.topjava.util.exception.ExceptionUtil;
8+
9+
import java.time.LocalDateTime;
10+
import java.util.Collection;
411

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

19+
@Autowired
1120
private MealRepository repository;
1221

22+
@Override
23+
public Meal get(int id, int userId) {
24+
return ExceptionUtil.checkNotFoundWithId(repository.get(id, userId), id);
25+
}
26+
27+
@Override
28+
public void delete(int id, int userId) {
29+
ExceptionUtil.checkNotFoundWithId(repository.delete(id, userId), id);
30+
}
31+
32+
@Override
33+
public Collection<Meal> getBetweenDateTimes(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
34+
return repository.getBetween(startDateTime, endDateTime, userId);
35+
}
36+
37+
@Override
38+
public Collection<Meal> getAll(int userId) {
39+
return repository.getAll(userId);
40+
}
41+
42+
@Override
43+
public Meal update(Meal meal, int userId) {
44+
return ExceptionUtil.checkNotFoundWithId(repository.save(meal, userId), meal.getId());
45+
}
46+
47+
@Override
48+
public Meal save(Meal meal, int userId) {
49+
return repository.save(meal, userId);
50+
}
1351
}

src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java renamed to src/main/java/ru/javawebinar/topjava/to/MealWithExceed.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/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.MealWithExceed;
4+
import ru.javawebinar.topjava.to.MealWithExceed;
55

66
import java.time.LocalDate;
77
import java.time.LocalDateTime;

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

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

3+
import java.time.LocalDate;
34
import java.time.LocalDateTime;
45
import java.time.format.DateTimeFormatter;
56

@@ -10,6 +11,9 @@
1011
public class TimeUtil {
1112
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
1213

14+
public static final LocalDate MIN_DATE = LocalDate.of(1, 1, 1);
15+
public static final LocalDate MAX_DATE = LocalDate.of(3000, 1, 1);
16+
1317
public static <T extends Comparable<? super T>> boolean isBetween(T value, T start, T end) {
1418
return value.compareTo(start) >= 0 && value.compareTo(end) <= 0;
1519
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,72 @@
11
package ru.javawebinar.topjava.web.meal;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import ru.javawebinar.topjava.AuthorizedUser;
8+
import ru.javawebinar.topjava.model.Meal;
39
import ru.javawebinar.topjava.service.MealService;
10+
import ru.javawebinar.topjava.to.MealWithExceed;
11+
import ru.javawebinar.topjava.util.MealsUtil;
12+
import ru.javawebinar.topjava.util.TimeUtil;
13+
14+
import java.time.LocalDate;
15+
import java.time.LocalTime;
16+
import java.util.List;
417

518
/**
619
* GKislin
720
* 06.03.2015.
821
*/
22+
@Controller
923
public class MealRestController {
24+
private static final Logger LOG = LoggerFactory.getLogger(MealRestController.class);
25+
26+
@Autowired
1027
private MealService service;
1128

29+
public Meal get(int id) {
30+
int userId = AuthorizedUser.id();
31+
LOG.info("get meal {} for User {}", id, userId);
32+
return service.get(id, userId);
33+
}
34+
35+
public void delete(int id) {
36+
int userId = AuthorizedUser.id();
37+
LOG.info("delete meal {} for User {}", id, userId);
38+
service.delete(id, userId);
39+
}
40+
41+
public List<MealWithExceed> getAll() {
42+
int userId = AuthorizedUser.id();
43+
LOG.info("getAll for User {}", userId);
44+
return MealsUtil.getWithExceeded(service.getAll(userId), AuthorizedUser.getCaloriesPerDay());
45+
}
46+
47+
public void update(Meal meal, int id) {
48+
meal.setId(id);
49+
int userId = AuthorizedUser.id();
50+
LOG.info("update {} for User {}", meal, userId);
51+
service.update(meal, userId);
52+
}
53+
54+
public Meal create(Meal meal) {
55+
meal.setId(null);
56+
int userId = AuthorizedUser.id();
57+
LOG.info("create {} for User {}", meal, userId);
58+
return service.save(meal, userId);
59+
}
60+
61+
public List<MealWithExceed> getBetween(LocalDate startDate, LocalTime startTime, LocalDate endDate, LocalTime endTime) {
62+
int userId = AuthorizedUser.id();
63+
LOG.info("getBetween dates {} - {} for time {} - {} for User {}", startDate, endDate, startTime, endTime, userId);
64+
65+
return MealsUtil.getFilteredWithExceeded(
66+
service.getBetweenDates(startDate != null ? startDate : TimeUtil.MIN_DATE, endDate != null ? endDate : TimeUtil.MAX_DATE, userId),
67+
startTime != null ? startTime : LocalTime.MIN,
68+
endTime != null ? endTime : LocalTime.MAX,
69+
AuthorizedUser.getCaloriesPerDay()
70+
);
71+
}
1272
}

‎src/main/webapp/mealList.jsp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</tr>
3333
</thead>
3434
<c:forEach items="${mealList}" var="meal">
35-
<jsp:useBean id="meal" scope="page" type="ru.javawebinar.topjava.model.MealWithExceed"/>
35+
<jsp:useBean id="meal" scope="page" type="ru.javawebinar.topjava.to.MealWithExceed"/>
3636
<tr class="${meal.exceed ? 'exceeded' : 'normal'}">
3737
<td>
3838
<%--${meal.dateTime.toLocalDate()} ${meal.dateTime.toLocalTime()}--%>

0 commit comments

Comments
 (0)