Add base repository

This commit is contained in:
Sambo Chea 2020-06-18 19:47:20 +07:00
parent 0121fd4bcb
commit f14bede40b
13 changed files with 104 additions and 3 deletions

View File

@ -0,0 +1,5 @@
package com.cubetiqs.demo;
public final class Constants {
public static final String STATUS = "status";
}

View File

@ -0,0 +1,10 @@
package com.cubetiqs.demo.config;
import com.cubetiqs.demo.repository.BaseRepositoryImpl;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class, basePackages = "com.cubetiqs.demo.repository")
public class BaseRepositoryConfig {
}

View File

@ -1,5 +1,6 @@
package com.cubetiqs.demo.domain;
import com.cubetiqs.demo.Constants;
import org.springframework.data.domain.Persistable;
import javax.persistence.*;
@ -16,6 +17,10 @@ public class BaseEntity<ID extends Serializable> implements Serializable, Persis
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
@Column(name = Constants.STATUS)
@Enumerated(EnumType.ORDINAL)
private Status status;
public void setId(ID id) {
this.id = id;
}
@ -38,6 +43,14 @@ public class BaseEntity<ID extends Serializable> implements Serializable, Persis
this.createdDate = createdDate;
}
public void setStatus(Status status) {
this.status = status;
}
public Status getStatus() {
return status;
}
@PrePersist
public void beforeSave() {
if (createdDate == null) {

View File

@ -0,0 +1,6 @@
package com.cubetiqs.demo.domain;
public enum Status {
ACTIVE,
DEACTIVE
}

View File

@ -4,7 +4,9 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.io.Serializable;
import java.util.List;
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
List<T> findAllActives();
}

View File

@ -0,0 +1,30 @@
package com.cubetiqs.demo.repository;
import com.cubetiqs.demo.Constants;
import com.cubetiqs.demo.domain.Status;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import javax.persistence.EntityManager;
import java.io.Serializable;
import java.util.List;
@NoRepositoryBean
public class BaseRepositoryImpl<T, ID extends Serializable>
extends SimpleJpaRepository<T, ID>
implements BaseRepository<T, ID> {
private final EntityManager entityManager;
public BaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
@Override
public List<T> findAllActives() {
Specification<T> specification = (Specification<T>) (root, query, criteriaBuilder) ->
criteriaBuilder.equal(root.get(Constants.STATUS), Status.ACTIVE);
return findAll(specification);
}
}

View File

@ -3,7 +3,8 @@ package com.cubetiqs.demo.repository;
import com.cubetiqs.demo.domain.PostEntity;
import org.springframework.stereotype.Repository;
@Repository
public interface PostRepository extends BaseRepository<PostEntity, Long> {
}

View File

@ -26,7 +26,7 @@ public class PostController {
@RequestParam(value = "q", defaultValue = "") String q
) {
if ("list".equalsIgnoreCase(viewType)) {
return ResponseEntity.ok(postService.findAll());
return ResponseEntity.ok(postService.findAllActives());
}
return ResponseEntity.ok(postService.findAll(pageable));
}

View File

@ -26,7 +26,7 @@ public class UserController {
@GetMapping
public List<UserEntity> getAllUsers(Pageable pageable) {
return userService.findAll();
return userService.findAllActives();
}
@GetMapping("/{id}")

View File

@ -14,4 +14,5 @@ public interface BaseService<T, ID extends Serializable> {
Page<T> findAll(Pageable pageable, String q);
Page<T> findAll(Pageable pageable);
List<T> findAll();
List<T> findAllActives();
}

View File

@ -18,4 +18,9 @@ public abstract class BaseServiceImpl<T, ID extends Serializable> implements Bas
public Page<T> findAll(Pageable pageable) {
return getRepository().findAll(pageable);
}
@Override
public List<T> findAllActives() {
return getRepository().findAllActives();
}
}

View File

@ -4,8 +4,12 @@ import com.cubetiqs.demo.domain.PostEntity;
import com.cubetiqs.demo.repository.BaseRepository;
import com.cubetiqs.demo.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PostServiceImpl extends BaseServiceImpl<PostEntity, Long> implements PostService {
private final PostRepository postRepository;
@ -19,4 +23,14 @@ public class PostServiceImpl extends BaseServiceImpl<PostEntity, Long> implement
public BaseRepository<PostEntity, Long> getRepository() {
return postRepository;
}
@Override
public Page<PostEntity> findAll(Pageable pageable, String q) {
return null;
}
@Override
public List<PostEntity> findAllActives() {
return postRepository.findAllActives();
}
}

View File

@ -4,8 +4,12 @@ import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.repository.BaseRepository;
import com.cubetiqs.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implements UserService {
private final UserRepository userRepository;
@ -19,4 +23,14 @@ public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implement
public BaseRepository<UserEntity, Long> getRepository() {
return userRepository;
}
@Override
public Page<UserEntity> findAll(Pageable pageable, String q) {
return null;
}
@Override
public List<UserEntity> findAllActives() {
return userRepository.findAllActives();
}
}