|
|
|
@@ -3,7 +3,9 @@ package com.faf223.expensetrackerfaf.controller;
|
|
|
|
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
|
|
|
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
|
|
|
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
|
|
|
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
|
|
|
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
|
|
|
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
|
|
|
|
|
|
|
import com.faf223.expensetrackerfaf.model.Credential;
|
|
|
|
import com.faf223.expensetrackerfaf.model.User;
|
|
|
|
import com.faf223.expensetrackerfaf.model.User;
|
|
|
|
|
|
|
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
|
|
|
import com.faf223.expensetrackerfaf.service.UserService;
|
|
|
|
import com.faf223.expensetrackerfaf.service.UserService;
|
|
|
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
|
|
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
|
|
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
|
|
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
|
|
|
@@ -19,6 +21,9 @@ import org.springframework.validation.BindingResult;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/users")
|
|
|
|
@RequestMapping("/users")
|
|
|
|
@@ -27,6 +32,7 @@ public class UserController {
|
|
|
|
|
|
|
|
|
|
|
|
private final UserService userService;
|
|
|
|
private final UserService userService;
|
|
|
|
private final UserMapper userMapper;
|
|
|
|
private final UserMapper userMapper;
|
|
|
|
|
|
|
|
private final CredentialRepository credentialRepository;
|
|
|
|
|
|
|
|
|
|
|
|
@PatchMapping()
|
|
|
|
@PatchMapping()
|
|
|
|
public ResponseEntity<UserDTO> updateUser(@RequestBody @Valid UserCreationDTO userDTO,
|
|
|
|
public ResponseEntity<UserDTO> updateUser(@RequestBody @Valid UserCreationDTO userDTO,
|
|
|
|
@@ -35,21 +41,34 @@ public class UserController {
|
|
|
|
throw new UserNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
|
|
|
throw new UserNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
|
|
|
|
|
|
|
|
|
|
|
User user = userMapper.toUser(userDTO);
|
|
|
|
User user = userMapper.toUser(userDTO);
|
|
|
|
|
|
|
|
|
|
|
|
if (!bindingResult.hasErrors()) {
|
|
|
|
if (!bindingResult.hasErrors()) {
|
|
|
|
|
|
|
|
|
|
|
|
userService.updateUser(user);
|
|
|
|
userService.updateUser(user);
|
|
|
|
return ResponseEntity.ok(userMapper.toDto(user));
|
|
|
|
return ResponseEntity.ok(userMapper.toDto(user));
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new UserNotFoundException("The user has not been found");
|
|
|
|
throw new UserNotFoundException("The user has not been found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/get-user-data")
|
|
|
|
@GetMapping("/get-user-data")
|
|
|
|
public ResponseEntity<UserDTO> getUser() {
|
|
|
|
public ResponseEntity<Map<String, String>> getUser() {
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
|
|
|
|
|
|
|
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
|
|
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
|
|
|
User user = userService.getUserByEmail(userDetails.getUsername());
|
|
|
|
User user = userService.getUserByEmail(userDetails.getUsername());
|
|
|
|
if (user != null) return ResponseEntity.ok(userMapper.toDto(user));
|
|
|
|
Optional<Credential> credential = credentialRepository.findByUser(user);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (credential.isPresent()) {
|
|
|
|
|
|
|
|
Map<String, String> userData = new HashMap<>();
|
|
|
|
|
|
|
|
userData.put("firstname", user.getFirstName());
|
|
|
|
|
|
|
|
userData.put("lastname", user.getLastName());
|
|
|
|
|
|
|
|
userData.put("username", user.getUsername());
|
|
|
|
|
|
|
|
userData.put("userrole", credential.get().getRole().toString()); // Assuming UserRole is an enum
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(userData);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new UserNotFoundException("The user has not been found");
|
|
|
|
throw new UserNotFoundException("The user has not been found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|