{"action":"created","comment":{"url":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/comments/111629864","pull_request_review_id":32886561,"id":111629864,"diff_hunk":"@@ -1,37 +1,84 @@\n package ru.javawebinar.topjava.repository.jdbc;\n \n+import org.springframework.beans.factory.annotation.Autowired;\n+import org.springframework.dao.support.DataAccessUtils;\n+import org.springframework.jdbc.core.BeanPropertyRowMapper;\n+import org.springframework.jdbc.core.JdbcTemplate;\n+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;\n+import org.springframework.jdbc.core.simple.SimpleJdbcInsert;\n import org.springframework.stereotype.Repository;\n import ru.javawebinar.topjava.model.Meal;\n import ru.javawebinar.topjava.repository.MealRepository;\n+import ru.javawebinar.topjava.util.DateTimeUtil;\n \n+import javax.sql.DataSource;\n import java.time.LocalDateTime;\n+import java.util.Comparator;\n import java.util.List;\n+import java.util.stream.Collectors;\n+import java.util.stream.Stream;\n \n @Repository\n public class JdbcMealRepositoryImpl implements MealRepository {\n+    private static final BeanPropertyRowMapper<Meal> ROW_MAPPER = BeanPropertyRowMapper.newInstance(Meal.class);\n+    private static final Comparator<Meal> MEAL_COMPARATOR = Comparator.comparing(Meal::getDateTime).reversed();\n+\n+    private final JdbcTemplate jdbcTemplate;\n+    private final SimpleJdbcInsert insertMeal;\n+\n+    @Autowired\n+    public JdbcMealRepositoryImpl(DataSource dataSource, JdbcTemplate jdbcTemplate) {\n+        this.insertMeal = new SimpleJdbcInsert(dataSource)\n+                                .withTableName(\"meals\")\n+                                .usingGeneratedKeyColumns(\"id\");\n+        this.jdbcTemplate = jdbcTemplate;\n+    }\n \n     @Override\n     public Meal save(Meal meal, int userId) {\n-        return null;\n+        MapSqlParameterSource map = new MapSqlParameterSource()\n+                .addValue(\"id\", meal.getId())\n+                .addValue(\"datetime\", meal.getDateTime())\n+                .addValue(\"description\", meal.getDescription())\n+                .addValue(\"calories\", meal.getCalories())\n+                .addValue(\"user_id\", userId);\n+\n+        if (meal.isNew()) {\n+            Number newKey = insertMeal.executeAndReturnKey(map);\n+            meal.setId(newKey.intValue());\n+        }\n+        return jdbcTemplate.update(\"UPDATE meals SET user_id=?, description=?, datetime=?, calories=? WHERE id=? AND user_id=?\",\n+                userId, meal.getDescription(), meal.getDateTime(), meal.getCalories(), meal.getId(), userId) == 0 ? null : meal;\n     }\n \n     @Override\n     public boolean delete(int id, int userId) {\n-        return false;\n+        return jdbcTemplate.update(\"DELETE FROM meals WHERE user_id = ? AND id = ?\", userId, id) != 0;\n     }\n \n     @Override\n     public Meal get(int id, int userId) {\n-        return null;\n+        List<Meal> list = jdbcTemplate.query(\"SELECT * FROM meals WHERE user_id = ? AND id = ?\", ROW_MAPPER, userId, id);\n+        return DataAccessUtils.singleResult(list);\n     }\n \n     @Override\n     public List<Meal> getAll(int userId) {\n-        return null;\n+        return getAllAsStream(userId)\n+                .sorted(MEAL_COMPARATOR)\n+                .collect(Collectors.toList());\n     }\n \n     @Override\n     public List<Meal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId) {\n-        return null;\n+        return getAllAsStream(userId)\n+                .filter(meal -> DateTimeUtil.isBetween(meal.getDateTime(), startDate, endDate))","path":"src/main/java/ru/javawebinar/topjava/repository/jdbc/JdbcMealRepositoryImpl.java","position":null,"original_position":80,"commit_id":"859c0e3d9efb9f750f42b5c8b15beead94984e02","original_commit_id":"859c0e3d9efb9f750f42b5c8b15beead94984e02","user":{"login":"truefirefox","id":15744097,"avatar_url":"https://avatars1.githubusercontent.com/u/15744097?v=3","gravatar_id":"","url":"https://api.github.com/users/truefirefox","html_url":"https://github.com/truefirefox","followers_url":"https://api.github.com/users/truefirefox/followers","following_url":"https://api.github.com/users/truefirefox/following{/other_user}","gists_url":"https://api.github.com/users/truefirefox/gists{/gist_id}","starred_url":"https://api.github.com/users/truefirefox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/truefirefox/subscriptions","organizations_url":"https://api.github.com/users/truefirefox/orgs","repos_url":"https://api.github.com/users/truefirefox/repos","events_url":"https://api.github.com/users/truefirefox/events{/privacy}","received_events_url":"https://api.github.com/users/truefirefox/received_events","type":"User","site_admin":false},"body":"делай сразу запрос с учетом дат и сортировки","created_at":"2017-04-14T20:16:40Z","updated_at":"2017-04-14T20:26:26Z","html_url":"https://github.com/JavaWebinar/topjava10/pull/3#discussion_r111629864","pull_request_url":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/comments/111629864"},"html":{"href":"https://github.com/JavaWebinar/topjava10/pull/3#discussion_r111629864"},"pull_request":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3","id":115718956,"html_url":"https://github.com/JavaWebinar/topjava10/pull/3","diff_url":"https://github.com/JavaWebinar/topjava10/pull/3.diff","patch_url":"https://github.com/JavaWebinar/topjava10/pull/3.patch","issue_url":"https://api.github.com/repos/JavaWebinar/topjava10/issues/3","number":3,"state":"open","locked":false,"title":"Hw3","user":{"login":"elusive935","id":25685867,"avatar_url":"https://avatars3.githubusercontent.com/u/25685867?v=3","gravatar_id":"","url":"https://api.github.com/users/elusive935","html_url":"https://github.com/elusive935","followers_url":"https://api.github.com/users/elusive935/followers","following_url":"https://api.github.com/users/elusive935/following{/other_user}","gists_url":"https://api.github.com/users/elusive935/gists{/gist_id}","starred_url":"https://api.github.com/users/elusive935/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elusive935/subscriptions","organizations_url":"https://api.github.com/users/elusive935/orgs","repos_url":"https://api.github.com/users/elusive935/repos","events_url":"https://api.github.com/users/elusive935/events{/privacy}","received_events_url":"https://api.github.com/users/elusive935/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-04-13T09:40:35Z","updated_at":"2017-04-14T20:26:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3/commits","review_comments_url":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3/comments","review_comment_url":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/comments{/number}","comments_url":"https://api.github.com/repos/JavaWebinar/topjava10/issues/3/comments","statuses_url":"https://api.github.com/repos/JavaWebinar/topjava10/statuses/859c0e3d9efb9f750f42b5c8b15beead94984e02","head":{"label":"elusive935:HW3","ref":"HW3","sha":"859c0e3d9efb9f750f42b5c8b15beead94984e02","user":{"login":"elusive935","id":25685867,"avatar_url":"https://avatars3.githubusercontent.com/u/25685867?v=3","gravatar_id":"","url":"https://api.github.com/users/elusive935","html_url":"https://github.com/elusive935","followers_url":"https://api.github.com/users/elusive935/followers","following_url":"https://api.github.com/users/elusive935/following{/other_user}","gists_url":"https://api.github.com/users/elusive935/gists{/gist_id}","starred_url":"https://api.github.com/users/elusive935/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elusive935/subscriptions","organizations_url":"https://api.github.com/users/elusive935/orgs","repos_url":"https://api.github.com/users/elusive935/repos","events_url":"https://api.github.com/users/elusive935/events{/privacy}","received_events_url":"https://api.github.com/users/elusive935/received_events","type":"User","site_admin":false},"repo":{"id":87613355,"name":"topjava10","full_name":"elusive935/topjava10","owner":{"login":"elusive935","id":25685867,"avatar_url":"https://avatars3.githubusercontent.com/u/25685867?v=3","gravatar_id":"","url":"https://api.github.com/users/elusive935","html_url":"https://github.com/elusive935","followers_url":"https://api.github.com/users/elusive935/followers","following_url":"https://api.github.com/users/elusive935/following{/other_user}","gists_url":"https://api.github.com/users/elusive935/gists{/gist_id}","starred_url":"https://api.github.com/users/elusive935/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elusive935/subscriptions","organizations_url":"https://api.github.com/users/elusive935/orgs","repos_url":"https://api.github.com/users/elusive935/repos","events_url":"https://api.github.com/users/elusive935/events{/privacy}","received_events_url":"https://api.github.com/users/elusive935/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/elusive935/topjava10","description":null,"fork":true,"url":"https://api.github.com/repos/elusive935/topjava10","forks_url":"https://api.github.com/repos/elusive935/topjava10/forks","keys_url":"https://api.github.com/repos/elusive935/topjava10/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elusive935/topjava10/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elusive935/topjava10/teams","hooks_url":"https://api.github.com/repos/elusive935/topjava10/hooks","issue_events_url":"https://api.github.com/repos/elusive935/topjava10/issues/events{/number}","events_url":"https://api.github.com/repos/elusive935/topjava10/events","assignees_url":"https://api.github.com/repos/elusive935/topjava10/assignees{/user}","branches_url":"https://api.github.com/repos/elusive935/topjava10/branches{/branch}","tags_url":"https://api.github.com/repos/elusive935/topjava10/tags","blobs_url":"https://api.github.com/repos/elusive935/topjava10/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elusive935/topjava10/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elusive935/topjava10/git/refs{/sha}","trees_url":"https://api.github.com/repos/elusive935/topjava10/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elusive935/topjava10/statuses/{sha}","languages_url":"https://api.github.com/repos/elusive935/topjava10/languages","stargazers_url":"https://api.github.com/repos/elusive935/topjava10/stargazers","contributors_url":"https://api.github.com/repos/elusive935/topjava10/contributors","subscribers_url":"https://api.github.com/repos/elusive935/topjava10/subscribers","subscription_url":"https://api.github.com/repos/elusive935/topjava10/subscription","commits_url":"https://api.github.com/repos/elusive935/topjava10/commits{/sha}","git_commits_url":"https://api.github.com/repos/elusive935/topjava10/git/commits{/sha}","comments_url":"https://api.github.com/repos/elusive935/topjava10/comments{/number}","issue_comment_url":"https://api.github.com/repos/elusive935/topjava10/issues/comments{/number}","contents_url":"https://api.github.com/repos/elusive935/topjava10/contents/{+path}","compare_url":"https://api.github.com/repos/elusive935/topjava10/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elusive935/topjava10/merges","archive_url":"https://api.github.com/repos/elusive935/topjava10/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elusive935/topjava10/downloads","issues_url":"https://api.github.com/repos/elusive935/topjava10/issues{/number}","pulls_url":"https://api.github.com/repos/elusive935/topjava10/pulls{/number}","milestones_url":"https://api.github.com/repos/elusive935/topjava10/milestones{/number}","notifications_url":"https://api.github.com/repos/elusive935/topjava10/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elusive935/topjava10/labels{/name}","releases_url":"https://api.github.com/repos/elusive935/topjava10/releases{/id}","deployments_url":"https://api.github.com/repos/elusive935/topjava10/deployments","created_at":"2017-04-08T06:29:09Z","updated_at":"2017-04-08T06:29:11Z","pushed_at":"2017-04-14T05:45:32Z","git_url":"git://github.com/elusive935/topjava10.git","ssh_url":"git@github.com:elusive935/topjava10.git","clone_url":"https://github.com/elusive935/topjava10.git","svn_url":"https://github.com/elusive935/topjava10","homepage":null,"size":145,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"JavaWebinar:master","ref":"master","sha":"6fba93e8e9ace1bf0834beb14b82a58df83608f5","user":{"login":"JavaWebinar","id":11200258,"avatar_url":"https://avatars2.githubusercontent.com/u/11200258?v=3","gravatar_id":"","url":"https://api.github.com/users/JavaWebinar","html_url":"https://github.com/JavaWebinar","followers_url":"https://api.github.com/users/JavaWebinar/followers","following_url":"https://api.github.com/users/JavaWebinar/following{/other_user}","gists_url":"https://api.github.com/users/JavaWebinar/gists{/gist_id}","starred_url":"https://api.github.com/users/JavaWebinar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JavaWebinar/subscriptions","organizations_url":"https://api.github.com/users/JavaWebinar/orgs","repos_url":"https://api.github.com/users/JavaWebinar/repos","events_url":"https://api.github.com/users/JavaWebinar/events{/privacy}","received_events_url":"https://api.github.com/users/JavaWebinar/received_events","type":"User","site_admin":false},"repo":{"id":85123351,"name":"topjava10","full_name":"JavaWebinar/topjava10","owner":{"login":"JavaWebinar","id":11200258,"avatar_url":"https://avatars2.githubusercontent.com/u/11200258?v=3","gravatar_id":"","url":"https://api.github.com/users/JavaWebinar","html_url":"https://github.com/JavaWebinar","followers_url":"https://api.github.com/users/JavaWebinar/followers","following_url":"https://api.github.com/users/JavaWebinar/following{/other_user}","gists_url":"https://api.github.com/users/JavaWebinar/gists{/gist_id}","starred_url":"https://api.github.com/users/JavaWebinar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JavaWebinar/subscriptions","organizations_url":"https://api.github.com/users/JavaWebinar/orgs","repos_url":"https://api.github.com/users/JavaWebinar/repos","events_url":"https://api.github.com/users/JavaWebinar/events{/privacy}","received_events_url":"https://api.github.com/users/JavaWebinar/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/JavaWebinar/topjava10","description":null,"fork":false,"url":"https://api.github.com/repos/JavaWebinar/topjava10","forks_url":"https://api.github.com/repos/JavaWebinar/topjava10/forks","keys_url":"https://api.github.com/repos/JavaWebinar/topjava10/keys{/key_id}","collaborators_url":"https://api.github.com/repos/JavaWebinar/topjava10/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/JavaWebinar/topjava10/teams","hooks_url":"https://api.github.com/repos/JavaWebinar/topjava10/hooks","issue_events_url":"https://api.github.com/repos/JavaWebinar/topjava10/issues/events{/number}","events_url":"https://api.github.com/repos/JavaWebinar/topjava10/events","assignees_url":"https://api.github.com/repos/JavaWebinar/topjava10/assignees{/user}","branches_url":"https://api.github.com/repos/JavaWebinar/topjava10/branches{/branch}","tags_url":"https://api.github.com/repos/JavaWebinar/topjava10/tags","blobs_url":"https://api.github.com/repos/JavaWebinar/topjava10/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/JavaWebinar/topjava10/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/JavaWebinar/topjava10/git/refs{/sha}","trees_url":"https://api.github.com/repos/JavaWebinar/topjava10/git/trees{/sha}","statuses_url":"https://api.github.com/repos/JavaWebinar/topjava10/statuses/{sha}","languages_url":"https://api.github.com/repos/JavaWebinar/topjava10/languages","stargazers_url":"https://api.github.com/repos/JavaWebinar/topjava10/stargazers","contributors_url":"https://api.github.com/repos/JavaWebinar/topjava10/contributors","subscribers_url":"https://api.github.com/repos/JavaWebinar/topjava10/subscribers","subscription_url":"https://api.github.com/repos/JavaWebinar/topjava10/subscription","commits_url":"https://api.github.com/repos/JavaWebinar/topjava10/commits{/sha}","git_commits_url":"https://api.github.com/repos/JavaWebinar/topjava10/git/commits{/sha}","comments_url":"https://api.github.com/repos/JavaWebinar/topjava10/comments{/number}","issue_comment_url":"https://api.github.com/repos/JavaWebinar/topjava10/issues/comments{/number}","contents_url":"https://api.github.com/repos/JavaWebinar/topjava10/contents/{+path}","compare_url":"https://api.github.com/repos/JavaWebinar/topjava10/compare/{base}...{head}","merges_url":"https://api.github.com/repos/JavaWebinar/topjava10/merges","archive_url":"https://api.github.com/repos/JavaWebinar/topjava10/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/JavaWebinar/topjava10/downloads","issues_url":"https://api.github.com/repos/JavaWebinar/topjava10/issues{/number}","pulls_url":"https://api.github.com/repos/JavaWebinar/topjava10/pulls{/number}","milestones_url":"https://api.github.com/repos/JavaWebinar/topjava10/milestones{/number}","notifications_url":"https://api.github.com/repos/JavaWebinar/topjava10/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/JavaWebinar/topjava10/labels{/name}","releases_url":"https://api.github.com/repos/JavaWebinar/topjava10/releases{/id}","deployments_url":"https://api.github.com/repos/JavaWebinar/topjava10/deployments","created_at":"2017-03-15T21:37:54Z","updated_at":"2017-04-05T10:20:49Z","pushed_at":"2017-04-13T18:37:27Z","git_url":"git://github.com/JavaWebinar/topjava10.git","ssh_url":"git@github.com:JavaWebinar/topjava10.git","clone_url":"https://github.com/JavaWebinar/topjava10.git","svn_url":"https://github.com/JavaWebinar/topjava10","homepage":null,"size":135,"stargazers_count":1,"watchers_count":1,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":58,"mirror_url":null,"open_issues_count":2,"forks":58,"open_issues":2,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3"},"html":{"href":"https://github.com/JavaWebinar/topjava10/pull/3"},"issue":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/issues/3"},"comments":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/JavaWebinar/topjava10/statuses/859c0e3d9efb9f750f42b5c8b15beead94984e02"}}}}
{"actor":{"display_login":"truefirefox"}}