Add Repository and Service

This commit is contained in:
Sambo Chea 2020-06-17 17:54:21 +07:00
parent 9ee0199b50
commit 50d6479c8d
12 changed files with 108 additions and 15 deletions

5
homework/Home.md Normal file
View File

@ -0,0 +1,5 @@
### Homework
- [ ] Create project all functions from start
- [ ] Add functions into BaseService (CRUD)
- [ ] Create BaseController

View File

@ -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> {
}

View File

@ -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<PostEntity, Long> {
public interface PostRepository extends BaseRepository<PostEntity, Long> {
}

View File

@ -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<UserEntity, Long> {
public interface UserRepository extends BaseRepository<UserEntity, Long> {
}

View 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();
}
}

View File

@ -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<UserEntity> getAllUsers(Pageable pageable) {
return userService.findAllUsers();
return userService.findAll();
}
@GetMapping("/{id}")

View 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();
}

View 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();
}
}

View 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> {
}

View 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;
}
}

View File

@ -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<UserEntity> findAllUsers();
public interface UserService extends BaseService<UserEntity, Long> {
}

View File

@ -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<UserEntity, Long> implements UserService {
private final UserRepository userRepository;
@Autowired
@ -17,7 +16,7 @@ public class UserServiceImpl implements UserService {
}
@Override
public List<UserEntity> findAllUsers() {
return userRepository.findAll();
public BaseRepository<UserEntity, Long> getRepository() {
return userRepository;
}
}