-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatisticsQueryBuilder.java
More file actions
43 lines (36 loc) · 1.5 KB
/
StatisticsQueryBuilder.java
File metadata and controls
43 lines (36 loc) · 1.5 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
package lv.ctco.springboottemplate.features.statistics;
import lv.ctco.springboottemplate.features.statistics.models.StatisticsFormat;
import lv.ctco.springboottemplate.features.statistics.models.StatisticsQuery;
import lv.ctco.springboottemplate.features.statistics.models.StatisticsRequest;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
@Component
public class StatisticsQueryBuilder {
public StatisticsQuery build(StatisticsRequest request) {
List<String> errors = new ArrayList<>();
LocalDate from = parseDate("from", request.from(), errors);
LocalDate to = parseDate("to", request.to(), errors);
if (from != null && to != null && from.isAfter(to)) {
errors.add("Parameter 'from' must be before or equal to 'to'.");
}
if (!errors.isEmpty()) {
throw new IllegalArgumentException(String.join("; ", errors));
}
StatisticsFormat format = StatisticsQuery.parseFormat(request.format());
return new StatisticsQuery(from, to, format);
}
private LocalDate parseDate(String name, String raw, List<String> errors) {
if (raw == null || raw.isBlank()) {
return null;
}
try {
return LocalDate.parse(raw);
} catch (DateTimeParseException e) {
errors.add("Invalid '" + name + "' date: " + raw);
return null;
}
}
}