Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f14bede40b | |||
| 0121fd4bcb | |||
| 50d6479c8d | |||
| 9ee0199b50 |
5
homework/Home.md
Normal file
5
homework/Home.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
### Homework
|
||||||
|
|
||||||
|
- [ ] Create project all functions from start
|
||||||
|
- [ ] Add functions into BaseService (CRUD)
|
||||||
|
- [ ] Create BaseController
|
||||||
5
src/main/java/com/cubetiqs/demo/Constants.java
Normal file
5
src/main/java/com/cubetiqs/demo/Constants.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package com.cubetiqs.demo;
|
||||||
|
|
||||||
|
public final class Constants {
|
||||||
|
public static final String STATUS = "status";
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@ import java.io.File;
|
|||||||
public class DemoApplication implements CommandLineRunner {
|
public class DemoApplication implements CommandLineRunner {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
SpringApplication.run(DemoApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.cubetiqs.demo.domain;
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.Constants;
|
||||||
import org.springframework.data.domain.Persistable;
|
import org.springframework.data.domain.Persistable;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
@@ -16,6 +17,10 @@ public class BaseEntity<ID extends Serializable> implements Serializable, Persis
|
|||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Date createdDate;
|
private Date createdDate;
|
||||||
|
|
||||||
|
@Column(name = Constants.STATUS)
|
||||||
|
@Enumerated(EnumType.ORDINAL)
|
||||||
|
private Status status;
|
||||||
|
|
||||||
public void setId(ID id) {
|
public void setId(ID id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
@@ -38,6 +43,14 @@ public class BaseEntity<ID extends Serializable> implements Serializable, Persis
|
|||||||
this.createdDate = createdDate;
|
this.createdDate = createdDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setStatus(Status status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Status getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
@PrePersist
|
@PrePersist
|
||||||
public void beforeSave() {
|
public void beforeSave() {
|
||||||
if (createdDate == null) {
|
if (createdDate == null) {
|
||||||
|
|||||||
6
src/main/java/com/cubetiqs/demo/domain/Status.java
Normal file
6
src/main/java/com/cubetiqs/demo/domain/Status.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
public enum Status {
|
||||||
|
ACTIVE,
|
||||||
|
DEACTIVE
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package com.cubetiqs.demo.repository;
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.PostEntity;
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface PostRepository extends JpaRepository<PostEntity, Long> {
|
public interface PostRepository extends BaseRepository<PostEntity, Long> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package com.cubetiqs.demo.repository;
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.UserEntity;
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
public interface UserRepository extends BaseRepository<UserEntity, Long> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
33
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.cubetiqs.demo.rest;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.service.PostService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(path = {"/posts"})
|
||||||
|
public class PostController {
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PostController(PostService postService) {
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<Object> getAllPosts(
|
||||||
|
Pageable pageable,
|
||||||
|
@RequestParam(value = "view", defaultValue = "list") String viewType,
|
||||||
|
@RequestParam(value = "q", defaultValue = "") String q
|
||||||
|
) {
|
||||||
|
if ("list".equalsIgnoreCase(viewType)) {
|
||||||
|
return ResponseEntity.ok(postService.findAllActives());
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(postService.findAll(pageable));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,28 +2,31 @@ package com.cubetiqs.demo.rest;
|
|||||||
|
|
||||||
import com.cubetiqs.demo.domain.UserEntity;
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
import com.cubetiqs.demo.repository.UserRepository;
|
import com.cubetiqs.demo.repository.UserRepository;
|
||||||
|
import com.cubetiqs.demo.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(path = {"/users"})
|
@RequestMapping(path = {"/users"})
|
||||||
public class UserController {
|
public class UserController {
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UserController(UserRepository userRepository) {
|
public UserController(UserRepository userRepository, UserService userService) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public Page<UserEntity> getAllUsers(Pageable pageable) {
|
public List<UserEntity> getAllUsers(Pageable pageable) {
|
||||||
return userRepository.findAll(pageable);
|
return userService.findAllActives();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
|
|||||||
18
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
18
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public interface BaseService<T, ID extends Serializable> {
|
||||||
|
BaseRepository<T, ID> getRepository();
|
||||||
|
Page<T> findAll(Pageable pageable, String q);
|
||||||
|
Page<T> findAll(Pageable pageable);
|
||||||
|
List<T> findAll();
|
||||||
|
List<T> findAllActives();
|
||||||
|
}
|
||||||
26
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
26
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public abstract class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID> {
|
||||||
|
@Override
|
||||||
|
public List<T> findAll() {
|
||||||
|
return getRepository().findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<T> findAll(Pageable pageable) {
|
||||||
|
return getRepository().findAll(pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> findAllActives() {
|
||||||
|
return getRepository().findAllActives();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/main/java/com/cubetiqs/demo/service/PostService.java
Normal file
8
src/main/java/com/cubetiqs/demo/service/PostService.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface PostService extends BaseService<PostEntity, Long> {
|
||||||
|
}
|
||||||
36
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
36
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PostServiceImpl(PostRepository postRepository) {
|
||||||
|
this.postRepository = postRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal file
9
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface UserService extends BaseService<UserEntity, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
36
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal file
36
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public UserServiceImpl(UserRepository userRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8080
|
port: 8081
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
jpa:
|
jpa:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8080
|
port: 8081
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
jpa:
|
jpa:
|
||||||
|
|||||||
Reference in New Issue
Block a user