add dto, refactor names, update controllers
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -8,51 +11,56 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
public UserController(UserService userService) {
|
||||
public UserController(UserService userService, UserMapper userMapper) {
|
||||
this.userService = userService;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public ResponseEntity<List<User>> getAllUsers() {
|
||||
List<User> users = userService.getUsers();
|
||||
public ResponseEntity<List<UserDTO>> getAllUsers() {
|
||||
List<UserDTO> users = userService.getUsers().stream().map(userMapper::toDto).collect(Collectors.toList());
|
||||
if (!users.isEmpty()) return ResponseEntity.ok(users);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<User> createNewUser(@RequestBody User user,
|
||||
public ResponseEntity<UserDTO> createNewUser(@RequestBody UserCreationDTO userDTO,
|
||||
BindingResult bindingResult) {
|
||||
User user = userMapper.toUser(userDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
userService.createOrUpdateUser(user);
|
||||
return ResponseEntity.ok(user);
|
||||
return ResponseEntity.ok(userMapper.toDto(user));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@PatchMapping()
|
||||
public ResponseEntity<User> updateUser(@RequestBody User user,
|
||||
public ResponseEntity<UserDTO> updateUser(@RequestBody UserCreationDTO userDTO,
|
||||
BindingResult bindingResult) {
|
||||
User user = userMapper.toUser(userDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
userService.createOrUpdateUser(user);
|
||||
return ResponseEntity.ok(user);
|
||||
return ResponseEntity.ok(userMapper.toDto(user));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/{userUuid}")
|
||||
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
|
||||
public ResponseEntity<UserDTO> getUser(@PathVariable String userUuid) {
|
||||
User user = userService.getUserById(userUuid);
|
||||
if (user != null) return ResponseEntity.ok(user);
|
||||
if (user != null) return ResponseEntity.ok(userMapper.toDto(user));
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user