0.0.10
This commit is contained in:
1 parent
93a145b561
commit
470484064a
8 files changed
+65
-10
No files matched your search
@@ -9,10 +9,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import site.easystartup.web.domain.model.User;
|
||||
import site.easystartup.web.domain.request.LoginRequest;
|
||||
import site.easystartup.web.domain.request.SignupRequest;
|
||||
@@ -26,6 +23,7 @@ import site.easystartup.web.service.AuthService;
|
||||
import site.easystartup.web.service.UserService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.security.Principal;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
@@ -38,6 +36,8 @@ public class AuthController {
|
||||
private final ResponseErrorValidation responseErrorValidation;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
|
||||
|
||||
@PostMapping("/signup")
|
||||
public ResponseEntity<Object> registration(@Valid @RequestBody SignupRequest signupRequest,
|
||||
BindingResult bindingResult) {
|
||||
|
||||
@@ -4,10 +4,12 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import site.easystartup.web.domain.model.User;
|
||||
import site.easystartup.web.domain.response.MessageResponse;
|
||||
import site.easystartup.web.domain.validation.ResponseErrorValidation;
|
||||
import site.easystartup.web.dto.UserDto;
|
||||
import site.easystartup.web.exception.NoPermissionException;
|
||||
@@ -17,6 +19,7 @@ import site.easystartup.web.project.dto.ProjectDto;
|
||||
import site.easystartup.web.project.service.ProjectService;
|
||||
import site.easystartup.web.request.UserRequest;
|
||||
import site.easystartup.web.service.UserService;
|
||||
import site.easystartup.web.util.FastChecksUtil;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.security.Principal;
|
||||
@@ -24,15 +27,31 @@ import java.security.Principal;
|
||||
@RestController
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
private final site.easystartup.web.service.UserService userService;
|
||||
private final ModelMapper modelMapper;
|
||||
private final ResponseErrorValidation responseErrorValidation;
|
||||
private final FastChecksUtil checksUtil;
|
||||
|
||||
@GetMapping("/user/current")
|
||||
public ResponseEntity<Object> getCurrentUser(Principal principal) {
|
||||
//if(principal == null)
|
||||
try {
|
||||
return ResponseEntity.ok().body(modelMapper.map(userService.getUserByPrincipal(principal), UserDto.class));
|
||||
} catch(Throwable e) {
|
||||
return ResponseEntity.badRequest().body(new MessageResponse("User not found"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("/user/{username}")
|
||||
public ResponseEntity<UserDto> user(@PathVariable String username) {
|
||||
return ResponseEntity.ok().body(modelMapper.map(userService.getUserByUsername(username), UserDto.class));
|
||||
public ResponseEntity<UserDto> getUser(@PathVariable String username) {
|
||||
Long id = Long.parseLong(username);
|
||||
ResponseEntity<UserDto> result = checksUtil.nullIfValid(id);
|
||||
return result == null ? ResponseEntity.ok().body(modelMapper.map(userService.getUserByUsername(username), UserDto.class)) : result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PutMapping("/user/edit/{username}")
|
||||
public ResponseEntity<Object> userEdit(@PathVariable String username,@Valid @RequestBody UserRequest userRequest,
|
||||
BindingResult bindingResult, Principal principal) {
|
||||
@@ -40,7 +59,7 @@ public class UserController {
|
||||
throw new NoPermissionException(String.format("User %s can not edit profile of user %s", principal.getName(),
|
||||
username));
|
||||
|
||||
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||
|
||||
User editedUser = userService.editUser(username, userRequest);
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.util.Map;
|
||||
@Service
|
||||
public class ResponseErrorValidation {
|
||||
|
||||
public ResponseEntity<Object> mapValidationService(BindingResult result) {
|
||||
public<T> ResponseEntity<Object> mapValidationService(BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
Map<String, String> errorMap = new HashMap<>();
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ public class ForumController {
|
||||
public ResponseEntity<List<DiscussionDto>> my(Principal principal) {
|
||||
var discussions = forumService.getDiscussionsByAuthor(principal)
|
||||
.stream().map(discussion -> modelMapper.map(discussion, DiscussionDto.class)).toList();
|
||||
System.out.println(principal.getName());
|
||||
return ResponseEntity.ok().body(discussions);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,4 +34,6 @@ public class JWTUtil {
|
||||
return decodedJWT.getClaim("username").asString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@ public class UserService {
|
||||
private final StorageService storageService;
|
||||
|
||||
public User getUserByPrincipal(Principal principal) {
|
||||
if(principal == null) return getCurrent();
|
||||
return userRepo.findByUsername(principal.getName());
|
||||
}
|
||||
|
||||
@@ -29,6 +30,11 @@ public class UserService {
|
||||
return userRepo.findById(userId).orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||
}
|
||||
|
||||
public boolean userExists(Long userId) {
|
||||
return userRepo.existsById(userId);
|
||||
}
|
||||
|
||||
|
||||
public User getUserByUsername(String username) {
|
||||
return userRepo.findUserByUsername(username).orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||
}
|
||||
@@ -49,4 +55,10 @@ public class UserService {
|
||||
user.setRequests(userRequest.getRequests() == null ? user.getRequests() : userRequest.getRequests());
|
||||
return userRepo.save(user);
|
||||
}
|
||||
|
||||
public User getCurrent() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
String login = authentication.getName();
|
||||
return getUserByUsername(login);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package site.easystartup.web.util;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import site.easystartup.web.service.UserService;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class FastChecksUtil {
|
||||
private final UserService userService;
|
||||
|
||||
public<T> ResponseEntity<T> nullIfValid(Long user) {
|
||||
if(!userService.userExists(user)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user