Skip to content

Commit 5eef3f6

Browse files
committed
methods in daoRepository have changed with new names, also check this refactors in other classes.
1 parent 067ecd1 commit 5eef3f6

File tree

7 files changed

+55
-44
lines changed

7 files changed

+55
-44
lines changed

src/main/java/ir/bigz/springbootreal/dal/DaoRepository.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,32 @@ public interface DaoRepository<T, K extends Serializable> {
1919

2020
Object getORMapper();
2121
<E> E getORMapper(Class<E> type);
22-
<S extends T> S insert(S entity);
23-
<S extends T> Iterable<S> insert(Iterable<S> entities);
22+
<S extends T> S save(S entity);
23+
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
24+
2425
<S extends T> S update(S entity);
25-
<S extends T> Iterable<S> update(Iterable<S> entities);
26+
<S extends T> Iterable<S> updateAll(Iterable<S> entities);
27+
28+
void deleteById(K id);
2629
<S extends T> void delete(S entity);
27-
void delete(K id);
2830
void deleteAll();
29-
Optional<T> find(K id);
30-
<S extends T> List<S> find(List<K> entityIds);
31+
void deleteAllById(Iterable<? extends K> entities);
32+
void deleteAll(Iterable<? extends T> entities);
33+
34+
Optional<T> findById(K id);
35+
Stream<T> findAll();
36+
List<T> findAll(Sort sort);
37+
Page<T> findAll(Pageable pageable);
3138
<S extends T> List<S> find(String entityName);
39+
3240
List<T> genericSearch(String query);
41+
List<T> genericSearch(CriteriaQuery<T> criteriaQuery);
42+
Page<T> genericSearch(CriteriaQuery<T> criteriaQuery, Pageable pageable);
43+
Page<T> genericSearch(String query, Pageable pageable);
44+
3345
List<T> nativeQuery(String query, Map<String, Object> parameters);
3446
PageResult<T> pageCreateQuery(String nativeQuery, PagedQuery pagedQuery, Map<String, Object> parameterMap, boolean getTotalCount);
47+
3548
void flush();
3649
void clear();
37-
38-
Stream<T> getAll();
39-
List<T> getAll(Sort sort);
40-
Page<T> getAll(Pageable pageable);
41-
List<T> genericSearch(CriteriaQuery<T> criteriaQuery);
42-
Page<T> genericSearch(CriteriaQuery<T> criteriaQuery, Pageable pageable);
43-
Page<T> genericSearch(String query, Pageable pageable);
4450
}

src/main/java/ir/bigz/springbootreal/dal/DaoRepositoryImpl.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public <E> E getORMapper(Class<E> type) {
7171

7272
@Override
7373
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
74-
public <S extends T> S insert(S entity) {
74+
public <S extends T> S save(S entity) {
7575
entityManager.persist(entity);
7676
return entity;
7777
}
7878

7979
@Override
80-
public <S extends T> Iterable<S> insert(Iterable<S> entities) {
80+
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
8181
return null;
8282
}
8383

@@ -88,7 +88,7 @@ public <S extends T> S update(S entity) {
8888
}
8989

9090
@Override
91-
public <S extends T> Iterable<S> update(Iterable<S> entities) {
91+
public <S extends T> Iterable<S> updateAll(Iterable<S> entities) {
9292
return null;
9393
}
9494

@@ -99,25 +99,30 @@ public <S extends T> void delete(S entity) {
9999

100100
@Override
101101
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
102-
public void delete(K id) {
103-
entityManager.remove(find(id).get());
102+
public void deleteById(K id) {
103+
entityManager.remove(findById(id).get());
104104
}
105105

106106
@Override
107107
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
108108
public void deleteAll() {
109-
getAll().forEach(t -> entityManager.remove(t));
109+
findAll().forEach(t -> entityManager.remove(t));
110110
}
111111

112112
@Override
113-
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
114-
public Optional<T> find(K id) throws IllegalArgumentException {
115-
return Optional.of(entityManager.find(daoType, id));
113+
public void deleteAllById(Iterable<? extends K> entities) {
114+
116115
}
117116

118117
@Override
119-
public <S extends T> List<S> find(List<K> entityIds) {
120-
return null;
118+
public void deleteAll(Iterable<? extends T> entities) {
119+
120+
}
121+
122+
@Override
123+
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
124+
public Optional<T> findById(K id) throws IllegalArgumentException {
125+
return Optional.of(entityManager.find(daoType, id));
121126
}
122127

123128
@Override
@@ -220,7 +225,7 @@ public void clear() {
220225
@SuppressWarnings("unchecked")
221226
@Override
222227
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
223-
public Stream<T> getAll() {
228+
public Stream<T> findAll() {
224229
Session session = entityManager.unwrap(Session.class);
225230
return session.createQuery("from " + daoType.getName())
226231
.setHint(QueryHints.HINT_FETCH_SIZE, 50)
@@ -229,7 +234,7 @@ public Stream<T> getAll() {
229234

230235
@Override
231236
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
232-
public List<T> getAll(Sort sort) {
237+
public List<T> findAll(Sort sort) {
233238
CriteriaQuery<T> query = criteriaBuilder.createQuery(daoType);
234239
Root<T> root = query.from(daoType);
235240
query.orderBy(orderByClauseBuilder(root, sort));
@@ -240,7 +245,7 @@ public List<T> getAll(Sort sort) {
240245

241246
@Override
242247
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
243-
public Page<T> getAll(Pageable pageable) {
248+
public Page<T> findAll(Pageable pageable) {
244249

245250
long totalCount = totalCountOfEntities();
246251

src/main/java/ir/bigz/springbootreal/datagenerator/DataGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private void createUser(Faker faker){
4848
createDateFromString("2020-10-20")).getTime()));
4949
return user;
5050
}).collect(Collectors.toList());
51-
userList.forEach(userRepository::insert);
51+
userList.forEach(userRepository::save);
5252
}
5353

5454
private String generateNationalCode(Faker faker){

src/main/java/ir/bigz/springbootreal/service/UserServiceImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public UserServiceImpl(UserRepository userRepository,
4747
@Cacheable(value = "userCache", key = "#userId", condition = "#userId != null", unless = "#result==null")
4848
public UserModel getUser(Long userId) {
4949
try {
50-
Optional<User> user = userRepository.find(userId);
50+
Optional<User> user = userRepository.findById(userId);
5151
return userMapper.userToUserModel(user.get());
5252
} catch (RuntimeException exception) {
5353
throw AppException.newInstance(
@@ -65,7 +65,7 @@ public UserModel addUser(UserModel userModel) {
6565
userModel.setInsertDate(Utils.getLocalTimeNow());
6666
userModel.setActiveStatus(true);
6767
User user = userMapper.userModelToUser(userModel);
68-
User insert = userRepository.insert(user);
68+
User insert = userRepository.save(user);
6969
return userMapper.userToUserModel(insert);
7070
}
7171
throw new RuntimeException("user has already exist");
@@ -82,7 +82,7 @@ public UserModel addUser(UserModel userModel) {
8282
@CachePut(value = "userCache", key = "#userId", condition = "#userId != null", unless = "#result==null")
8383
public UserModel updateUser(long userId, UserModel userModel) {
8484
try {
85-
Optional<User> user = userRepository.find(userId);
85+
Optional<User> user = userRepository.findById(userId);
8686
User sourceUser = user.get();
8787
User updateUser = userMapper.userModelToUser(userModel);
8888
mapUserForUpdate(sourceUser, updateUser);
@@ -101,8 +101,8 @@ public UserModel updateUser(long userId, UserModel userModel) {
101101
@CacheEvict(value = "userCache", beforeInvocation = true, key = "#userId")
102102
public String deleteUser(long userId) {
103103
try {
104-
userRepository.find(userId);
105-
userRepository.delete(userId);
104+
userRepository.findById(userId);
105+
userRepository.deleteById(userId);
106106
return "Success";
107107

108108
} catch (RuntimeException exception) {
@@ -117,7 +117,7 @@ public String deleteUser(long userId) {
117117
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
118118
public List<UserModel> getAll() {
119119
try {
120-
Stream<User> allUser = userRepository.getAll();
120+
Stream<User> allUser = userRepository.findAll();
121121
return allUser.map(userMapper::userToUserModel).collect(Collectors.toList());
122122
} catch (RuntimeException exception) {
123123
throw AppException.newInstance(
@@ -152,7 +152,7 @@ public Page<UserModel> getUserSearchResult(UserSearchDto userSearchDto, String s
152152
public Page<UserModel> getAllUserPage(String sortOrder, Sort.Direction direction, Integer pageNumber, Integer pageSize) {
153153
Sort.Order order = new Sort.Order(direction, sortOrder);
154154
Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(order));
155-
Page<User> all = userRepository.getAll(pageable);
155+
Page<User> all = userRepository.findAll(pageable);
156156
List<UserModel> collect = all.get().map(userMapper::userToUserModel).collect(Collectors.toList());
157157
return new PageImpl<>(collect, pageable, all.getTotalElements());
158158
}

src/test/groovy/ir/bigz/springbootreal/web/RepositoryTest.groovy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class RepositoryTest extends InitTestContainerDB {
4444
def user = generateUser()
4545

4646
when: "insert entity to db and assign id to entity"
47-
userRepository.insert(user)
47+
userRepository.save(user)
4848

4949
then: "entity has id, success persist"
5050
user.getId() != null
@@ -54,14 +54,14 @@ class RepositoryTest extends InitTestContainerDB {
5454
@Transactional
5555
def "deactivate user with stream data and not throw exception"(){
5656

57-
def countActiveUser = userRepository.getAll().filter(BaseEntity::isActiveStatus).count()
57+
def countActiveUser = userRepository.findAll().filter(BaseEntity::isActiveStatus).count()
5858

5959
when: "deactivate users"
60-
userRepository.getAll()
60+
userRepository.findAll()
6161
.filter(BaseEntity::isActiveStatus).peek(user -> { user.setActiveStatus(false) }).count()
6262

6363
then: "after deactivate user, count active must be zero"
64-
def count = userRepository.getAll().filter(BaseEntity::isActiveStatus).count()
64+
def count = userRepository.findAll().filter(BaseEntity::isActiveStatus).count()
6565
count == 0
6666
countActiveUser != count
6767

@@ -109,18 +109,18 @@ class RepositoryTest extends InitTestContainerDB {
109109
def user = generateUser()
110110

111111
and: "insert user to db"
112-
userRepository.insert(user)
112+
userRepository.save(user)
113113

114114
when: "find user and update"
115-
def find = userRepository.find(user.getId())
115+
def find = userRepository.findById(user.getId())
116116

117117
and: "update date"
118118
find.get().setUserName("sample2")
119119
find.get().setUpdateDate(Timestamp.valueOf(LocalDateTime.now()))
120120
userRepository.update(find.get())
121121

122122
then: "if properties are updated so test is success"
123-
def result = userRepository.find(user.id)
123+
def result = userRepository.findById(user.id)
124124
result.get().getUserName() == "sample2"
125125

126126
}

src/test/groovy/ir/bigz/springbootreal/web/SampleApiTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class SampleApiTest extends Specification{
8585
def model = generateUserModel()
8686

8787
and:"define behavior of userRepository methods"
88-
userRepository.insert(_) >> user
88+
userRepository.save(_) >> user
8989
userRepository.getUserWithNationalCode(_) >> null
9090

9191
and:"create json Object"

src/test/groovy/ir/bigz/springbootreal/web/ServiceTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ServiceTest extends InitTestContainerDB {
8484
queryParams.put("firstName", "h")
8585

8686
when: "call method"
87-
def result = userService.getUserSearchV2(queryParams, pagedQuery)
87+
def result = userService.getUserSearchWithNativeQuery(queryParams, pagedQuery)
8888

8989
then: "check result size"
9090
result.getResult().size() > 0

0 commit comments

Comments
 (0)