Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
2.2
  • Loading branch information
Slava committed Dec 7, 2016
commit f0b6bb22fe1013bcd25137953ddec2bad0002baa
29 changes: 29 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/Meal.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,33 @@
* 11.01.2015.
*/
public class Meal {
private Integer id;

private final LocalDateTime dateTime;

private final String description;

private final int calories;

public Meal(LocalDateTime dateTime, String description, int calories) {
this(null, dateTime, description, calories);
}

public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
this.id = id;
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public LocalDateTime getDateTime() {
return dateTime;
}
Expand All @@ -40,4 +55,18 @@ public LocalDate getDate() {
public LocalTime getTime() {
return dateTime.toLocalTime();
}

public boolean isNew() {
return id == null;
}

@Override
public String toString() {
return "Meal{" +
"id=" + id +
", dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
'}';
}
}
14 changes: 11 additions & 3 deletions src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* 11.01.2015.
*/
public class MealWithExceed {
private final Integer id;

private final LocalDateTime dateTime;

private final String description;
Expand All @@ -15,13 +17,18 @@ public class MealWithExceed {

private final boolean exceed;

public MealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
public MealWithExceed(Integer id, LocalDateTime dateTime, String description, int calories, boolean exceed) {
this.id = id;
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
this.exceed = exceed;
}

public Integer getId() {
return id;
}

public LocalDateTime getDateTime() {
return dateTime;
}
Expand All @@ -40,8 +47,9 @@ public boolean isExceed() {

@Override
public String toString() {
return "UserMealWithExceed{" +
"dateTime=" + dateTime +
return "MealWithExceed{" +
"id=" + id +
", dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
", exceed=" + exceed +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ru.javawebinar.topjava.repository;

import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.util.MealsUtil;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
* GKislin
* 15.09.2015.
*/
public class InMemoryMealRepositoryImpl implements MealRepository {
private Map<Integer, Meal> repository = new ConcurrentHashMap<>();
private AtomicInteger counter = new AtomicInteger(0);

{
MealsUtil.MEALS.forEach(this::save);
}

@Override
public Meal save(Meal meal) {
if (meal.isNew()) {
meal.setId(counter.incrementAndGet());
}
repository.put(meal.getId(), meal);
return meal;
}

@Override
public void delete(int id) {
repository.remove(id);
}

@Override
public Meal get(int id) {
return repository.get(id);
}

@Override
public Collection<Meal> getAll() {
return repository.values();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.javawebinar.topjava.repository;

import ru.javawebinar.topjava.model.Meal;

import java.util.Collection;

/**
* GKislin
* 06.03.2015.
*/
public interface MealRepository {
Meal save(Meal Meal);

void delete(int id);

Meal get(int id);

Collection<Meal> getAll();
}
6 changes: 3 additions & 3 deletions src/main/java/ru/javawebinar/topjava/util/MealsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static void main(String[] args) {
System.out.println(getFilteredWithExceededByCycle(MEALS, LocalTime.of(7, 0), LocalTime.of(12, 0), DEFAULT_CALORIES_PER_DAY));
}

public static List<MealWithExceed> getWithExceeded(List<Meal> meals, int caloriesPerDay) {
public static List<MealWithExceed> getWithExceeded(Collection<Meal> meals, int caloriesPerDay) {
return getFilteredWithExceeded(meals, LocalTime.MIN, LocalTime.MAX, caloriesPerDay);
}

public static List<MealWithExceed> getFilteredWithExceeded(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
public static List<MealWithExceed> getFilteredWithExceeded(Collection<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
Map<LocalDate, Integer> caloriesSumByDate = meals.stream()
.collect(
Collectors.groupingBy(Meal::getDate, Collectors.summingInt(Meal::getCalories))
Expand Down Expand Up @@ -65,6 +65,6 @@ public static List<MealWithExceed> getFilteredWithExceededByCycle(List<Meal> mea
}

public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) {
return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded);
return new MealWithExceed(meal.getId(), meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded);
}
}
57 changes: 54 additions & 3 deletions src/main/java/ru/javawebinar/topjava/web/MealServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.repository.InMemoryMealRepositoryImpl;
import ru.javawebinar.topjava.repository.MealRepository;
import ru.javawebinar.topjava.util.MealsUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
* User: gkislin
Expand All @@ -17,9 +23,54 @@
public class MealServlet extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(MealServlet.class);

private MealRepository repository;

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
repository = new InMemoryMealRepositoryImpl();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");

Meal meal = new Meal(id.isEmpty() ? null : Integer.valueOf(id),
LocalDateTime.parse(request.getParameter("dateTime")),
request.getParameter("description"),
Integer.valueOf(request.getParameter("calories")));

LOG.info(meal.isNew() ? "Create {}" : "Update {}", meal);
repository.save(meal);
response.sendRedirect("meals");
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LOG.info("getAll");
request.setAttribute("mealList", MealsUtil.getWithExceeded(MealsUtil.MEALS, MealsUtil.DEFAULT_CALORIES_PER_DAY));
request.getRequestDispatcher("/mealList.jsp").forward(request, response);
String action = request.getParameter("action");

if (action == null) {
LOG.info("getAll");
request.setAttribute("mealList",
MealsUtil.getWithExceeded(repository.getAll(), MealsUtil.DEFAULT_CALORIES_PER_DAY));
request.getRequestDispatcher("/mealList.jsp").forward(request, response);

} else if ("delete".equals(action)) {
int id = getId(request);
LOG.info("Delete {}", id);
repository.delete(id);
response.sendRedirect("meals");

} else if ("create".equals(action) || "update".equals(action)) {
final Meal meal = action.equals("create") ?
new Meal(LocalDateTime.now().withNano(0).withSecond(0), "", 1000) :
repository.get(getId(request));
request.setAttribute("meal", meal);
request.getRequestDispatcher("mealEdit.jsp").forward(request, response);
}
}

private int getId(HttpServletRequest request) {
String paramId = Objects.requireNonNull(request.getParameter("id"));
return Integer.valueOf(paramId);
}
}
51 changes: 51 additions & 0 deletions src/main/webapp/mealEdit.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<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>
</head>
<body>
<section>
<h2><a href="index.html">Home</a></h2>
<h3>${param.action == 'create' ? 'Create meal' : 'Edit meal'}</h3>
<hr>
<jsp:useBean id="meal" type="ru.javawebinar.topjava.model.Meal" scope="request"/>
<form method="post" action="meals">
<input type="hidden" name="id" value="${meal.id}">
<dl>
<dt>DateTime:</dt>
<dd><input type="datetime-local" value="${meal.dateTime}" name="dateTime"></dd>
</dl>
<dl>
<dt>Description:</dt>
<dd><input type="text" value="${meal.description}" size=40 name="description"></dd>
</dl>
<dl>
<dt>Calories:</dt>
<dd><input type="number" value="${meal.calories}" name="calories"></dd>
</dl>
<button type="submit">Save</button>
<button onclick="window.history.back()">Cancel</button>
</form>
</section>
</body>
</html>
5 changes: 5 additions & 0 deletions src/main/webapp/mealList.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
<section>
<h2><a href="index.html">Home</a></h2>
<h3>Meal list</h3>
<a href="meals?action=create">Add Meal</a>
<hr>
<table border="1" cellpadding="8" cellspacing="0">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th>Calories</th>
<th></th>
<th></th>
</tr>
</thead>
<c:forEach items="${mealList}" var="meal">
Expand All @@ -38,6 +41,8 @@
</td>
<td>${meal.description}</td>
<td>${meal.calories}</td>
<td><a href="meals?action=update&id=${meal.id}">Update</a></td>
<td><a href="meals?action=delete&id=${meal.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
Expand Down