From f14bede40b60b516b2f02ca70c930604ff464008 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 18 Jun 2020 19:47:20 +0700 Subject: [PATCH] Add base repository --- .../java/com/cubetiqs/demo/Constants.java | 5 ++++ .../demo/config/BaseRepositoryConfig.java | 10 +++++++ .../com/cubetiqs/demo/domain/BaseEntity.java | 13 ++++++++ .../java/com/cubetiqs/demo/domain/Status.java | 6 ++++ .../demo/repository/BaseRepository.java | 2 ++ .../demo/repository/BaseRepositoryImpl.java | 30 +++++++++++++++++++ .../demo/repository/PostRepository.java | 3 +- .../cubetiqs/demo/rest/PostController.java | 2 +- .../cubetiqs/demo/rest/UserController.java | 2 +- .../cubetiqs/demo/service/BaseService.java | 1 + .../demo/service/BaseServiceImpl.java | 5 ++++ .../demo/service/PostServiceImpl.java | 14 +++++++++ .../demo/service/UserServiceImpl.java | 14 +++++++++ 13 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/cubetiqs/demo/Constants.java create mode 100644 src/main/java/com/cubetiqs/demo/config/BaseRepositoryConfig.java create mode 100644 src/main/java/com/cubetiqs/demo/domain/Status.java create mode 100644 src/main/java/com/cubetiqs/demo/repository/BaseRepositoryImpl.java diff --git a/src/main/java/com/cubetiqs/demo/Constants.java b/src/main/java/com/cubetiqs/demo/Constants.java new file mode 100644 index 0000000..9211544 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/Constants.java @@ -0,0 +1,5 @@ +package com.cubetiqs.demo; + +public final class Constants { + public static final String STATUS = "status"; +} diff --git a/src/main/java/com/cubetiqs/demo/config/BaseRepositoryConfig.java b/src/main/java/com/cubetiqs/demo/config/BaseRepositoryConfig.java new file mode 100644 index 0000000..af58d5d --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/config/BaseRepositoryConfig.java @@ -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 { +} diff --git a/src/main/java/com/cubetiqs/demo/domain/BaseEntity.java b/src/main/java/com/cubetiqs/demo/domain/BaseEntity.java index 622c404..50f49a9 100644 --- a/src/main/java/com/cubetiqs/demo/domain/BaseEntity.java +++ b/src/main/java/com/cubetiqs/demo/domain/BaseEntity.java @@ -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 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 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) { diff --git a/src/main/java/com/cubetiqs/demo/domain/Status.java b/src/main/java/com/cubetiqs/demo/domain/Status.java new file mode 100644 index 0000000..0bf8f04 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/domain/Status.java @@ -0,0 +1,6 @@ +package com.cubetiqs.demo.domain; + +public enum Status { + ACTIVE, + DEACTIVE +} diff --git a/src/main/java/com/cubetiqs/demo/repository/BaseRepository.java b/src/main/java/com/cubetiqs/demo/repository/BaseRepository.java index c5ace18..21dbe86 100644 --- a/src/main/java/com/cubetiqs/demo/repository/BaseRepository.java +++ b/src/main/java/com/cubetiqs/demo/repository/BaseRepository.java @@ -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 extends JpaRepository { + List findAllActives(); } diff --git a/src/main/java/com/cubetiqs/demo/repository/BaseRepositoryImpl.java b/src/main/java/com/cubetiqs/demo/repository/BaseRepositoryImpl.java new file mode 100644 index 0000000..cd206e1 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/repository/BaseRepositoryImpl.java @@ -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 + extends SimpleJpaRepository + implements BaseRepository { + private final EntityManager entityManager; + public BaseRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { + super(entityInformation, entityManager); + this.entityManager = entityManager; + } + + @Override + public List findAllActives() { + Specification specification = (Specification) (root, query, criteriaBuilder) -> + criteriaBuilder.equal(root.get(Constants.STATUS), Status.ACTIVE); + return findAll(specification); + } +} diff --git a/src/main/java/com/cubetiqs/demo/repository/PostRepository.java b/src/main/java/com/cubetiqs/demo/repository/PostRepository.java index 91968ed..c783bae 100644 --- a/src/main/java/com/cubetiqs/demo/repository/PostRepository.java +++ b/src/main/java/com/cubetiqs/demo/repository/PostRepository.java @@ -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 { - + } diff --git a/src/main/java/com/cubetiqs/demo/rest/PostController.java b/src/main/java/com/cubetiqs/demo/rest/PostController.java index bf4618c..d9de977 100644 --- a/src/main/java/com/cubetiqs/demo/rest/PostController.java +++ b/src/main/java/com/cubetiqs/demo/rest/PostController.java @@ -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)); } diff --git a/src/main/java/com/cubetiqs/demo/rest/UserController.java b/src/main/java/com/cubetiqs/demo/rest/UserController.java index 8bc25bf..052d944 100644 --- a/src/main/java/com/cubetiqs/demo/rest/UserController.java +++ b/src/main/java/com/cubetiqs/demo/rest/UserController.java @@ -26,7 +26,7 @@ public class UserController { @GetMapping public List getAllUsers(Pageable pageable) { - return userService.findAll(); + return userService.findAllActives(); } @GetMapping("/{id}") diff --git a/src/main/java/com/cubetiqs/demo/service/BaseService.java b/src/main/java/com/cubetiqs/demo/service/BaseService.java index 4925b52..00cdd41 100644 --- a/src/main/java/com/cubetiqs/demo/service/BaseService.java +++ b/src/main/java/com/cubetiqs/demo/service/BaseService.java @@ -14,4 +14,5 @@ public interface BaseService { Page findAll(Pageable pageable, String q); Page findAll(Pageable pageable); List findAll(); + List findAllActives(); } diff --git a/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java index a7f9ea3..8bc5b2e 100644 --- a/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java +++ b/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java @@ -18,4 +18,9 @@ public abstract class BaseServiceImpl implements Bas public Page findAll(Pageable pageable) { return getRepository().findAll(pageable); } + + @Override + public List findAllActives() { + return getRepository().findAllActives(); + } } diff --git a/src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java index b0b897f..dc879b7 100644 --- a/src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java +++ b/src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java @@ -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 implements PostService { private final PostRepository postRepository; @@ -19,4 +23,14 @@ public class PostServiceImpl extends BaseServiceImpl implement public BaseRepository getRepository() { return postRepository; } + + @Override + public Page findAll(Pageable pageable, String q) { + return null; + } + + @Override + public List findAllActives() { + return postRepository.findAllActives(); + } } diff --git a/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java index f645f0f..af723e0 100644 --- a/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java +++ b/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java @@ -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 implements UserService { private final UserRepository userRepository; @@ -19,4 +23,14 @@ public class UserServiceImpl extends BaseServiceImpl implement public BaseRepository getRepository() { return userRepository; } + + @Override + public Page findAll(Pageable pageable, String q) { + return null; + } + + @Override + public List findAllActives() { + return userRepository.findAllActives(); + } }