Add user service

This commit is contained in:
Sambo Chea 2020-06-16 19:19:26 +07:00
parent 017e4c1643
commit 9ee0199b50
6 changed files with 44 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package com.cubetiqs.demo.rest;
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;
@ -9,21 +10,24 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping(path = {"/users"})
public class UserController {
private final UserRepository userRepository;
private final UserService userService;
@Autowired
public UserController(UserRepository userRepository) {
public UserController(UserRepository userRepository, UserService userService) {
this.userRepository = userRepository;
this.userService = userService;
}
@GetMapping
public Page<UserEntity> getAllUsers(Pageable pageable) {
return userRepository.findAll(pageable);
public List<UserEntity> getAllUsers(Pageable pageable) {
return userService.findAllUsers();
}
@GetMapping("/{id}")

View File

@ -0,0 +1,11 @@
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();
}

View File

@ -0,0 +1,23 @@
package com.cubetiqs.demo.service;
import com.cubetiqs.demo.domain.UserEntity;
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 {
private final UserRepository userRepository;
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<UserEntity> findAllUsers() {
return userRepository.findAll();
}
}

View File

@ -1,5 +1,5 @@
server:
port: 8080
port: 8081
spring:
jpa:

View File

@ -1,5 +1,5 @@
server:
port: 8080
port: 8081
spring:
jpa: