diff --git a/homework/Home.md b/homework/Home.md new file mode 100644 index 0000000..7300a26 --- /dev/null +++ b/homework/Home.md @@ -0,0 +1,5 @@ +### Homework + +- [ ] Create project all functions from start +- [ ] Add functions into BaseService (CRUD) +- [ ] Create BaseController \ No newline at end of file diff --git a/src/main/java/com/cubetiqs/demo/repository/BaseRepository.java b/src/main/java/com/cubetiqs/demo/repository/BaseRepository.java new file mode 100644 index 0000000..c5ace18 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/repository/BaseRepository.java @@ -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 extends JpaRepository { +} diff --git a/src/main/java/com/cubetiqs/demo/repository/PostRepository.java b/src/main/java/com/cubetiqs/demo/repository/PostRepository.java index 2b4683f..91968ed 100644 --- a/src/main/java/com/cubetiqs/demo/repository/PostRepository.java +++ b/src/main/java/com/cubetiqs/demo/repository/PostRepository.java @@ -1,10 +1,9 @@ package com.cubetiqs.demo.repository; import com.cubetiqs.demo.domain.PostEntity; -import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface PostRepository extends JpaRepository { +public interface PostRepository extends BaseRepository { } diff --git a/src/main/java/com/cubetiqs/demo/repository/UserRepository.java b/src/main/java/com/cubetiqs/demo/repository/UserRepository.java index 977d2f3..e44b8c8 100644 --- a/src/main/java/com/cubetiqs/demo/repository/UserRepository.java +++ b/src/main/java/com/cubetiqs/demo/repository/UserRepository.java @@ -1,10 +1,9 @@ package com.cubetiqs.demo.repository; import com.cubetiqs.demo.domain.UserEntity; -import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface UserRepository extends JpaRepository { +public interface UserRepository extends BaseRepository { } diff --git a/src/main/java/com/cubetiqs/demo/rest/PostController.java b/src/main/java/com/cubetiqs/demo/rest/PostController.java new file mode 100644 index 0000000..6c38946 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/rest/PostController.java @@ -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 getAllPosts(Pageable pageable) { + return postService.findAll(); + } +} diff --git a/src/main/java/com/cubetiqs/demo/rest/UserController.java b/src/main/java/com/cubetiqs/demo/rest/UserController.java index 9714860..8bc25bf 100644 --- a/src/main/java/com/cubetiqs/demo/rest/UserController.java +++ b/src/main/java/com/cubetiqs/demo/rest/UserController.java @@ -4,7 +4,6 @@ import com.cubetiqs.demo.domain.UserEntity; import com.cubetiqs.demo.repository.UserRepository; import com.cubetiqs.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -27,7 +26,7 @@ public class UserController { @GetMapping public List getAllUsers(Pageable pageable) { - return userService.findAllUsers(); + return userService.findAll(); } @GetMapping("/{id}") diff --git a/src/main/java/com/cubetiqs/demo/service/BaseService.java b/src/main/java/com/cubetiqs/demo/service/BaseService.java new file mode 100644 index 0000000..946b31d --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/service/BaseService.java @@ -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 { + BaseRepository getRepository(); + List findAll(); +} diff --git a/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java new file mode 100644 index 0000000..f980f90 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java @@ -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 implements BaseService { + @Override + public List findAll() { + return getRepository().findAll(); + } +} diff --git a/src/main/java/com/cubetiqs/demo/service/PostService.java b/src/main/java/com/cubetiqs/demo/service/PostService.java new file mode 100644 index 0000000..468c3b1 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/service/PostService.java @@ -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 { +} diff --git a/src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java new file mode 100644 index 0000000..b0b897f --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java @@ -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 implements PostService { + private final PostRepository postRepository; + + @Autowired + public PostServiceImpl(PostRepository postRepository) { + this.postRepository = postRepository; + } + + @Override + public BaseRepository getRepository() { + return postRepository; + } +} diff --git a/src/main/java/com/cubetiqs/demo/service/UserService.java b/src/main/java/com/cubetiqs/demo/service/UserService.java index fed87c3..a5b3220 100644 --- a/src/main/java/com/cubetiqs/demo/service/UserService.java +++ b/src/main/java/com/cubetiqs/demo/service/UserService.java @@ -3,9 +3,7 @@ package com.cubetiqs.demo.service; import com.cubetiqs.demo.domain.UserEntity; import org.springframework.stereotype.Service; -import java.util.List; - @Service -public interface UserService { - List findAllUsers(); +public interface UserService extends BaseService { + } diff --git a/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java index 6c63bd5..f645f0f 100644 --- a/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java +++ b/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java @@ -1,14 +1,13 @@ 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.stereotype.Service; -import java.util.List; - @Service -public class UserServiceImpl implements UserService { +public class UserServiceImpl extends BaseServiceImpl implements UserService { private final UserRepository userRepository; @Autowired @@ -17,7 +16,7 @@ public class UserServiceImpl implements UserService { } @Override - public List findAllUsers() { - return userRepository.findAll(); + public BaseRepository getRepository() { + return userRepository; } }