Updated crud service and base entity impl

This commit is contained in:
Sambo Chea 2020-07-03 14:57:16 +07:00
parent 40cffea983
commit 950f2efaaf
3 changed files with 12 additions and 10 deletions

View File

@ -1,8 +1,8 @@
package com.cubetiqs.data.service;
import com.cubetiqs.data.domain.BaseEntity;
import com.cubetiqs.data.repository.BaseRepository;
import java.io.Serializable;
import java.util.List;
/**
@ -12,7 +12,7 @@ import java.util.List;
* @since 1.0
*/
@NoServiceBean
public class CrudServiceImpl<T, ID extends BaseEntity<ID>> implements CrudService<T, ID> {
public abstract class AbstractCrudServiceImpl<T extends BaseEntity<ID>, ID extends Serializable> implements CrudService<T, ID> {
@Override
public List<T> findAll() {
return getRepository().findAll();
@ -35,11 +35,11 @@ public class CrudServiceImpl<T, ID extends BaseEntity<ID>> implements CrudServic
@Override
public T update(ID id, T entity) {
T update = read(id);
if (update == null) {
return null;
if (existsById(id)) {
entity.setId(id);
return getRepository().save(entity);
}
return getRepository().save(entity);
return null;
}
@Override
@ -53,7 +53,7 @@ public class CrudServiceImpl<T, ID extends BaseEntity<ID>> implements CrudServic
}
@Override
public BaseRepository<T, ID> getRepository() {
throw new ServiceNotImplementException("service must be implement!");
public boolean existsById(ID id) {
return getRepository().existsById(id);
}
}

View File

@ -64,4 +64,6 @@ public interface CrudService<T, ID> extends BaseService<T, ID> {
* @param ids List of IDs
*/
void deleteAll(List<ID> ids);
boolean existsById(ID id);
}

View File

@ -12,11 +12,11 @@ import java.util.List;
* @author sombochea
* @see CrudService
* @see PagingCrudService
* @see CrudServiceImpl
* @see AbstractCrudServiceImpl
* @since 1.0
*/
@NoServiceBean
public class PagingCrudServiceImpl<T, ID> extends CrudServiceImpl<T, ID> implements PagingCrudService<T, ID> {
public class PagingCrudServiceImpl<T, ID> extends AbstractCrudServiceImpl<T, ID> implements PagingCrudService<T, ID> {
@Override
public Page<T> findAll(Pageable pageable) {
return getRepository().findAll(pageable);