Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
fbec6db
Add description
JavaWebinar Nov 5, 2016
81a0a1a
Add HW0 comments
JavaWebinar Nov 10, 2016
4f29fe7
Update README.md
JavaOPs Nov 30, 2016
fab7022
Update README.md
JavaOPs Dec 1, 2016
ce1caba
Update README.md
JavaOPs Dec 1, 2016
c01186b
Prepare to HW0
meirakhmetov Dec 4, 2016
25dc217
1_1_HW0
meirakhmetov Dec 21, 2016
f7860da
1_2_HW0_cycle
meirakhmetov Dec 21, 2016
f48e28a
1_3_switch_to_war
meirakhmetov Dec 21, 2016
1d4c218
1_4_add_servlet_api
meirakhmetov Dec 21, 2016
502dd4a
1_4_add_servlet_api2
meirakhmetov Dec 21, 2016
0f5921b
1_5_forward_to_redirect
meirakhmetov Dec 22, 2016
f0250cb
1_6_logging
meirakhmetov Dec 22, 2016
6b461d9
1_6_remote_jmx
meirakhmetov Dec 22, 2016
24b92d1
HW1
meirakhmetov Dec 23, 2016
274b8ef
HW1_CRUD
meirakhmetov Jan 16, 2017
d18ae8d
2_3_app_layers
meirakhmetov Jan 16, 2017
b7c1ab2
2_4_add_spring_context
meirakhmetov Jan 17, 2017
fc99f7b
2_5_add_dependency_injection
meirakhmetov Jan 17, 2017
9f611f4
2_5_add_dependency_injection
meirakhmetov Jan 17, 2017
14ffad3
2_6_add_annotation_processing
meirakhmetov Jan 17, 2017
2aa144c
3_0_fix_validate
meirakhmetov Jan 17, 2017
951c7d1
3_01_HW2_repository
meirakhmetov Jan 17, 2017
564049b
3_01_HW2_repository
meirakhmetov Jan 17, 2017
379c502
3_02_HW2_meal_layers
meirakhmetov Jan 17, 2017
07a9a74
3_03_HW2_optional_MealServlet
meirakhmetov Jan 17, 2017
6ed96bf
3_4_HW2_optional_filter
meirakhmetov Jan 17, 2017
bb68b77
3_5_HW2_optional_select_user
meirakhmetov Jan 17, 2017
1106ce7
3_06_bean_life_cycle
meirakhmetov Jan 17, 2017
0eed750
3_07_add_junit
meirakhmetov Jan 17, 2017
e1f7370
3_08_add_spring_test
meirakhmetov Jan 17, 2017
f2e869e
3_09_add_postgresql
meirakhmetov Jan 17, 2017
cd1216f
3_10_populate_and_init_db
meirakhmetov Jan 18, 2017
f7a4702
3_11_impl_JdbcUserRepository
meirakhmetov Jan 18, 2017
970bbd3
3_12_test_UserService
meirakhmetov Jan 18, 2017
3330e80
3_13_test_logging
meirakhmetov Jan 18, 2017
710f0b2
3_14_fix_servlet
meirakhmetov Jan 18, 2017
0b9d329
4_0_fix
meirakhmetov Jan 19, 2017
f0c9bcb
4_1_HW3
meirakhmetov Jan 19, 2017
071a6d5
4_2_HW3_optional
meirakhmetov Jan 19, 2017
da0852d
4_3_improve_code
meirakhmetov Jan 19, 2017
74a7205
4_4_init_and_populate_db
meirakhmetov Jan 19, 2017
577298e
4_5_create_mock_test_ctx
meirakhmetov Jan 19, 2017
21a38c3
Update README.md
meirakhmetov Nov 25, 2024
59b9c05
Update Main.java
meirakhmetov Nov 25, 2024
ade580e
added var test to Main.java
meirakhmetov Nov 25, 2024
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
4_3_improve_code
  • Loading branch information
meirakhmetov committed Jan 19, 2017
commit da0852d6d77653f6f9ad8e1337aadc2fc7b2cdcc
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://docs.travis-ci.com/user/languages/java/
language: java
jdk: oraclejdk8

#https://dzone.com/articles/travis-ci-tutorial-java-projects
cache:
directories:
- $HOME/.m2

# https://docs.travis-ci.com/user/database-setup/#PostgreSQL
before_script:
- psql -c 'create database topjava' -U postgres
- psql -c 'create user "user"; grant all privileges on database topjava to "user"' -U postgres
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.repository.MealRepository;

Expand Down Expand Up @@ -32,6 +33,8 @@ public void delete(int id, int userId) {

@Override
public Collection<Meal> getBetweenDateTimes(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
Assert.notNull(startDateTime, "startDateTime must not be null");
Assert.notNull(endDateTime, "endDateTime must not be null");
return repository.getBetween(startDateTime, endDateTime, userId);
}

Expand All @@ -42,11 +45,13 @@ public Collection<Meal> getAll(int userId) {

@Override
public Meal update(Meal meal, int userId) {
Assert.notNull(meal, "meal must not be null");
return checkNotFoundWithId(repository.save(meal, userId), meal.getId());
}

@Override
public Meal save(Meal meal, int userId) {
Assert.notNull(meal, "meal must not be null");
return repository.save(meal, userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.repository.UserRepository;
import ru.javawebinar.topjava.util.exception.NotFoundException;
Expand All @@ -23,6 +24,7 @@ public class UserServiceImpl implements UserService {

@Override
public User save(User user) {
Assert.notNull(user, "user must not be null");
return repository.save(user);
}

Expand All @@ -38,6 +40,7 @@ public User get(int id) throws NotFoundException {

@Override
public User getByEmail(String email) throws NotFoundException {
Assert.notNull(email, "email must not be null");
return checkNotFound(repository.getByEmail(email), "email=" + email);
}

Expand All @@ -48,6 +51,7 @@ public List<User> getAll() {

@Override
public void update(User user) {
Assert.notNull(user, "user must not be null");
repository.save(user);
}
}
3 changes: 3 additions & 0 deletions src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class DateTimeUtil {
public static final LocalDate MIN_DATE = LocalDate.of(1, 1, 1);
public static final LocalDate MAX_DATE = LocalDate.of(3000, 1, 1);

private DateTimeUtil() {
}

public static <T extends Comparable<? super T>> boolean isBetween(T value, T start, T end) {
return value.compareTo(start) >= 0 && value.compareTo(end) <= 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* Date: 14.05.2014
*/
public class ValidationUtil {
private ValidationUtil() {
}

public static void checkNotFoundWithId(boolean found, int id) {
checkNotFound(found, "id=" + id);
}
Expand All @@ -23,7 +26,9 @@ public static <T> T checkNotFound(T object, String msg) {
}

public static void checkNotFound(boolean found, String msg) {
if (!found) throw new NotFoundException("Not found entity with " + msg);
if (!found) {
throw new NotFoundException("Not found entity with " + msg);
}
}

public static void checkNew(BaseEntity entity) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/javawebinar/topjava/web/MealServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
response.sendRedirect("meals");

} else if ("create".equals(action) || "update".equals(action)) {
final Meal meal = action.equals("create") ?
final Meal meal = "create".equals(action) ?
new Meal(LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES), "", 1000) :
mealController.get(getId(request));
request.setAttribute("meal", meal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,40 @@
* User: gkislin
*/
public abstract class AbstractUserController {
protected final Logger LOG = LoggerFactory.getLogger(getClass());
protected final Logger log = LoggerFactory.getLogger(getClass());

@Autowired
private UserService service;

public List<User> getAll() {
LOG.info("getAll");
log.info("getAll");
return service.getAll();
}

public User get(int id) {
LOG.info("get " + id);
log.info("get " + id);
return service.get(id);
}

public User create(User user) {
checkNew(user);
LOG.info("create " + user);
log.info("create " + user);
return service.save(user);
}

public void delete(int id) {
LOG.info("delete " + id);
log.info("delete " + id);
service.delete(id);
}

public void update(User user, int id) {
checkIdConsistent(user, id);
LOG.info("update " + user);
log.info("update " + user);
service.update(user);
}

public User getByMail(String email) {
LOG.info("getByEmail " + email);
log.info("getByEmail " + email);
return service.getByEmail(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* @param <T> : Entity
*/
public class ModelMatcher<T> {
public interface Comparator<T> {
boolean compare(T expected, T actual);
}

private static final Comparator DEFAULT_COMPARATOR =
(Object expected, Object actual) -> expected == actual || String.valueOf(expected).equals(String.valueOf(actual));

private Comparator<T> comparator;

public interface Comparator<T> {
boolean compare(T expected, T actual);
}

public ModelMatcher() {
this((Comparator<T>) DEFAULT_COMPARATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand All @@ -30,8 +29,6 @@ public class InMemoryMealRepositoryImpl implements MealRepository {

@Override
public Meal save(Meal meal, int userId) {
Objects.requireNonNull(meal);

if (meal.isNew()) {
meal.setId(counter.incrementAndGet());
} else if (get(meal.getId(), userId) == null) {
Expand Down Expand Up @@ -61,8 +58,6 @@ public Collection<Meal> getAll(int userId) {

@Override
public Collection<Meal> getBetween(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
Objects.requireNonNull(startDateTime);
Objects.requireNonNull(endDateTime);
return getAllAsStream(userId)
.filter(um -> DateTimeUtil.isBetween(um.getDateTime(), startDateTime, endDateTime))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand All @@ -31,7 +30,6 @@ public class InMemoryUserRepositoryImpl implements UserRepository {

@Override
public User save(User user) {
Objects.requireNonNull(user);
if (user.isNew()) {
user.setId(counter.incrementAndGet());
}
Expand Down Expand Up @@ -68,7 +66,6 @@ public List<User> getAll() {

@Override
public User getByEmail(String email) {
Objects.requireNonNull(email);
return repository.values().stream()
.filter(u -> email.equals(u.getEmail()))
.findFirst()
Expand Down