CRUD (sans auth) sur la ressource user
This commit is contained in:
parent
189245ef42
commit
d769bee503
@ -1,6 +1,7 @@
|
||||
package com.guams.review.controller;
|
||||
|
||||
import com.guams.review.modele.User;
|
||||
import com.guams.review.repository.UserRepository;
|
||||
import com.guams.review.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
@ -14,31 +15,67 @@ import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "api/users")
|
||||
public class UserController
|
||||
{
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public UserController(UserService userService) {
|
||||
public UserController(UserService userService, UserRepository userRepository) {
|
||||
this.userService = userService;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<User>> getUsers()
|
||||
{
|
||||
public ResponseEntity<List<User>> getUsers() {
|
||||
List<User> users = userService.getUsers();
|
||||
return new ResponseEntity<>(users, new HttpHeaders(),HttpStatus.OK);
|
||||
return new ResponseEntity<>(users, new HttpHeaders(), HttpStatus.OK);
|
||||
};
|
||||
|
||||
@GetMapping("/{user-id}")
|
||||
public ResponseEntity<User> getUser(@PathVariable("user-id") Long id) {
|
||||
Optional<User> exists = userRepository.findById(id);
|
||||
if (exists.isPresent()) {
|
||||
return new ResponseEntity<>(exists.get(), new HttpHeaders(), HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>(null, new HttpHeaders(), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<String> saveUser(@RequestParam User user)
|
||||
{
|
||||
public ResponseEntity<String> saveUser(@RequestBody User user) {
|
||||
userService.saveUser(user);
|
||||
Optional<User> exists = userService.findById(user.getId());
|
||||
if (exists.isPresent())
|
||||
{
|
||||
if (exists.isPresent()) {
|
||||
return new ResponseEntity<>("Created", new HttpHeaders(), HttpStatus.CREATED);
|
||||
}
|
||||
return new ResponseEntity<>("Error", new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@PutMapping("/{user-id}")
|
||||
public ResponseEntity<String> updateUser(@PathVariable("user-id") Long id, @RequestBody User user)
|
||||
{
|
||||
Optional<User> exists = userService.findById(id);
|
||||
if (exists.isPresent()) {
|
||||
User newUser = exists.get();
|
||||
newUser.setLogin(user.getLogin());
|
||||
newUser.setPassword(user.getPassword());
|
||||
newUser.setPosts(user.getPosts());
|
||||
userService.saveUser(newUser);
|
||||
return new ResponseEntity<>("Updated", new HttpHeaders(), HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>("User not found", new HttpHeaders(), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@DeleteMapping("{user-id}")
|
||||
public ResponseEntity<String> deleteUser(@PathVariable("user-id") Long id)
|
||||
{
|
||||
Optional<User> exists = userService.findById(id);
|
||||
if (exists.isPresent()) {
|
||||
userRepository.delete(exists.get());
|
||||
return new ResponseEntity<>("Deleted", new HttpHeaders(), HttpStatus.OK);
|
||||
|
||||
}
|
||||
return new ResponseEntity<>("User not found", new HttpHeaders(), HttpStatus.NOT_FOUND);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ public class User {
|
||||
@Column(name = "PASSWORD", nullable = false)
|
||||
private String password;
|
||||
|
||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@OneToMany(mappedBy = "user")
|
||||
private List<Post> posts;
|
||||
|
||||
|
||||
|
@ -33,4 +33,5 @@ public class UserService
|
||||
{
|
||||
return userRepository.findById(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user