|
1 | 1 | package com.sap.demo; |
2 | 2 |
|
| 3 | +import io.swagger.annotations.ApiOperation; |
| 4 | +import io.swagger.annotations.ApiParam; |
3 | 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.data.domain.Page; |
| 7 | +import org.springframework.data.domain.PageRequest; |
| 8 | +import org.springframework.data.domain.Pageable; |
| 9 | +import org.springframework.data.domain.Sort; |
| 10 | +import org.springframework.http.ResponseEntity; |
4 | 11 | import org.springframework.web.bind.annotation.*; |
5 | 12 |
|
| 13 | +import javax.validation.Valid; |
| 14 | +import java.util.Optional; |
| 15 | + |
6 | 16 | @RestController() |
7 | | -@RequestMapping(path = "/users") |
| 17 | +@RequestMapping(path = "/v1/users") |
8 | 18 | public class UserController { |
9 | 19 |
|
10 | | - @Autowired // This means to get the bean called userRepository |
11 | | - // Which is auto-generated by Spring, we will use it to handle the data |
12 | | - private UserRepository userRepository; |
| 20 | + private static final String DEFAULT_PAGE_SIZE = "10"; |
| 21 | + private static final String DEFAULT_PAGE_INDEX = "0"; |
| 22 | + private static final String DEFAULT_PAGE_SORT_DIRECTION = "ASC"; |
| 23 | + |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private UserService userService; |
| 27 | + |
13 | 28 |
|
14 | | - @PostMapping(path = "/") // Map ONLY GET Requests |
| 29 | + @PostMapping() |
| 30 | + @ApiOperation( |
| 31 | + value = "Add a new user", |
| 32 | + notes = "Returns the same user added.", |
| 33 | + response = User.class) |
15 | 34 | public @ResponseBody |
16 | | - User addUser(@RequestParam String name |
17 | | - , @RequestParam String email |
18 | | - , @RequestParam String locale) { |
19 | | - // @ResponseBody means the returned String is the response, not a view name |
20 | | - // @RequestParam means it is a parameter from the GET or POST request |
21 | | - |
22 | | - User n = new User(); |
23 | | - n.setName(name); |
24 | | - n.setEmail(email); |
25 | | - n.setLocale(locale); |
26 | | - userRepository.save(n); |
27 | | - return n; |
| 35 | + ResponseEntity<User> addUser(@Valid @RequestBody User user) { |
| 36 | + userService.save(user); |
| 37 | + return ResponseEntity.ok().body(user); |
28 | 38 | } |
29 | 39 |
|
30 | | - @GetMapping(path = "/") |
| 40 | + @GetMapping() |
| 41 | + @ApiOperation( |
| 42 | + value = "Get all users", |
| 43 | + notes = "Returns first N users specified by the size parameter with page offset specified by page parameter.", |
| 44 | + response = Page.class) |
31 | 45 | public @ResponseBody |
32 | | - Iterable<User> getAllUsers() { |
33 | | - // This returns a JSON or XML with the users |
34 | | - return userRepository.findAll(); |
| 46 | + Page<User> getAllUsers( |
| 47 | + @ApiParam("The size of the page to be returned") @RequestParam(required = false, defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize, |
| 48 | + @ApiParam("Zero-based page index") @RequestParam(required = false, defaultValue = DEFAULT_PAGE_INDEX) Integer pageNo, |
| 49 | + @ApiParam("Field name to sort by") @RequestParam(required = false, defaultValue = "name") String sortBy, |
| 50 | + @ApiParam("Direction to sort by") @RequestParam(required = false, defaultValue = DEFAULT_PAGE_SORT_DIRECTION) Sort.Direction direction |
| 51 | + ) { |
| 52 | + Pageable paging = PageRequest.of(pageNo, pageSize, direction, sortBy); |
| 53 | + return userService.findAll(paging); |
35 | 54 | } |
36 | 55 |
|
37 | 56 | @GetMapping(path = "/{email}") |
| 57 | + @ApiOperation( |
| 58 | + value = "Find user by email address", |
| 59 | + notes = "Returns the user matched by the given email.", |
| 60 | + response = User.class) |
38 | 61 | public @ResponseBody |
39 | | - User getUserByEmail(@PathVariable("email") String email) { |
40 | | - return userRepository.findByEmail(email).orElseThrow(() -> new NotFoundError()); |
| 62 | + ResponseEntity<User> getUserByEmail(@PathVariable("email") String email) { |
| 63 | + Optional<User> user = userService.findByEmail(email); |
| 64 | + return ResponseEntity.of(user); |
41 | 65 | } |
42 | 66 |
|
43 | 67 | } |
0 commit comments