Compare commits

..

No commits in common. "SHOP-5" and "master" have entirely different histories.

20 changed files with 82 additions and 234 deletions

View file

@ -41,7 +41,6 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.flywaydb:flyway-core") implementation("org.flywaydb:flyway-core")
implementation("org.flywaydb:flyway-database-postgresql:11.10.0") implementation("org.flywaydb:flyway-database-postgresql:11.10.0")
implementation("org.postgresql:postgresql") implementation("org.postgresql:postgresql")

View file

@ -1,8 +1,6 @@
package ru.team58.profileservice.controller; package ru.team58.profileservice.controller;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.team58.profileservice.controller.exceptions.BadRequestException; import ru.team58.profileservice.controller.exceptions.BadRequestException;
import ru.team58.profileservice.controller.exceptions.UnauthorizedException; import ru.team58.profileservice.controller.exceptions.UnauthorizedException;
@ -11,7 +9,7 @@ import ru.team58.profileservice.controller.schemes.CreateUserRequest;
import ru.team58.profileservice.controller.schemes.GetUserResponse; import ru.team58.profileservice.controller.schemes.GetUserResponse;
import ru.team58.profileservice.controller.schemes.UpdateUserRequest; import ru.team58.profileservice.controller.schemes.UpdateUserRequest;
import ru.team58.profileservice.controller.schemes.UserResponse; import ru.team58.profileservice.controller.schemes.UserResponse;
import ru.team58.profileservice.mapper.*; import ru.team58.profileservice.mapper.UserMapper;
import ru.team58.profileservice.service.UserDTO; import ru.team58.profileservice.service.UserDTO;
import ru.team58.profileservice.service.UserService; import ru.team58.profileservice.service.UserService;
import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException; import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException;
@ -24,18 +22,6 @@ public class UserController {
@Autowired @Autowired
UserService userService; UserService userService;
@Autowired
UserResponseMapper userResponseMapper;
@Autowired
GetUserMapper getUserMapper;
@Autowired
UpdateUserMapper updateUserMapper;
@Autowired
CreateUserMapper createUserMapper;
@GetMapping("/") @GetMapping("/")
public UserResponse getMe(Principal principal) { public UserResponse getMe(Principal principal) {
if (principal == null) { if (principal == null) {
@ -48,11 +34,11 @@ public class UserController {
throw new UserNotFoundException(); throw new UserNotFoundException();
} }
return userResponseMapper.toUserResponse(userService.getByUsername(principal.getName())); return UserMapper.INSTANCE.toUserResponse(userService.getByUsername(principal.getName()));
} }
@PutMapping(value = "/", consumes = {MediaType.APPLICATION_JSON_VALUE}) @PutMapping("/")
public void updateMe(Principal principal, @Valid @RequestBody UpdateUserRequest request) { public void updateMe(Principal principal, @RequestBody UpdateUserRequest request) {
if (principal == null) { if (principal == null) {
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
@ -63,7 +49,7 @@ public class UserController {
throw new UserNotFoundException(); throw new UserNotFoundException();
} }
userService.updateUser(updateUserMapper.toUserDTO(request)); userService.updateUser(UserMapper.INSTANCE.toUserDTO(request));
} }
@DeleteMapping("/") @DeleteMapping("/")
@ -83,9 +69,9 @@ public class UserController {
userService.deleteUser(user.getId()); userService.deleteUser(user.getId());
} }
@PostMapping(value = "/", consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping("/")
public void createMe(@Valid @RequestBody CreateUserRequest request) { public void createMe(@RequestBody CreateUserRequest request) {
UserDTO user = createUserMapper.toUserDTO(request); UserDTO user = UserMapper.INSTANCE.toUserDTO(request);
try { try {
userService.createUser(user); userService.createUser(user);
@ -106,7 +92,7 @@ public class UserController {
throw new UserNotFoundException(); throw new UserNotFoundException();
} }
return getUserMapper.toGetUserResponse(user); return UserMapper.INSTANCE.toGetUserResponse(user);
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View file

@ -1,16 +0,0 @@
package ru.team58.profileservice.controller.advice;
import lombok.*;
import org.springframework.http.HttpStatus;
import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
public class ErrorDTO {
HttpStatus status;
List<String> errors;
}

View file

@ -2,63 +2,39 @@ package ru.team58.profileservice.controller.advice;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import ru.team58.profileservice.controller.exceptions.*; import ru.team58.profileservice.controller.exceptions.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ControllerAdvice @ControllerAdvice
public class UserControllerExceptionHandler { public class UserControllerExceptionHandler {
@ExceptionHandler(UserNotFoundException.class) @ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<ErrorDTO> handleUserNotFoundException(UserNotFoundException ex) { public ResponseEntity<ErrorDetails> handleUserNotFoundException(UserNotFoundException ex) {
var error = new ErrorDTO(HttpStatus.FORBIDDEN, List.of("User not found")); ErrorDetails errorDetails = new ErrorDetails("User not found", HttpStatus.FORBIDDEN.value());
return ResponseEntity.status(error.status).body(error); return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN);
} }
@ExceptionHandler(UserAlreadyExistsException.class) @ExceptionHandler(UserAlreadyExistsException.class)
public ResponseEntity<ErrorDTO> handleUserAlreadyExistsException(UserAlreadyExistsException ex) { public ResponseEntity<ErrorDetails> handleUserAlreadyExistsException(UserAlreadyExistsException ex) {
var errorDetails = new ErrorDetails("User already exists", HttpStatus.BAD_REQUEST.value()); ErrorDetails errorDetails = new ErrorDetails("User already exists", HttpStatus.BAD_REQUEST.value());
var error = new ErrorDTO(HttpStatus.BAD_REQUEST, List.of("User already exists")); return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
return ResponseEntity.status(error.status).body(error);
} }
@ExceptionHandler(BadRequestException.class) @ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorDTO> handleBadRequestException(BadRequestException ex) { public ResponseEntity<ErrorDetails> handleBadRequestException(BadRequestException ex) {
var error = new ErrorDTO(HttpStatus.BAD_REQUEST, List.of("Bad request")); ErrorDetails errorDetails = new ErrorDetails("Bad Request", HttpStatus.BAD_REQUEST.value());
return ResponseEntity.status(error.status).body(error); return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
} }
@ExceptionHandler(ForbiddenException.class) @ExceptionHandler(ForbiddenException.class)
public ResponseEntity<ErrorDTO> handleForbiddenException(ForbiddenException ex) { public ResponseEntity<ErrorDetails> handleForbiddenException(ForbiddenException ex) {
var error = new ErrorDTO(HttpStatus.FORBIDDEN, List.of("Forbidden")); ErrorDetails errorDetails = new ErrorDetails("Forbidden", HttpStatus.FORBIDDEN.value());
return ResponseEntity.status(error.status).body(error); return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN);
} }
@ExceptionHandler(UnauthorizedException.class) @ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<ErrorDTO> handleUnauthorizedException(UnauthorizedException ex) { public ResponseEntity<ErrorDetails> handleUnauthorizedException(UnauthorizedException ex) {
var error = new ErrorDTO(HttpStatus.UNAUTHORIZED, List.of("Unauthorized")); ErrorDetails errorDetails = new ErrorDetails("Unauthorized", HttpStatus.UNAUTHORIZED.value());
return ResponseEntity.status(error.status).body(error); return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorDTO> handleValidationExceptions(
MethodArgumentNotValidException ex) {
var errors = new ArrayList<String>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
var fieldName = ((FieldError) error).getField();
var errorMessage = error.getDefaultMessage();
errors.add(String.format("%s: %s", fieldName, errorMessage));
});
var error = new ErrorDTO(HttpStatus.BAD_REQUEST, errors);
return ResponseEntity.status(error.status).body(error);
} }
} }

View file

@ -1,4 +0,0 @@
package ru.team58.profileservice.controller.exceptions;
public class IncorrectEmailException extends BadRequestException {
}

View file

@ -1,10 +1,15 @@
package ru.team58.profileservice.controller.schemes; package ru.team58.profileservice.controller.schemes;
import lombok.*; import lombok.Builder;
import lombok.experimental.SuperBuilder; import lombok.Getter;
import lombok.Setter;
@Getter @Getter
@Setter @Setter
@SuperBuilder @Builder
@NoArgsConstructor public class CreateUserRequest {
public class CreateUserRequest extends UserScheme {} String username;
String firstName;
String lastName;
String email;
}

View file

@ -1,13 +1,18 @@
package ru.team58.profileservice.controller.schemes; package ru.team58.profileservice.controller.schemes;
import lombok.*; import lombok.Builder;
import lombok.experimental.SuperBuilder; import lombok.Getter;
import lombok.Setter;
import java.util.UUID; import java.util.UUID;
@Getter @Getter
@Setter @Setter
@SuperBuilder @Builder
@NoArgsConstructor public class UpdateUserRequest {
public class UpdateUserRequest extends UserScheme { UUID id;
String username;
String firstName;
String lastName;
String email;
} }

View file

@ -1,41 +0,0 @@
package ru.team58.profileservice.controller.schemes;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import java.util.UUID;
@SuperBuilder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class UserScheme {
private UUID id;
@NotBlank(message = "Username cannot be empty")
@Pattern(regexp = "^[a-z0-9.]+$", message = "Username can have only characters: a-z and 0-9")
@Size(min = 3, max = 14, message = "Username must be bigger than 3 and less than 14")
private String username;
@NotBlank(message = "First name cannot be empty")
@Pattern(regexp = "^[a-zA-Zа-яА-Я]*$", message = "First name can contain only letters")
@Size(min = 3, max = 255, message = "First name must be bigger than 3 and less than 255")
private String firstName;
@NotBlank(message = "Last name cannot be empty")
@Pattern(regexp = "^[a-zA-Zа-яА-Я]*$", message = "Last name can contain only letters")
@Size(min = 3, max = 255, message = "Last name must be bigger than 3 and less than 255")
private String lastName;
@NotBlank
@Email(regexp = "[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,3}", message = """
Email can contain: a-z, 0-9, '.', '_', '%', '+', '-'.
And have format like: "example@domain.com".""")
@Size(min = 5, max = 255, message = "Email must be bigger than 5 and less than 255")
private String email;
}

View file

@ -1,12 +0,0 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import ru.team58.profileservice.controller.schemes.CreateUserRequest;
import ru.team58.profileservice.service.UserDTO;
@Mapper(componentModel = "spring")
public interface CreateUserMapper {
@Mapping(target = "id", ignore = true)
UserDTO toUserDTO(CreateUserRequest request);
}

View file

@ -1,11 +0,0 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import ru.team58.profileservice.controller.schemes.GetUserResponse;
import ru.team58.profileservice.service.UserDTO;
@Mapper(componentModel = "spring")
public interface GetUserMapper {
GetUserResponse toGetUserResponse(UserDTO dto);
}

View file

@ -1,10 +0,0 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import ru.team58.profileservice.controller.schemes.UpdateUserRequest;
import ru.team58.profileservice.service.UserDTO;
@Mapper(componentModel = "spring")
public interface UpdateUserMapper {
UserDTO toUserDTO(UpdateUserRequest request);
}

View file

@ -1,10 +0,0 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import ru.team58.profileservice.persistence.entity.UserEntity;
import ru.team58.profileservice.service.UserDTO;
@Mapper(componentModel = "spring")
public interface UserDTOMapper {
UserDTO toUserDTO(UserEntity entity);
}

View file

@ -1,10 +0,0 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import ru.team58.profileservice.persistence.entity.UserEntity;
import ru.team58.profileservice.service.UserDTO;
@Mapper(componentModel = "spring")
public interface UserEntityMapper {
UserEntity toUserEntity(UserDTO dto);
}

View file

@ -0,0 +1,27 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import ru.team58.profileservice.controller.schemes.CreateUserRequest;
import ru.team58.profileservice.controller.schemes.GetUserResponse;
import ru.team58.profileservice.controller.schemes.UpdateUserRequest;
import ru.team58.profileservice.controller.schemes.UserResponse;
import ru.team58.profileservice.persistence.entity.UserEntity;
import ru.team58.profileservice.service.UserDTO;
@Mapper(componentModel = "spring")
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
UserDTO toUserDTO(UserEntity entity);
UserDTO toUserDTO(UpdateUserRequest request);
@Mapping(target = "id", ignore = true)
UserDTO toUserDTO(CreateUserRequest request);
UserEntity toUserEntity(UserDTO dto);
UserResponse toUserResponse(UserDTO dto);
GetUserResponse toGetUserResponse(UserDTO dto);
}

View file

@ -1,10 +0,0 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import ru.team58.profileservice.controller.schemes.UserResponse;
import ru.team58.profileservice.service.UserDTO;
@Mapper(componentModel = "spring")
public interface UserResponseMapper {
UserResponse toUserResponse(UserDTO dto);
}

View file

@ -1,12 +0,0 @@
package ru.team58.profileservice.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import ru.team58.profileservice.controller.schemes.UserScheme;
import ru.team58.profileservice.service.UserDTO;
@Mapper(uses = {CreateUserMapper.class, UpdateUserMapper.class})
public interface UserSchemeMapper {
@Mapping(target = "id", ignore = true)
UserDTO toUserDTO(UserScheme user);
}

View file

@ -1,7 +1,6 @@
package ru.team58.profileservice.persistence.entity; package ru.team58.profileservice.persistence.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.*; import lombok.*;
import java.util.UUID; import java.util.UUID;

View file

@ -1,7 +1,12 @@
package ru.team58.profileservice.service; package ru.team58.profileservice.service;
import jakarta.validation.Valid;
import lombok.*; import lombok.*;
import ru.team58.profileservice.persistence.entity.UserEntity;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@Getter @Getter

View file

@ -2,8 +2,7 @@ package ru.team58.profileservice.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.team58.profileservice.mapper.UserDTOMapper; import ru.team58.profileservice.mapper.UserMapper;
import ru.team58.profileservice.mapper.UserEntityMapper;
import ru.team58.profileservice.persistence.entity.UserEntity; import ru.team58.profileservice.persistence.entity.UserEntity;
import ru.team58.profileservice.persistence.repo.UserRepository; import ru.team58.profileservice.persistence.repo.UserRepository;
import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException; import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException;
@ -16,21 +15,16 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
UserRepository userRepository; UserRepository userRepository;
@Autowired
UserDTOMapper userDTOMapper;
@Autowired
UserEntityMapper userEntityMapper;
@Override @Override
public UserDTO getById(UUID id) { public UserDTO getById(UUID id) {
UserEntity user = userRepository.findById(id).orElseThrow(UserNotFoundException::new); UserEntity user = userRepository.findById(id).orElseThrow(UserNotFoundException::new);
return userDTOMapper.toUserDTO(user); return UserMapper.INSTANCE.toUserDTO(user);
} }
@Override @Override
public UserDTO getByUsername(String username) { public UserDTO getByUsername(String username) {
UserEntity user = userRepository.findByUsername(username).orElseThrow(UserNotFoundException::new); UserEntity user = userRepository.findByUsername(username).orElseThrow(UserNotFoundException::new);
return userDTOMapper.toUserDTO(user); return UserMapper.INSTANCE.toUserDTO(user);
} }
@Override @Override
@ -38,7 +32,7 @@ public class UserServiceImpl implements UserService {
userRepository.findByUsername(user.username).ifPresent(u -> { userRepository.findByUsername(user.username).ifPresent(u -> {
throw new UserAlreadyExistsException(); throw new UserAlreadyExistsException();
}); });
return userRepository.save(userEntityMapper.toUserEntity(user)).getId(); return userRepository.save(UserMapper.INSTANCE.toUserEntity(user)).getId();
} }
@Override @Override
@ -50,6 +44,6 @@ public class UserServiceImpl implements UserService {
@Override @Override
public void updateUser(UserDTO user) { public void updateUser(UserDTO user) {
userRepository.findById(user.getId()).orElseThrow(UserNotFoundException::new); userRepository.findById(user.getId()).orElseThrow(UserNotFoundException::new);
userRepository.save(userEntityMapper.toUserEntity(user)); userRepository.save(UserMapper.INSTANCE.toUserEntity(user));
} }
} }

View file

@ -1,12 +0,0 @@
package ru.team58.profileservice;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ProfileServiceTest {
@Test
public void contextLoads() {
}
}