Skip to content

Commit ca17128

Browse files
committed
8_03_HW07_formatters
1 parent ad95b8c commit ca17128

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.javawebinar.topjava.web.converter;
2+
3+
import org.springframework.format.Formatter;
4+
5+
import java.time.LocalDate;
6+
import java.time.LocalTime;
7+
import java.time.format.DateTimeFormatter;
8+
import java.util.Locale;
9+
10+
public class DateTimeFormatters {
11+
public static class LocalDateFormatter implements Formatter<LocalDate> {
12+
13+
@Override
14+
public LocalDate parse(String text, Locale locale) {
15+
return LocalDate.parse(text);
16+
}
17+
18+
@Override
19+
public String print(LocalDate lt, Locale locale) {
20+
return lt.format(DateTimeFormatter.ISO_LOCAL_DATE);
21+
}
22+
}
23+
24+
public static class LocalTimeFormatter implements Formatter<LocalTime> {
25+
26+
@Override
27+
public LocalTime parse(String text, Locale locale) {
28+
return LocalTime.parse(text);
29+
}
30+
31+
@Override
32+
public String print(LocalTime lt, Locale locale) {
33+
return lt.format(DateTimeFormatter.ISO_LOCAL_TIME);
34+
}
35+
}
36+
}

src/main/java/ru/javawebinar/topjava/web/meal/MealRestController.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package ru.javawebinar.topjava.web.meal;
22

3-
import org.springframework.format.annotation.DateTimeFormat;
43
import org.springframework.http.HttpStatus;
54
import org.springframework.http.MediaType;
65
import org.springframework.http.ResponseEntity;
6+
import org.springframework.lang.Nullable;
77
import org.springframework.web.bind.annotation.*;
88
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
99
import ru.javawebinar.topjava.model.Meal;
1010
import ru.javawebinar.topjava.to.MealTo;
1111

1212
import java.net.URI;
13-
import java.time.LocalDateTime;
13+
import java.time.LocalDate;
14+
import java.time.LocalTime;
1415
import java.util.List;
1516

1617
@RestController
@@ -55,10 +56,13 @@ public ResponseEntity<Meal> createWithLocation(@RequestBody Meal meal) {
5556
return ResponseEntity.created(uriOfNewResource).body(created);
5657
}
5758

58-
@GetMapping("/between")
59+
@Override
60+
@GetMapping("/filter")
5961
public List<MealTo> getBetween(
60-
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDateTime,
61-
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDateTime) {
62-
return super.getBetween(startDateTime.toLocalDate(), startDateTime.toLocalTime(), endDateTime.toLocalDate(), endDateTime.toLocalTime());
62+
@RequestParam @Nullable LocalDate startDate,
63+
@RequestParam @Nullable LocalTime startTime,
64+
@RequestParam @Nullable LocalDate endDate,
65+
@RequestParam @Nullable LocalTime endTime) {
66+
return super.getBetween(startDate, startTime, endDate, endTime);
6367
}
6468
}

src/main/resources/spring/spring-mvc.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<bean class="ru.javawebinar.topjava.web.json.JacksonObjectMapper" id="objectMapper" factory-method="getMapper"/>
99

10-
<mvc:annotation-driven>
10+
<mvc:annotation-driven conversion-service="conversionService">
1111
<mvc:message-converters>
1212
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
1313
<property name="objectMapper" ref="objectMapper"/>
@@ -23,6 +23,15 @@
2323
</mvc:message-converters>
2424
</mvc:annotation-driven>
2525

26+
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
27+
<property name="formatters">
28+
<set>
29+
<bean class="ru.javawebinar.topjava.web.converter.DateTimeFormatters.LocalTimeFormatter"/>
30+
<bean class="ru.javawebinar.topjava.web.converter.DateTimeFormatters.LocalDateFormatter"/>
31+
</set>
32+
</property>
33+
</bean>
34+
2635
<context:component-scan base-package="ru.javawebinar.**.web"/>
2736

2837
<!-- all resources inside folder src/main/webapp/resources are mapped so they can be referred to inside JSP files -->

src/test/java/ru/javawebinar/topjava/web/meal/MealRestControllerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,18 @@ void getAll() throws Exception {
8181

8282
@Test
8383
void getBetween() throws Exception {
84-
perform(MockMvcRequestBuilders.get(REST_URL + "between?startDateTime=2020-01-30T07:00&endDateTime=2020-01-31T11:00:00"))
84+
perform(MockMvcRequestBuilders.get(REST_URL + "filter")
85+
.param("startDate", "2020-01-30").param("startTime", "07:00")
86+
.param("endDate", "2020-01-31").param("endTime", "11:00"))
8587
.andExpect(status().isOk())
8688
.andDo(print())
8789
.andExpect(TO_MATCHER.contentJson(createTo(meal5, true), createTo(meal1, false)));
8890
}
91+
92+
@Test
93+
void getBetweenAll() throws Exception {
94+
perform(MockMvcRequestBuilders.get(REST_URL + "filter?startDate=&endTime="))
95+
.andExpect(status().isOk())
96+
.andExpect(TO_MATCHER.contentJson(getTos(meals, user.getCaloriesPerDay())));
97+
}
8998
}

0 commit comments

Comments
 (0)