4
4
import org .springframework .jdbc .core .BeanPropertyRowMapper ;
5
5
import org .springframework .jdbc .core .JdbcTemplate ;
6
6
import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
7
+ import org .springframework .jdbc .core .namedparam .NamedParameterJdbcTemplate ;
7
8
import org .springframework .jdbc .core .simple .SimpleJdbcInsert ;
8
9
import org .springframework .stereotype .Repository ;
9
10
import ru .javawebinar .topjava .model .User ;
10
11
import ru .javawebinar .topjava .model .UserMeal ;
11
12
import ru .javawebinar .topjava .repository .UserMealRepository ;
12
13
13
14
import javax .sql .DataSource ;
15
+ import java .sql .Timestamp ;
14
16
import java .time .LocalDateTime ;
15
17
import java .util .List ;
16
18
@@ -26,6 +28,9 @@ public class JdbcUserMealRepositoryImpl implements UserMealRepository {
26
28
@ Autowired
27
29
private JdbcTemplate jdbcTemplate ;
28
30
31
+ @ Autowired
32
+ private NamedParameterJdbcTemplate namedParameterJdbcTemplate ;
33
+
29
34
private SimpleJdbcInsert insertMeal ;
30
35
31
36
public JdbcUserMealRepositoryImpl (DataSource dataSource ) {
@@ -38,32 +43,45 @@ public UserMeal save(UserMeal userMeal, int userId) {
38
43
MapSqlParameterSource map = new MapSqlParameterSource ()
39
44
.addValue ("time" ,userMeal .getDateTime ())
40
45
.addValue ("calories" ,userMeal .getCalories ())
41
- .addValue ("description" ,userMeal .getDescription ());
46
+ .addValue ("description" ,userMeal .getDescription ())
47
+ .addValue ("user_id" ,userId );
48
+ if (userMeal .isNew ()) {
49
+ Number newKey = insertMeal .executeAndReturnKey (map );
50
+ userMeal .setId (newKey .intValue ());
51
+ } else {
52
+ namedParameterJdbcTemplate .update (
53
+ "UPDATE meals SET time=: time, calories=: calories, description=: description " +
54
+ "WHERE meal_id=:meal_id AND user_id=: user_id" ,map );
55
+ }
42
56
return userMeal ;
43
57
}
44
58
45
59
@ Override
46
60
public boolean delete (int id , int userId ) {
47
- return jdbcTemplate .update ("DELETE FROM meals WHERE meal_id=?" , id ) !=0 ;
61
+ return jdbcTemplate .update ("DELETE FROM meals WHERE meal_id=? AND user_id=?" , id , userId ) !=0 ;
48
62
}
49
63
50
64
@ Override
51
65
public UserMeal get (int id , int userId ) {
52
- return jdbcTemplate .queryForObject ("SELECT * FROM meals WHERE meal_id=?" ,ROW_MAPPER , id );
66
+ return jdbcTemplate .queryForObject ("SELECT * FROM meals WHERE meal_id=? AND user_id=? " ,ROW_MAPPER , id , userId );
53
67
}
54
68
55
69
@ Override
56
70
public List <UserMeal > getAll (int userId ) {
57
- return null ;
71
+ return jdbcTemplate . query ( "SELECT * FROM meals ORDER BY user_id" , ROW_MAPPER ) ;
58
72
}
59
73
60
74
@ Override
61
75
public void deleteAll (int userId ) {
76
+ jdbcTemplate .update ("DELETE FROM meals WHERE user_id=?" , userId );
62
77
63
78
}
64
79
65
80
@ Override
66
81
public List <UserMeal > getBetween (LocalDateTime startDate , LocalDateTime endDate , int userId ) {
67
- return null ;
82
+ Timestamp startD = Timestamp .valueOf (startDate );
83
+ Timestamp endD = Timestamp .valueOf (endDate );
84
+ return jdbcTemplate .query (
85
+ "SELECT * FROM meals WHERE user_id=? AND time BETWEEN ? AND ?" ,ROW_MAPPER , userId ,startD , endD );
68
86
}
69
87
}
0 commit comments