-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserController.java
63 lines (52 loc) · 2.2 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
package com.music.review.app.controllers;
import com.music.review.app.domain.entities.users.dtos.UserCreateDTO;
import com.music.review.app.domain.entities.users.dtos.UserGetDTO;
import com.music.review.app.domain.entities.users.dtos.UserUpdateDTO;
import com.music.review.app.services.UserService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
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("users")
@SecurityRequirement(name = "bearer-key")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService){
this.userService = userService;
}
@PostMapping
public ResponseEntity<UserGetDTO> createUser(@RequestBody @Valid UserCreateDTO userCreateDTO){
return ResponseEntity.status(HttpStatus.CREATED)
.body(this.userService.saveUser(userCreateDTO));
}
@GetMapping("/id/{id}")
public ResponseEntity<UserGetDTO> findByIdUser(@PathVariable Long id){
return ResponseEntity.status(HttpStatus.OK)
.body(new UserGetDTO(this.userService.findById(id)));
}
@GetMapping("/email/{email}")
public ResponseEntity<UserGetDTO> findByEmailUser(@PathVariable String email){
return ResponseEntity.status(HttpStatus.OK)
.body(new UserGetDTO(this.userService.findByEmail(email)));
}
@GetMapping
public ResponseEntity<List<UserGetDTO>> findAllUsers(){
return ResponseEntity.status(HttpStatus.OK)
.body(this.userService.findAll());
}
@DeleteMapping("/{id}")
public ResponseEntity<Object> deleteUser(@PathVariable Long id){
this.userService.deleteById(id);
return ResponseEntity.noContent().build();
}
@PutMapping
public ResponseEntity<UserGetDTO> updateUser(@RequestBody @Valid UserUpdateDTO userUpdateDTO){
return ResponseEntity.status(HttpStatus.OK)
.body(this.userService.update(userUpdateDTO));
}
}