-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathUserController.java
More file actions
135 lines (119 loc) · 3.96 KB
/
UserController.java
File metadata and controls
135 lines (119 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
*
* Copyright (c) 2018-2020 Givantha Kalansuriya, This source is a part of
* Staxrt - sample application source code.
* http://staxrt.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.staxrt.tutorial.controller;
import com.staxrt.tutorial.dto.PageResponseDto;
import com.staxrt.tutorial.dto.UserDto;
import com.staxrt.tutorial.dto.UserResponseDto;
import com.staxrt.tutorial.exception.ResourceNotFoundException;
import com.staxrt.tutorial.exception.UserAlreadyExistException;
import com.staxrt.tutorial.model.User;
import com.staxrt.tutorial.repository.UserRepository;
import com.staxrt.tutorial.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* The type User controller.
*
* @author Givantha Kalansuriya
*/
@RestController
@RequestMapping("/api/v1")
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
UserService userService;
/**
* Get all users list.
*
* @return the list
*/
@GetMapping("/demo")
public String printHello() {
return "Hello World!";
}
@GetMapping("/users")
public ResponseEntity<List<User>> getAllUsers() {
return ResponseEntity.ok(userService.findAllUserse());
}
/**
* Gets users by id.
*
* @Pathvaraible userId the user id
* @return the users by id
* @throws ResourceNotFoundException the resource not found exception
*/
@GetMapping("/users/{id}")
public ResponseEntity<User> getUsersById(@PathVariable(value = "id") Long userId) {
return ResponseEntity.ok(userService.findByUserId(userId));
}
/**
* Create user user.
*
* @RequestBody user
* @return the user
*/
@PostMapping("/users")
public ResponseEntity<UserResponseDto> createUser(@Valid @RequestBody UserDto user) {
return ResponseEntity.ok(userService.createUser(user));
}
/**
* Update user response entity.
*
* @RequestBody user the user id
*
* @return the response entity
* @throws ResourceNotFoundException the resource not found exception
*/
@PutMapping("/users/{id}")
public ResponseEntity<UserResponseDto> updateUser(
@PathVariable(value = "id") Long userId, @Valid @RequestBody UserDto userDetails) {
return ResponseEntity.ok(userService.updateuser(userId, userDetails));
}
/**
* Delete user map.
*
* @Pathvaraible userId the user id
* @return the map
* @throws Exception the exception
*/
@DeleteMapping("/user/{id}")
public ResponseEntity<Map<String, Boolean>> deleteUser(@PathVariable(value = "id") Long userId) {
return ResponseEntity.ok(userService.deleteUser(userId));
}
@GetMapping("/users/getPaginatedusers")
public ResponseEntity<PageResponseDto<UserResponseDto>> getPaginatedUsers(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "5") int size,
@RequestParam(defaultValue = "id") String sortBy,
@RequestParam(defaultValue = "asc") String direction) {
PageResponseDto<UserResponseDto> users = userService.getUsers(page, size, sortBy, direction);
return ResponseEntity.ok(users);
}
}