-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathUserController.java
More file actions
53 lines (45 loc) · 1.89 KB
/
UserController.java
File metadata and controls
53 lines (45 loc) · 1.89 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
package com.ironhack.users_micro.controller;
import com.ironhack.users_micro.dto.UserPatchAccountDTO;
import com.ironhack.users_micro.dto.UserResponseDTO;
import com.ironhack.users_micro.exception.UserNotFoundException;
import com.ironhack.users_micro.model.User;
import com.ironhack.users_micro.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/user")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/{id}")
public ResponseEntity<?> getUserById(@PathVariable long id) {
try {
UserResponseDTO foundUser = userService.getUserById(id);
return new ResponseEntity<>(foundUser, HttpStatus.FOUND);
} catch (UserNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
User createdUser = userService.createUser(user);
return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
}
@PatchMapping("/account/{userId}")
public ResponseEntity<?> patchAccountId(@RequestBody UserPatchAccountDTO userPatchAccountDTO, @PathVariable("userId") Long userId) {
try {
User updatedUser = userService.patchAccountId(userId, userPatchAccountDTO.getAccountID());
return new ResponseEntity<>(updatedUser, HttpStatus.ACCEPTED);
} catch (UserNotFoundException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
}
}
}