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

@ -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);
} }

View File

@ -2,6 +2,7 @@ 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.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -9,21 +10,24 @@ 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.findAllUsers();
} }
@GetMapping("/{id}") @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: server:
port: 8080 port: 8081
spring: spring:
jpa: jpa:

View File

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