Add Repository and Service
This commit is contained in:
parent
9ee0199b50
commit
50d6479c8d
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
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
|
||||||
|
}
|
@ -1,10 +1,9 @@
|
|||||||
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> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
27
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
27
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package com.cubetiqs.demo.rest;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import com.cubetiqs.demo.service.PostService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(path = {"/posts"})
|
||||||
|
public class PostController {
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PostController(PostService postService) {
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<PostEntity> getAllPosts(Pageable pageable) {
|
||||||
|
return postService.findAll();
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,6 @@ 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 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;
|
||||||
@ -27,7 +26,7 @@ public class UserController {
|
|||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<UserEntity> getAllUsers(Pageable pageable) {
|
public List<UserEntity> getAllUsers(Pageable pageable) {
|
||||||
return userService.findAllUsers();
|
return userService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
|
13
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
13
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
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();
|
||||||
|
List<T> findAll();
|
||||||
|
}
|
14
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
14
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
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> {
|
||||||
|
}
|
22
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
22
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
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.stereotype.Service;
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
@ -3,9 +3,7 @@ package com.cubetiqs.demo.service;
|
|||||||
import com.cubetiqs.demo.domain.UserEntity;
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public interface UserService {
|
public interface UserService extends BaseService<UserEntity, Long> {
|
||||||
List<UserEntity> findAllUsers();
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
package com.cubetiqs.demo.service;
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.UserEntity;
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
import com.cubetiqs.demo.repository.UserRepository;
|
import com.cubetiqs.demo.repository.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implements UserService {
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -17,7 +16,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<UserEntity> findAllUsers() {
|
public BaseRepository<UserEntity, Long> getRepository() {
|
||||||
return userRepository.findAll();
|
return userRepository;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user