Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d4a881b

Browse filesBrowse files
committed
leson03_allPatches
1 parent 7ecf211 commit d4a881b
Copy full SHA for d4a881b
Expand file treeCollapse file tree

37 files changed

+991
-180
lines changed
Open diff view settings
Collapse file

‎pom.xml‎

Copy file name to clipboardExpand all lines: pom.xml
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<!-- Logging -->
2222
<logback.version>1.2.3</logback.version>
2323
<slf4j.version>1.7.25</slf4j.version>
24+
25+
<!--DB-->
26+
<postgresql.version>42.1.4</postgresql.version>
27+
<!-- Tests -->
28+
<junit.version>4.12</junit.version>
2429
</properties>
2530

2631
<build>
@@ -36,6 +41,14 @@
3641
<target>${java.version}</target>
3742
</configuration>
3843
</plugin>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-surefire-plugin</artifactId>
47+
<version>2.20.1</version>
48+
<configuration>
49+
<argLine>-Dfile.encoding=UTF-8</argLine>
50+
</configuration>
51+
</plugin>
3952
</plugins>
4053
</build>
4154

@@ -55,6 +68,13 @@
5568
<scope>runtime</scope>
5669
</dependency>
5770

71+
<dependency>
72+
<groupId>org.slf4j</groupId>
73+
<artifactId>jul-to-slf4j</artifactId>
74+
<version>${slf4j.version}</version>
75+
<scope>runtime</scope>
76+
</dependency>
77+
5878
<dependency>
5979
<groupId>ch.qos.logback</groupId>
6080
<artifactId>logback-classic</artifactId>
@@ -74,6 +94,18 @@
7494
</exclusion>
7595
</exclusions>
7696
</dependency>
97+
<dependency>
98+
<groupId>org.springframework</groupId>
99+
<artifactId>spring-jdbc</artifactId>
100+
<version>${spring.version}</version>
101+
</dependency>
102+
103+
<!--DataBase-->
104+
<dependency>
105+
<groupId>org.postgresql</groupId>
106+
<artifactId>postgresql</artifactId>
107+
<version>${postgresql.version}</version>
108+
</dependency>
77109

78110
<!--Web-->
79111
<dependency>
@@ -88,6 +120,32 @@
88120
<artifactId>jstl</artifactId>
89121
<version>1.2</version>
90122
</dependency>
123+
124+
<!--Test-->
125+
<dependency>
126+
<groupId>junit</groupId>
127+
<artifactId>junit</artifactId>
128+
<version>${junit.version}</version>
129+
<scope>test</scope>
130+
</dependency>
131+
<dependency>
132+
<groupId>org.springframework</groupId>
133+
<artifactId>spring-test</artifactId>
134+
<version>${spring.version}</version>
135+
<scope>test</scope>
136+
</dependency>
137+
<dependency>
138+
<groupId>org.assertj</groupId>
139+
<artifactId>assertj-core</artifactId>
140+
<version>3.8.0</version>
141+
<scope>test</scope>
142+
</dependency>
143+
<dependency>
144+
<groupId>org.assertj</groupId>
145+
<artifactId>assertj-core</artifactId>
146+
<version>RELEASE</version>
147+
</dependency>
148+
91149
</dependencies>
92150

93151
<profiles>
Collapse file

‎src/main/java/ru/javawebinar/topjava/AuthorizedUser.java‎

Copy file name to clipboardExpand all lines: src/main/java/ru/javawebinar/topjava/AuthorizedUser.java
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package ru.javawebinar.topjava;
22

3+
import ru.javawebinar.topjava.model.AbstractBaseEntity;
4+
35
import static ru.javawebinar.topjava.util.MealsUtil.DEFAULT_CALORIES_PER_DAY;
46

57
public class AuthorizedUser {
8+
private static int id = AbstractBaseEntity.START_SEQ;
69

710
public static int id() {
8-
return 1;
11+
return id;
12+
}
13+
14+
public static void setId(int id) {
15+
AuthorizedUser.id = id;
916
}
1017

1118
public static int getCaloriesPerDay() {
Collapse file
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.javawebinar.topjava;
2+
3+
import ru.javawebinar.topjava.model.Role;
4+
import ru.javawebinar.topjava.model.User;
5+
6+
import java.util.Arrays;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static ru.javawebinar.topjava.model.AbstractBaseEntity.START_SEQ;
10+
11+
public class UserTestData {
12+
public static final int USER_ID = START_SEQ;
13+
public static final int ADMIN_ID = START_SEQ + 1;
14+
15+
public static final User USER = new User(USER_ID, "User", "user@yandex.ru", "password", Role.ROLE_USER);
16+
public static final User ADMIN = new User(ADMIN_ID, "Admin", "admin@gmail.com", "admin", Role.ROLE_ADMIN);
17+
18+
public static void assertMatch(User actual, User expected) {
19+
assertThat(actual).isEqualToIgnoringGivenFields(expected, "registered", "roles");
20+
}
21+
22+
public static void assertMatch(Iterable<User> actual, User... expected) {
23+
assertMatch(actual, Arrays.asList(expected));
24+
}
25+
26+
public static void assertMatch(Iterable<User> actual, Iterable<User> expected) {
27+
assertThat(actual).usingElementComparatorIgnoringFields("registered", "roles").isEqualTo(expected);
28+
}
29+
}
Collapse file

‎src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java‎

Copy file name to clipboardExpand all lines: src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package ru.javawebinar.topjava.model;
22

33
public abstract class AbstractBaseEntity {
4+
public static final int START_SEQ = 100000;
5+
46
protected Integer id;
57

8+
public AbstractBaseEntity() {
9+
}
10+
611
protected AbstractBaseEntity(Integer id) {
712
this.id = id;
813
}
@@ -23,4 +28,22 @@ public boolean isNew() {
2328
public String toString() {
2429
return String.format("Entity %s (%s)", getClass().getName(), id);
2530
}
31+
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) {
36+
return true;
37+
}
38+
if (o == null || getClass() != o.getClass()) {
39+
return false;
40+
}
41+
AbstractBaseEntity that = (AbstractBaseEntity) o;
42+
return id != null && id.equals(that.id);
43+
}
44+
45+
@Override
46+
public int hashCode() {
47+
return id == null ? 0 : id;
48+
}
2649
}
Collapse file

‎src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java‎

Copy file name to clipboardExpand all lines: src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ public abstract class AbstractNamedEntity extends AbstractBaseEntity {
44

55
protected String name;
66

7+
public AbstractNamedEntity() {
8+
}
9+
710
protected AbstractNamedEntity(Integer id, String name) {
811
super(id);
912
this.name = name;
Collapse file

‎src/main/java/ru/javawebinar/topjava/model/Meal.java‎

Copy file name to clipboardExpand all lines: src/main/java/ru/javawebinar/topjava/model/Meal.java
+2-16Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import java.time.LocalDateTime;
55
import java.time.LocalTime;
66

7-
public class Meal {
8-
private Integer id;
9-
7+
public class Meal extends AbstractBaseEntity {
108
private final LocalDateTime dateTime;
119

1210
private final String description;
@@ -18,20 +16,12 @@ public Meal(LocalDateTime dateTime, String description, int calories) {
1816
}
1917

2018
public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
21-
this.id = id;
19+
super(id);
2220
this.dateTime = dateTime;
2321
this.description = description;
2422
this.calories = calories;
2523
}
2624

27-
public Integer getId() {
28-
return id;
29-
}
30-
31-
public void setId(Integer id) {
32-
this.id = id;
33-
}
34-
3525
public LocalDateTime getDateTime() {
3626
return dateTime;
3727
}
@@ -52,10 +42,6 @@ public LocalTime getTime() {
5242
return dateTime.toLocalTime();
5343
}
5444

55-
public boolean isNew() {
56-
return id == null;
57-
}
58-
5945
@Override
6046
public String toString() {
6147
return "Meal{" +
Collapse file

‎src/main/java/ru/javawebinar/topjava/model/User.java‎

Copy file name to clipboardExpand all lines: src/main/java/ru/javawebinar/topjava/model/User.java
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public class User extends AbstractNamedEntity {
2020

2121
private int caloriesPerDay = DEFAULT_CALORIES_PER_DAY;
2222

23+
public User() {
24+
}
25+
26+
public User(User u) {
27+
this(u.getId(), u.getName(), u.getEmail(), u.getPassword(), u.getCaloriesPerDay(), u.isEnabled(), u.getRoles());
28+
}
29+
2330
public User(Integer id, String name, String email, String password, Role role, Role... roles) {
2431
this(id, name, email, password, DEFAULT_CALORIES_PER_DAY, true, EnumSet.of(role, roles));
2532
}
Collapse file

‎src/main/java/ru/javawebinar/topjava/repository/MealRepository.java‎

Copy file name to clipboardExpand all lines: src/main/java/ru/javawebinar/topjava/repository/MealRepository.java
+13-5Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
import ru.javawebinar.topjava.model.Meal;
44

5-
import java.util.Collection;
5+
import java.time.LocalDateTime;
6+
import java.util.List;
67

78
public interface MealRepository {
8-
Meal save(Meal meal);
9+
// null if updated meal do not belong to userId
10+
Meal save(Meal meal, int userId);
911

10-
void delete(int id);
12+
// false if meal do not belong to userId
13+
boolean delete(int id, int userId);
1114

12-
Meal get(int id);
15+
// null if meal do not belong to userId
16+
Meal get(int id, int userId);
1317

14-
Collection<Meal> getAll();
18+
// ORDERED dateTime desc
19+
List<Meal> getAll(int userId);
20+
21+
// ORDERED dateTime desc
22+
List<Meal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId);
1523
}
Collapse file
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ru.javawebinar.topjava.repository.jdbc;
2+
3+
import org.springframework.stereotype.Repository;
4+
import ru.javawebinar.topjava.model.Meal;
5+
import ru.javawebinar.topjava.repository.MealRepository;
6+
7+
import java.time.LocalDateTime;
8+
import java.util.List;
9+
10+
@Repository
11+
public class JdbcMealRepositoryImpl implements MealRepository {
12+
13+
@Override
14+
public Meal save(Meal meal, int userId) {
15+
return null;
16+
}
17+
18+
@Override
19+
public boolean delete(int id, int userId) {
20+
return false;
21+
}
22+
23+
@Override
24+
public Meal get(int id, int userId) {
25+
return null;
26+
}
27+
28+
@Override
29+
public List<Meal> getAll(int userId) {
30+
return null;
31+
}
32+
33+
@Override
34+
public List<Meal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId) {
35+
return null;
36+
}
37+
}
Collapse file
+82Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ru.javawebinar.topjava.repository.jdbc;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.dao.support.DataAccessUtils;
5+
import org.springframework.jdbc.core.BeanPropertyRowMapper;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
8+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
9+
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
10+
import org.springframework.stereotype.Repository;
11+
import ru.javawebinar.topjava.model.User;
12+
import ru.javawebinar.topjava.repository.UserRepository;
13+
14+
import javax.sql.DataSource;
15+
import java.util.List;
16+
17+
@Repository
18+
public class JdbcUserRepositoryImpl implements UserRepository {
19+
20+
private static final BeanPropertyRowMapper<User> ROW_MAPPER = BeanPropertyRowMapper.newInstance(User.class);
21+
22+
private final JdbcTemplate jdbcTemplate;
23+
24+
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
25+
26+
private final SimpleJdbcInsert insertUser;
27+
28+
@Autowired
29+
public JdbcUserRepositoryImpl(DataSource dataSource, JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
30+
this.insertUser = new SimpleJdbcInsert(dataSource)
31+
.withTableName("users")
32+
.usingGeneratedKeyColumns("id");
33+
34+
this.jdbcTemplate = jdbcTemplate;
35+
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
36+
}
37+
38+
@Override
39+
public User save(User user) {
40+
MapSqlParameterSource map = new MapSqlParameterSource()
41+
.addValue("id", user.getId())
42+
.addValue("name", user.getName())
43+
.addValue("email", user.getEmail())
44+
.addValue("password", user.getPassword())
45+
.addValue("registered", user.getRegistered())
46+
.addValue("enabled", user.isEnabled())
47+
.addValue("caloriesPerDay", user.getCaloriesPerDay());
48+
49+
if (user.isNew()) {
50+
Number newKey = insertUser.executeAndReturnKey(map);
51+
user.setId(newKey.intValue());
52+
} else {
53+
namedParameterJdbcTemplate.update(
54+
"UPDATE users SET name=:name, email=:email, password=:password, " +
55+
"registered=:registered, enabled=:enabled, calories_per_day=:caloriesPerDay WHERE id=:id", map);
56+
}
57+
return user;
58+
}
59+
60+
@Override
61+
public boolean delete(int id) {
62+
return jdbcTemplate.update("DELETE FROM users WHERE id=?", id) != 0;
63+
}
64+
65+
@Override
66+
public User get(int id) {
67+
List<User> users = jdbcTemplate.query("SELECT * FROM users WHERE id=?", ROW_MAPPER, id);
68+
return DataAccessUtils.singleResult(users);
69+
}
70+
71+
@Override
72+
public User getByEmail(String email) {
73+
// return jdbcTemplate.queryForObject("SELECT * FROM users WHERE email=?", ROW_MAPPER, email);
74+
List<User> users = jdbcTemplate.query("SELECT * FROM users WHERE email=?", ROW_MAPPER, email);
75+
return DataAccessUtils.singleResult(users);
76+
}
77+
78+
@Override
79+
public List<User> getAll() {
80+
return jdbcTemplate.query("SELECT * FROM users ORDER BY name, email", ROW_MAPPER);
81+
}
82+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.