Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 259cb70

Browse files
committed
Applied patch - 6_0_fix
1 parent 22df5a3 commit 259cb70

7 files changed

Lines changed: 30 additions & 28 deletions

File tree

src/main/java/ru/javawebinar/topjava/repository/jpa/JpaMealRepository.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@ public class JpaMealRepository implements MealRepository {
2424
@Override
2525
@Transactional
2626
public Meal save(Meal meal, int userId) {
27-
if (!meal.isNew() && get(meal.getId(), userId) == null) {
28-
return null;
29-
}
3027
meal.setUser(em.getReference(User.class, userId));
3128
if (meal.isNew()) {
3229
em.persist(meal);
3330
return meal;
34-
} else {
35-
return em.merge(meal);
31+
} else if (get(meal.getId(), userId) == null) {
32+
return null;
3633
}
34+
return em.merge(meal);
3735
}
3836

3937
@Override

src/main/java/ru/javawebinar/topjava/service/UserService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.springframework.util.Assert;
88
import ru.javawebinar.topjava.model.User;
99
import ru.javawebinar.topjava.repository.UserRepository;
10-
import ru.javawebinar.topjava.util.exception.NotFoundException;
1110

1211
import java.util.List;
1312

@@ -31,15 +30,15 @@ public User create(User user) {
3130
}
3231

3332
@CacheEvict(value = "users", allEntries = true)
34-
public void delete(int id) throws NotFoundException {
33+
public void delete(int id) {
3534
checkNotFoundWithId(repository.delete(id), id);
3635
}
3736

38-
public User get(int id) throws NotFoundException {
37+
public User get(int id) {
3938
return checkNotFoundWithId(repository.get(id), id);
4039
}
4140

42-
public User getByEmail(String email) throws NotFoundException {
41+
public User getByEmail(String email) {
4342
Assert.notNull(email, "email must not be null");
4443
return checkNotFound(repository.getByEmail(email), "email=" + email);
4544
}
@@ -50,7 +49,7 @@ public List<User> getAll() {
5049
}
5150

5251
@CacheEvict(value = "users", allEntries = true)
53-
public void update(User user) throws NotFoundException {
52+
public void update(User user) {
5453
Assert.notNull(user, "user must not be null");
5554
checkNotFoundWithId(repository.save(user), user.getId());
5655
}

src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,11 @@ public static String toString(LocalDateTime ldt) {
3232
}
3333

3434
public static LocalDateTime getStartInclusive(LocalDate localDate) {
35-
return startOfDay(localDate != null ? localDate : MIN_DATE);
35+
return (localDate != null ? localDate : MIN_DATE).atStartOfDay();
3636
}
3737

3838
public static LocalDateTime getEndExclusive(LocalDate localDate) {
39-
return startOfDay(localDate != null ? localDate.plus(1, ChronoUnit.DAYS) : MAX_DATE);
40-
}
41-
42-
private static LocalDateTime startOfDay(LocalDate localDate) {
43-
return LocalDateTime.of(localDate, LocalTime.MIN);
39+
return (localDate != null ? localDate.plus(1, ChronoUnit.DAYS) : MAX_DATE).atStartOfDay();
4440
}
4541
}
4642

src/test/java/ru/javawebinar/topjava/MealTestData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class MealTestData {
2525

2626
public static final List<Meal> MEALS = List.of(MEAL7, MEAL6, MEAL5, MEAL4, MEAL3, MEAL2, MEAL1);
2727

28-
public static Meal getCreated() {
28+
public static Meal getNew() {
2929
return new Meal(null, of(2015, Month.JUNE, 1, 18, 0), "Созданный ужин", 300);
3030
}
3131

src/test/java/ru/javawebinar/topjava/UserTestData.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import ru.javawebinar.topjava.model.Role;
44
import ru.javawebinar.topjava.model.User;
55

6+
import java.util.Collections;
7+
import java.util.Date;
68
import java.util.List;
79

810
import static org.assertj.core.api.Assertions.assertThat;
@@ -15,6 +17,17 @@ public class UserTestData {
1517
public static final User USER = new User(USER_ID, "User", "[email protected]", "password", Role.ROLE_USER);
1618
public static final User ADMIN = new User(ADMIN_ID, "Admin", "[email protected]", "admin", Role.ROLE_ADMIN);
1719

20+
public static User getNew() {
21+
return new User(null, "New", "[email protected]", "newPass", 1555, false, new Date(), Collections.singleton(Role.ROLE_USER));
22+
}
23+
24+
public static User getUpdated() {
25+
User updated = new User(USER);
26+
updated.setName("UpdatedName");
27+
updated.setCaloriesPerDay(330);
28+
return updated;
29+
}
30+
1831
public static void assertMatch(User actual, User expected) {
1932
assertThat(actual).isEqualToIgnoringGivenFields(expected, "registered", "roles");
2033
}

src/test/java/ru/javawebinar/topjava/service/MealServiceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class MealServiceTest {
3939

4040
private static StringBuilder results = new StringBuilder();
4141

42+
@Autowired
43+
private MealService service;
44+
4245
@Rule
4346
public ExpectedException thrown = ExpectedException.none();
4447

@@ -62,9 +65,6 @@ public static void printResult() {
6265
"\n---------------------------------");
6366
}
6467

65-
@Autowired
66-
private MealService service;
67-
6868
@Test
6969
public void delete() throws Exception {
7070
service.delete(MEAL1_ID, USER_ID);
@@ -86,7 +86,7 @@ public void deleteNotOwn() throws Exception {
8686

8787
@Test
8888
public void create() throws Exception {
89-
Meal newMeal = getCreated();
89+
Meal newMeal = getNew();
9090
Meal created = service.create(newMeal, USER_ID);
9191
Integer newId = created.getId();
9292
newMeal.setId(newId);
@@ -103,7 +103,7 @@ public void get() throws Exception {
103103
@Test
104104
public void getNotFound() throws Exception {
105105
thrown.expect(NotFoundException.class);
106-
service.get(MEAL1_ID, ADMIN_ID);
106+
service.get(1, ADMIN_ID);
107107
}
108108

109109
@Test

src/test/java/ru/javawebinar/topjava/service/UserServiceTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import ru.javawebinar.topjava.model.User;
1717
import ru.javawebinar.topjava.util.exception.NotFoundException;
1818

19-
import java.util.Collections;
20-
import java.util.Date;
2119
import java.util.List;
2220

2321
import static ru.javawebinar.topjava.UserTestData.*;
@@ -44,7 +42,7 @@ public void setUp() throws Exception {
4442

4543
@Test
4644
public void create() throws Exception {
47-
User newUser = new User(null, "New", "[email protected]", "newPass", 1555, false, new Date(), Collections.singleton(Role.ROLE_USER));
45+
User newUser = getNew();
4846
User created = service.create(newUser);
4947
Integer newId = created.getId();
5048
newUser.setId(newId);
@@ -87,9 +85,7 @@ public void getByEmail() throws Exception {
8785

8886
@Test
8987
public void update() throws Exception {
90-
User updated = new User(USER);
91-
updated.setName("UpdatedName");
92-
updated.setCaloriesPerDay(330);
88+
User updated = getUpdated();
9389
service.update(updated);
9490
assertMatch(service.get(USER_ID), updated);
9591
}

0 commit comments

Comments
 (0)