Repository navigation
Expand file tree
/
Copy path3_04_HW2_optional_filter.patch
More file actions
220 lines (214 loc) · 7.44 KB
/
Copy path3_04_HW2_optional_filter.patch
File metadata and controls
220 lines (214 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
Index: src/main/webapp/css/style.css
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/webapp/css/style.css (date 1531049556494)
+++ src/main/webapp/css/style.css (date 1531049556494)
@@ -0,0 +1,24 @@
+dl {
+ background: none repeat scroll 0 0 #FAFAFA;
+ margin: 8px 0;
+ padding: 0;
+}
+
+dt {
+ display: inline-block;
+ width: 170px;
+}
+
+dd {
+ display: inline-block;
+ margin-left: 8px;
+ vertical-align: top;
+}
+
+tr[data-mealExceed="false"] {
+ color: green;
+}
+
+tr[data-mealExceed="true"] {
+ color: red;
+}
Index: src/main/java/ru/javawebinar/topjava/web/MealServlet.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/web/MealServlet.java (date 1531049529000)
+++ src/main/java/ru/javawebinar/topjava/web/MealServlet.java (date 1531049556484)
@@ -11,10 +11,15 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
+import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
+import static ru.javawebinar.topjava.util.DateTimeUtil.parseLocalDate;
+import static ru.javawebinar.topjava.util.DateTimeUtil.parseLocalTime;
+
public class MealServlet extends HttpServlet {
private ConfigurableApplicationContext springContext;
@@ -36,17 +41,28 @@
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
- Meal meal = new Meal(
- LocalDateTime.parse(request.getParameter("dateTime")),
- request.getParameter("description"),
- Integer.parseInt(request.getParameter("calories")));
+ String action = request.getParameter("action");
+ if (action == null) {
+ Meal meal = new Meal(
+ LocalDateTime.parse(request.getParameter("dateTime")),
+ request.getParameter("description"),
+ Integer.parseInt(request.getParameter("calories")));
- if (request.getParameter("id").isEmpty()) {
- mealController.create(meal);
- } else {
- mealController.update(meal, getId(request));
- }
- response.sendRedirect("meals");
+ if (request.getParameter("id").isEmpty()) {
+ mealController.create(meal);
+ } else {
+ mealController.update(meal, getId(request));
+ }
+ response.sendRedirect("meals");
+
+ } else if ("filter".equals(action)) {
+ LocalDate startDate = parseLocalDate(request.getParameter("startDate"));
+ LocalDate endDate = parseLocalDate(request.getParameter("endDate"));
+ LocalTime startTime = parseLocalTime(request.getParameter("startTime"));
+ LocalTime endTime = parseLocalTime(request.getParameter("endTime"));
+ request.setAttribute("meals", mealController.getBetween(startDate, startTime, endDate, endTime));
+ request.getRequestDispatcher("/meals.jsp").forward(request, response);
+ }
}
@Override
Index: src/main/webapp/mealForm.jsp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/webapp/mealForm.jsp (date 1531049529000)
+++ src/main/webapp/mealForm.jsp (date 1531049556521)
@@ -4,24 +4,7 @@
<html>
<head>
<title>Meal</title>
- <style>
- dl {
- background: none repeat scroll 0 0 #FAFAFA;
- margin: 8px 0;
- padding: 0;
- }
-
- dt {
- display: inline-block;
- width: 170px;
- }
-
- dd {
- display: inline-block;
- margin-left: 8px;
- vertical-align: top;
- }
- </style>
+ <link rel="stylesheet" href="css/style.css">
</head>
<body>
<section>
Index: src/main/webapp/meals.jsp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/webapp/meals.jsp (date 1531049529000)
+++ src/main/webapp/meals.jsp (date 1531049556535)
@@ -2,24 +2,35 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://topjava.javawebinar.ru/functions" %>
-<%--<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>--%>
<html>
<head>
<title>Meal list</title>
- <style>
- .normal {
- color: green;
- }
-
- .exceeded {
- color: red;
- }
- </style>
+ <link rel="stylesheet" href="css/style.css">
</head>
<body>
<section>
<h3><a href="index.html">Home</a></h3>
<h2>Meals</h2>
+ <form method="post" action="meals?action=filter">
+ <dl>
+ <dt>From Date:</dt>
+ <dd><input type="date" name="startDate" value="${param.startDate}"></dd>
+ </dl>
+ <dl>
+ <dt>To Date:</dt>
+ <dd><input type="date" name="endDate" value="${param.endDate}"></dd>
+ </dl>
+ <dl>
+ <dt>From Time:</dt>
+ <dd><input type="time" name="startTime" value="${param.startTime}"></dd>
+ </dl>
+ <dl>
+ <dt>To Time:</dt>
+ <dd><input type="time" name="endTime" value="${param.endTime}"></dd>
+ </dl>
+ <button type="submit">Filter</button>
+ </form>
+ <hr/>
<a href="meals?action=create">Add Meal</a>
<hr/>
<table border="1" cellpadding="8" cellspacing="0">
@@ -34,7 +45,7 @@
</thead>
<c:forEach items="${meals}" var="meal">
<jsp:useBean id="meal" scope="page" type="ru.javawebinar.topjava.to.MealWithExceed"/>
- <tr class="${meal.exceed ? 'exceeded' : 'normal'}">
+ <tr data-mealExceed="${meal.exceed}">
<td>
<%--${meal.dateTime.toLocalDate()} ${meal.dateTime.toLocalTime()}--%>
<%--<%=TimeUtil.toString(meal.getDateTime())%>--%>
Index: src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java (date 1531049529000)
+++ src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java (date 1531049556468)
@@ -1,7 +1,10 @@
package ru.javawebinar.topjava.util;
+import org.springframework.util.StringUtils;
+
import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class DateTimeUtil {
@@ -18,4 +21,12 @@
public static String toString(LocalDateTime ldt) {
return ldt == null ? "" : ldt.format(DATE_TIME_FORMATTER);
}
+
+ public static LocalDate parseLocalDate(String str) {
+ return StringUtils.isEmpty(str) ? null : LocalDate.parse(str);
+ }
+
+ public static LocalTime parseLocalTime(String str) {
+ return StringUtils.isEmpty(str) ? null : LocalTime.parse(str);
+ }
}