-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathUserController.java
77 lines (67 loc) · 2.82 KB
/
UserController.java
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
package com.accenture.codingtest.springbootcodingtest.controller;
import com.accenture.codingtest.springbootcodingtest.entity.User;
import com.accenture.codingtest.springbootcodingtest.model.Role;
import com.accenture.codingtest.springbootcodingtest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/v1/users/{role}")
public ResponseEntity<List<User>> getAllUsers(@PathVariable("role") String role) {
ResponseEntity<List<User>> response = null;
if(Role.ADMIN.toString().equalsIgnoreCase(role)) {
response = userService.getAllUsers();
} else {
response = new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
return response;
}
@GetMapping("/v1/users/{user_id}/{role}")
public ResponseEntity<User> getUserById(@PathVariable("user_id") String user_id,
@PathVariable("role") String role) {
ResponseEntity<User> response = null;
if(Role.ADMIN.toString().equalsIgnoreCase(role)) {
response = userService.getUserById(user_id);
} else {
response = new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
return response;
}
@PostMapping("/v1/users/{role}")
public ResponseEntity<User> saveUser(@RequestBody User user, @PathVariable("role") String role) {
ResponseEntity<User> response = null;
if(Role.ADMIN.toString().equalsIgnoreCase(role)) {
response = userService.saveUser(user);
} else {
response = new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
return response;
}
@PutMapping("/v1/users")
public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable("role") String role) {
ResponseEntity<User> response = null;
if(Role.ADMIN.toString().equalsIgnoreCase(role)) {
response = userService.updateUser(user);
} else {
response = new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
return response;
}
@DeleteMapping("/v1/users/{user_id}")
public ResponseEntity<Void> deleteUserById(@PathVariable("user_id") String user_id,
@PathVariable("role") String role) {
ResponseEntity<Void> response;
if(Role.ADMIN.toString().equalsIgnoreCase(role)) {
response = userService.deleteUser(user_id);
} else {
response = new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
return response;
}
}