Skip to content

Commit 9cb684c

Browse files
committed
Applied: 2-2-HW1-optional.patch
1 parent eb1e73e commit 9cb684c

File tree

8 files changed

+223
-11
lines changed

8 files changed

+223
-11
lines changed

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,29 @@
77
* 11.01.2015.
88
*/
99
public class UserMeal {
10-
protected final LocalDateTime dateTime;
10+
private Integer id;
1111

12-
protected final String description;
12+
private final LocalDateTime dateTime;
1313

14-
protected final int calories;
14+
private final String description;
15+
16+
private final int calories;
1517

1618
public UserMeal(LocalDateTime dateTime, String description, int calories) {
19+
this(null, dateTime, description, calories);
20+
}
21+
22+
public UserMeal(Integer id, LocalDateTime dateTime, String description, int calories) {
23+
this.id = id;
1724
this.dateTime = dateTime;
1825
this.description = description;
1926
this.calories = calories;
2027
}
2128

29+
public void setId(int id) {
30+
this.id = id;
31+
}
32+
2233
public LocalDateTime getDateTime() {
2334
return dateTime;
2435
}
@@ -30,4 +41,22 @@ public String getDescription() {
3041
public int getCalories() {
3142
return calories;
3243
}
44+
45+
public Integer getId() {
46+
return id;
47+
}
48+
49+
public boolean isNew() {
50+
return id == null;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "UserMeal{" +
56+
"id=" + id +
57+
", dateTime=" + dateTime +
58+
", description='" + description + '\'' +
59+
", calories=" + calories +
60+
'}';
61+
}
3362
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* 11.01.2015.
88
*/
99
public class UserMealWithExceed {
10+
protected final Integer id;
11+
1012
private final LocalDateTime dateTime;
1113

1214
private final String description;
@@ -16,12 +18,21 @@ public class UserMealWithExceed {
1618
private final boolean exceed;
1719

1820
public UserMealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
21+
this(null, dateTime, description, calories, exceed);
22+
}
23+
24+
public UserMealWithExceed(Integer id, LocalDateTime dateTime, String description, int calories, boolean exceed) {
25+
this.id = id;
1926
this.dateTime = dateTime;
2027
this.description = description;
2128
this.calories = calories;
2229
this.exceed = exceed;
2330
}
2431

32+
public Integer getId() {
33+
return id;
34+
}
35+
2536
public LocalDateTime getDateTime() {
2637
return dateTime;
2738
}
@@ -41,7 +52,8 @@ public boolean isExceed() {
4152
@Override
4253
public String toString() {
4354
return "UserMealWithExceed{" +
44-
"dateTime=" + dateTime +
55+
"id=" + id +
56+
", dateTime=" + dateTime +
4557
", description='" + description + '\'' +
4658
", calories=" + calories +
4759
", exceed=" + exceed +
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ru.javawebinar.topjava.repository;
2+
3+
import ru.javawebinar.topjava.model.UserMeal;
4+
import ru.javawebinar.topjava.util.UserMealsUtil;
5+
6+
import java.util.Collection;
7+
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.atomic.AtomicInteger;
10+
11+
/**
12+
* GKislin
13+
* 15.09.2015.
14+
*/
15+
public class InMemoryUserMealRepositoryImpl implements UserMealRepository {
16+
private Map<Integer, UserMeal> repository = new ConcurrentHashMap<>();
17+
private AtomicInteger counter = new AtomicInteger(0);
18+
19+
{
20+
UserMealsUtil.MEAL_LIST.forEach(this::save);
21+
}
22+
23+
@Override
24+
public UserMeal save(UserMeal userMeal) {
25+
if (userMeal.isNew()) {
26+
userMeal.setId(counter.incrementAndGet());
27+
}
28+
repository.put(userMeal.getId(), userMeal);
29+
return userMeal;
30+
}
31+
32+
@Override
33+
public void delete(int id) {
34+
repository.remove(id);
35+
}
36+
37+
@Override
38+
public UserMeal get(int id) {
39+
return repository.get(id);
40+
}
41+
42+
@Override
43+
public Collection<UserMeal> getAll() {
44+
return repository.values();
45+
}
46+
}
47+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.javawebinar.topjava.repository;
2+
3+
import ru.javawebinar.topjava.model.UserMeal;
4+
5+
import java.util.Collection;
6+
7+
/**
8+
* GKislin
9+
* 06.03.2015.
10+
*/
11+
public interface UserMealRepository {
12+
UserMeal save(UserMeal userMeal);
13+
14+
void delete(int id);
15+
16+
UserMeal get(int id);
17+
18+
Collection<UserMeal> getAll();
19+
}

src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public static void main(String[] args) {
3333
System.out.println(getFilteredWithExceededByCycle(MEAL_LIST, LocalTime.of(7, 0), LocalTime.of(12, 0), DEFAULT_CALORIES_PER_DAY));
3434
}
3535

36-
public static List<UserMealWithExceed> getWithExceeded(List<UserMeal> mealList, int caloriesPerDay) {
36+
public static List<UserMealWithExceed> getWithExceeded(Collection<UserMeal> mealList, int caloriesPerDay) {
3737
return getFilteredWithExceeded(mealList, LocalTime.MIN, LocalTime.MAX, caloriesPerDay);
3838
}
3939

40-
public static List<UserMealWithExceed> getFilteredWithExceeded(List<UserMeal> mealList, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
40+
public static List<UserMealWithExceed> getFilteredWithExceeded(Collection<UserMeal> mealList, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
4141
Map<LocalDate, Integer> caloriesSumByDate = mealList.stream()
4242
.collect(
4343
Collectors.groupingBy(um -> um.getDateTime().toLocalDate(),
@@ -66,6 +66,6 @@ public static List<UserMealWithExceed> getFilteredWithExceededByCycle(List<UserM
6666
}
6767

6868
public static UserMealWithExceed createWithExceed(UserMeal um, boolean exceeded) {
69-
return new UserMealWithExceed(um.getDateTime(), um.getDescription(), um.getCalories(), exceeded);
69+
return new UserMealWithExceed(um.getId(), um.getDateTime(), um.getDescription(), um.getCalories(), exceeded);
7070
}
71-
}
71+
}

src/main/java/ru/javawebinar/topjava/web/MealServlet.java

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import ru.javawebinar.topjava.model.UserMeal;
6+
import ru.javawebinar.topjava.repository.InMemoryUserMealRepositoryImpl;
7+
import ru.javawebinar.topjava.repository.UserMealRepository;
58
import ru.javawebinar.topjava.util.UserMealsUtil;
69

10+
import javax.servlet.ServletConfig;
711
import javax.servlet.ServletException;
812
import javax.servlet.http.HttpServlet;
913
import javax.servlet.http.HttpServletRequest;
1014
import javax.servlet.http.HttpServletResponse;
1115
import java.io.IOException;
16+
import java.time.LocalDateTime;
17+
import java.util.Objects;
1218

1319
/**
1420
* User: gkislin
@@ -17,9 +23,50 @@
1723
public class MealServlet extends HttpServlet {
1824
private static final Logger LOG = LoggerFactory.getLogger(MealServlet.class);
1925

26+
private UserMealRepository repository;
27+
28+
@Override
29+
public void init(ServletConfig config) throws ServletException {
30+
super.init(config);
31+
repository = new InMemoryUserMealRepositoryImpl();
32+
}
33+
34+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
35+
request.setCharacterEncoding("UTF-8");
36+
String id = request.getParameter("id");
37+
UserMeal userMeal = new UserMeal(id.isEmpty() ? null : Integer.valueOf(id),
38+
LocalDateTime.parse(request.getParameter("dateTime")),
39+
request.getParameter("description"),
40+
Integer.valueOf(request.getParameter("calories")));
41+
LOG.info(userMeal.isNew() ? "Create {}" : "Update {}", userMeal);
42+
repository.save(userMeal);
43+
response.sendRedirect("meals");
44+
}
45+
2046
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21-
LOG.info("getAll");
22-
request.setAttribute("mealList", UserMealsUtil.getWithExceeded(UserMealsUtil.MEAL_LIST, UserMealsUtil.DEFAULT_CALORIES_PER_DAY));
23-
request.getRequestDispatcher("/mealList.jsp").forward(request, response);
47+
String action = request.getParameter("action");
48+
49+
if (action == null) {
50+
LOG.info("getAll");
51+
request.setAttribute("mealList",
52+
UserMealsUtil.getWithExceeded(repository.getAll(), UserMealsUtil.DEFAULT_CALORIES_PER_DAY));
53+
request.getRequestDispatcher("/mealList.jsp").forward(request, response);
54+
} else if (action.equals("delete")) {
55+
int id = getId(request);
56+
LOG.info("Delete {}", id);
57+
repository.delete(id);
58+
response.sendRedirect("meals");
59+
} else {
60+
final UserMeal meal = action.equals("create") ?
61+
new UserMeal(LocalDateTime.now(), "", 1000) :
62+
repository.get(getId(request));
63+
request.setAttribute("meal", meal);
64+
request.getRequestDispatcher("mealEdit.jsp").forward(request, response);
65+
}
66+
}
67+
68+
private int getId(HttpServletRequest request) {
69+
String paramId = Objects.requireNonNull(request.getParameter("id"));
70+
return Integer.valueOf(paramId);
2471
}
2572
}

src/main/webapp/mealEdit.jsp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3+
4+
<html>
5+
<head>
6+
<title>Meal</title>
7+
<style>
8+
dl {
9+
background: none repeat scroll 0 0 #FAFAFA;
10+
margin: 8px 0;
11+
padding: 0;
12+
}
13+
14+
dt {
15+
display: inline-block;
16+
width: 170px;
17+
}
18+
19+
dd {
20+
display: inline-block;
21+
margin-left: 8px;
22+
vertical-align: top;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
<section>
28+
<h2><a href="">Home</a></h2>
29+
<h3>Edit meal</h3>
30+
<hr>
31+
<jsp:useBean id="meal" type="ru.javawebinar.topjava.model.UserMeal" scope="request"/>
32+
<form method="post" action="meals">
33+
<input type="hidden" name="id" value="${meal.id}">
34+
<dl>
35+
<dt>DateTime:</dt>
36+
<dd><input type="datetime-local" value="${meal.dateTime}" name="dateTime"></dd>
37+
</dl>
38+
<dl>
39+
<dt>Description:</dt>
40+
<dd><input type="text" value="${meal.description}" size=40 name="description"></dd>
41+
</dl>
42+
<dl>
43+
<dt>Calories:</dt>
44+
<dd><input type="number" value="${meal.calories}" name="calories"></dd>
45+
</dl>
46+
<button type="submit">Save</button>
47+
<button onclick="window.history.back()">Cancel</button>
48+
</form>
49+
</section>
50+
</body>
51+
</html>

src/main/webapp/mealList.jsp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
${meal.dateTime.toLocalDate()} ${meal.dateTime.toLocalTime()}
2+
<%--<%=TimeUtil.toString(meal.getDateTime())%>--%>
13
<%@ page import="ru.javawebinar.topjava.util.TimeUtil" %>
24
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
35
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
@@ -19,13 +21,16 @@
1921
<section>
2022
<h2><a href="index.html">Home</a></h2>
2123
<h3>Meal list</h3>
24+
<a href="meals?action=create">Add Meal</a>
2225
<hr>
2326
<table border="1" cellpadding="8" cellspacing="0">
2427
<thead>
2528
<tr>
2629
<th>Date</th>
2730
<th>Description</th>
2831
<th>Calories</th>
32+
<th></th>
33+
<th></th>
2934
</tr>
3035
</thead>
3136
<c:forEach items="${mealList}" var="meal">
@@ -37,6 +42,8 @@
3742
</td>
3843
<td>${meal.description}</td>
3944
<td>${meal.calories}</td>
45+
<td><a href="meals?action=update&id=${meal.id}">Update</a></td>
46+
<td><a href="meals?action=delete&id=${meal.id}">Delete</a></td>
4047
</tr>
4148
</c:forEach>
4249
</table>

0 commit comments

Comments
 (0)