diff --git a/src/main/java/ru/javawebinar/topjava/model/Meal.java b/src/main/java/ru/javawebinar/topjava/model/Meal.java index 50a2c1d6a..943ff5cd5 100644 --- a/src/main/java/ru/javawebinar/topjava/model/Meal.java +++ b/src/main/java/ru/javawebinar/topjava/model/Meal.java @@ -1,67 +1,17 @@ package ru.javawebinar.topjava.model; -import com.fasterxml.jackson.annotation.JsonIgnore; -import org.hibernate.annotations.OnDelete; -import org.hibernate.annotations.OnDeleteAction; -import org.hibernate.validator.constraints.Range; -import org.springframework.format.annotation.DateTimeFormat; -import ru.javawebinar.topjava.View; -import ru.javawebinar.topjava.util.DateTimeUtil; -import ru.javawebinar.topjava.util.validation.NoHtml; - -import javax.persistence.*; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; -@NamedQueries({ - @NamedQuery(name = Meal.ALL_SORTED, query = "SELECT m FROM Meal m WHERE m.user.id=:userId ORDER BY m.dateTime DESC"), - @NamedQuery(name = Meal.DELETE, query = "DELETE FROM Meal m WHERE m.id=:id AND m.user.id=:userId"), - @NamedQuery(name = Meal.GET_BETWEEN, query = """ - SELECT m FROM Meal m - WHERE m.user.id=:userId AND m.dateTime >= :startDateTime AND m.dateTime < :endDateTime ORDER BY m.dateTime DESC - """), -// @NamedQuery(name = Meal.UPDATE, query = "UPDATE Meal m SET m.dateTime = :datetime, m.calories= :calories," + -// "m.description=:desc where m.id=:id and m.user.id=:userId") -}) -@Entity -@Table(name = "meal", uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "date_time"}, name = "meal_unique_user_datetime_idx")}) -public class Meal extends AbstractBaseEntity { - public static final String ALL_SORTED = "Meal.getAll"; - public static final String DELETE = "Meal.delete"; - public static final String GET_BETWEEN = "Meal.getBetween"; - - @Column(name = "date_time", nullable = false) - @NotNull - @DateTimeFormat(pattern = DateTimeUtil.DATE_TIME_PATTERN) - private LocalDateTime dateTime; - - @Column(name = "description", nullable = false) - @NotBlank - @Size(min = 2, max = 120) - @NoHtml(groups = {View.Web.class}) - private String description; +public class Meal { + private final LocalDateTime dateTime; - @Column(name = "calories", nullable = false) - @NotNull - @Range(min = 10, max = 5000) - private Integer calories; + private final String description; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", nullable = false) - @OnDelete(action = OnDeleteAction.CASCADE) - @JsonIgnore - @NotNull(groups = View.Persist.class) - private User user; - - public Meal() { - } + private final int calories; - public Meal(Integer id, LocalDateTime dateTime, String description, int calories) { - super(id); + public Meal(LocalDateTime dateTime, String description, int calories) { this.dateTime = dateTime; this.description = description; this.calories = calories; @@ -86,34 +36,4 @@ public LocalDate getDate() { public LocalTime getTime() { return dateTime.toLocalTime(); } - - public void setDateTime(LocalDateTime dateTime) { - this.dateTime = dateTime; - } - - public void setDescription(String description) { - this.description = description; - } - - public void setCalories(Integer calories) { - this.calories = calories; - } - - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - - @Override - public String toString() { - return "Meal{" + - "id=" + id + - ", dateTime=" + dateTime + - ", description='" + description + '\'' + - ", calories=" + calories + - '}'; - } } diff --git a/src/main/java/ru/javawebinar/topjava/model/MealTo.java b/src/main/java/ru/javawebinar/topjava/model/MealTo.java new file mode 100644 index 000000000..07f04f8db --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/model/MealTo.java @@ -0,0 +1,30 @@ +package ru.javawebinar.topjava.model; + +import java.time.LocalDateTime; + +public class MealTo { + private final LocalDateTime dateTime; + + private final String description; + + private final int calories; + + private final boolean excess; + + public MealTo(LocalDateTime dateTime, String description, int calories, boolean excess) { + this.dateTime = dateTime; + this.description = description; + this.calories = calories; + this.excess = excess; + } + + @Override + public String toString() { + return "MealTo{" + + "dateTime=" + dateTime + + ", description='" + description + '\'' + + ", calories=" + calories + + ", excess=" + excess + + '}'; + } +} diff --git a/src/main/java/ru/javawebinar/topjava/to/MealTo.java b/src/main/java/ru/javawebinar/topjava/to/MealTo.java deleted file mode 100644 index 059f14a44..000000000 --- a/src/main/java/ru/javawebinar/topjava/to/MealTo.java +++ /dev/null @@ -1,69 +0,0 @@ -package ru.javawebinar.topjava.to; - -import java.beans.ConstructorProperties; -import java.time.LocalDateTime; -import java.util.Objects; - -public class MealTo extends BaseTo { - - private final LocalDateTime dateTime; - - private final String description; - - private final int calories; - - private final boolean excess; - - @ConstructorProperties({"id", "dateTime", "description", "calories", "excess"}) - public MealTo(Integer id, LocalDateTime dateTime, String description, int calories, boolean excess) { - super(id); - this.dateTime = dateTime; - this.description = description; - this.calories = calories; - this.excess = excess; - } - - public LocalDateTime getDateTime() { - return dateTime; - } - - public String getDescription() { - return description; - } - - public int getCalories() { - return calories; - } - - public boolean isExcess() { - return excess; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - MealTo mealTo = (MealTo) o; - return calories == mealTo.calories && - excess == mealTo.excess && - Objects.equals(id, mealTo.id) && - Objects.equals(dateTime, mealTo.dateTime) && - Objects.equals(description, mealTo.description); - } - - @Override - public int hashCode() { - return Objects.hash(id, dateTime, description, calories, excess); - } - - @Override - public String toString() { - return "MealTo{" + - "id=" + id + - ", dateTime=" + dateTime + - ", description='" + description + '\'' + - ", calories=" + calories + - ", excess=" + excess + - '}'; - } -} diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java index af07e74d3..c29e1fbbb 100644 --- a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java @@ -1,30 +1,34 @@ package ru.javawebinar.topjava.util; import ru.javawebinar.topjava.model.Meal; -import ru.javawebinar.topjava.to.MealTo; +import ru.javawebinar.topjava.model.MealTo; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.LocalTime; -import java.util.Collection; +import java.time.Month; +import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.function.Predicate; import java.util.stream.Collectors; public class MealsUtil { - - private MealsUtil() { - } - - public static List getTos(Collection meals, int caloriesPerDay) { - return filterByPredicate(meals, caloriesPerDay, meal -> true); - } - - public static List getFilteredTos(Collection meals, int caloriesPerDay, LocalTime startTime, LocalTime endTime) { - return filterByPredicate(meals, caloriesPerDay, meal -> Util.isBetweenHalfOpen(meal.getTime(), startTime, endTime)); + public static void main(String[] args) { + List meals = Arrays.asList( + new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 10, 0), "Завтрак", 500), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 13, 0), "Обед", 1000), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 20, 0), "Ужин", 500), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 0, 0), "Еда на граничное значение", 100), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 10, 0), "Завтрак", 1000), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 13, 0), "Обед", 500), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 20, 0), "Ужин", 410) + ); + + List mealsTo = filteredByStreams(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); + mealsTo.forEach(System.out::println); } - private static List filterByPredicate(Collection meals, int caloriesPerDay, Predicate filter) { + public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { Map caloriesSumByDate = meals.stream() .collect( Collectors.groupingBy(Meal::getDate, Collectors.summingInt(Meal::getCalories)) @@ -32,12 +36,12 @@ private static List filterByPredicate(Collection meals, int calori ); return meals.stream() - .filter(filter) + .filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getTime(), startTime, endTime)) .map(meal -> createTo(meal, caloriesSumByDate.get(meal.getDate()) > caloriesPerDay)) - .toList(); + .collect(Collectors.toList()); } - public static MealTo createTo(Meal meal, boolean excess) { - return new MealTo(meal.getId(), meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess); + private static MealTo createTo(Meal meal, boolean excess) { + return new MealTo(meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess); } } diff --git a/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java b/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java new file mode 100644 index 000000000..550b590c9 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java @@ -0,0 +1,9 @@ +package ru.javawebinar.topjava.util; + +import java.time.LocalTime; + +public class TimeUtil { + public static boolean isBetweenHalfOpen(LocalTime lt, LocalTime startTime, LocalTime endTime) { + return !lt.isBefore(startTime) && lt.isBefore(endTime); + } +} diff --git a/src/main/java/ru/javawebinar/topjava/web/UserServlet.java b/src/main/java/ru/javawebinar/topjava/web/UserServlet.java new file mode 100644 index 000000000..dcbb1c294 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/web/UserServlet.java @@ -0,0 +1,16 @@ +package ru.javawebinar.topjava.web; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.io.IOException; + +public class UserServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.getRequestDispatcher("/users.jsp").forward(request, response); + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 558c28967..e576c30a3 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -6,65 +6,13 @@ TopJava - - spring.profiles.default - postgres,datajpa - - - - contextConfigLocation - - classpath:spring/spring-app.xml - classpath:spring/spring-db.xml - - - - - - org.springframework.web.context.ContextLoaderListener - - mvc-dispatcher - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - classpath:spring/spring-mvc.xml - - - throwExceptionIfNoHandlerFound - true - - 1 + userServlet + ru.javawebinar.topjava.web.UserServlet + 0 - mvc-dispatcher - / + userServlet + /users - - - encodingFilter - org.springframework.web.filter.CharacterEncodingFilter - - encoding - UTF-8 - - - forceEncoding - true - - - - encodingFilter - /* - - - - - springSecurityFilterChain - org.springframework.web.filter.DelegatingFilterProxy - - - springSecurityFilterChain - /* - diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html new file mode 100644 index 000000000..921d3b472 --- /dev/null +++ b/src/main/webapp/index.html @@ -0,0 +1,13 @@ + + + + Java Enterprise (Topjava) + + +

Проект Java Enterprise (Topjava)

+
+ + + diff --git a/src/main/webapp/users.jsp b/src/main/webapp/users.jsp new file mode 100644 index 000000000..650c8dda4 --- /dev/null +++ b/src/main/webapp/users.jsp @@ -0,0 +1,11 @@ +<%@ page contentType="text/html;charset=UTF-8" %> + + + Users + + +

Home

+
+

Users

+ + \ No newline at end of file