From a0fd9e13b90f1d0d6a26de49d08803a63a53f173 Mon Sep 17 00:00:00 2001 From: Bogdan <97698639+don-bigdad@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:04:00 +0300 Subject: [PATCH] mvn fix --- .../controller/AuthenticationController.java | 8 ++-- .../springbootbookshop/dto/UserDto.java | 12 ++---- .../dto/UserRegisterResponseDto.java | 5 ++- .../springbootbookshop/mapper/UserMapper.java | 9 +---- .../service/UserService.java | 4 +- .../service/impl/UserServiceImpl.java | 13 ++----- .../changes/02-create-users-table.yaml | 2 +- .../changes/03-create-roles-table.yaml | 20 +++++++++- .../changes/04-create-users-roles.yaml | 38 +++++++++++++++++++ .../db/changelog/db.changelog-master.yaml | 4 +- 10 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 src/main/resources/db/changelog/changes/04-create-users-roles.yaml diff --git a/src/main/java/com/example/springbootbookshop/controller/AuthenticationController.java b/src/main/java/com/example/springbootbookshop/controller/AuthenticationController.java index 08b523f..c5a917d 100644 --- a/src/main/java/com/example/springbootbookshop/controller/AuthenticationController.java +++ b/src/main/java/com/example/springbootbookshop/controller/AuthenticationController.java @@ -1,8 +1,8 @@ package com.example.springbootbookshop.controller; +import com.example.springbootbookshop.dto.UserDto; import com.example.springbootbookshop.dto.UserLoginRequestDto; import com.example.springbootbookshop.dto.UserLoginResponseDto; -import com.example.springbootbookshop.dto.UserRegisterResponseDto; import com.example.springbootbookshop.dto.UserRegistrationRequestDto; import com.example.springbootbookshop.exception.RegistrationException; import com.example.springbootbookshop.security.AuthenticationService; @@ -24,13 +24,13 @@ public class AuthenticationController { private final AuthenticationService authenticationService; @PostMapping(value = "/register") - public UserRegisterResponseDto register(@RequestBody @Valid - UserRegistrationRequestDto requestDto) + public UserDto register(@RequestBody @Valid + UserRegistrationRequestDto requestDto) throws RegistrationException { return userService.register(requestDto); } - @PostMapping(value = "/login") + @PostMapping(value = "/login1") public UserLoginResponseDto login(@RequestBody @Valid UserLoginRequestDto requestDto) { return authenticationService.authenticate(requestDto); } diff --git a/src/main/java/com/example/springbootbookshop/dto/UserDto.java b/src/main/java/com/example/springbootbookshop/dto/UserDto.java index 3d8bc52..4e1e25d 100644 --- a/src/main/java/com/example/springbootbookshop/dto/UserDto.java +++ b/src/main/java/com/example/springbootbookshop/dto/UserDto.java @@ -1,12 +1,8 @@ package com.example.springbootbookshop.dto; -import com.example.springbootbookshop.entity.Role; -import java.util.Set; - public record UserDto(Long id, - String email, - String firstName, - String lastName, - String shippingAddress, - Set roles) { + String email, + String firstName, + String lastName, + String shippingAddress) { } diff --git a/src/main/java/com/example/springbootbookshop/dto/UserRegisterResponseDto.java b/src/main/java/com/example/springbootbookshop/dto/UserRegisterResponseDto.java index addfad7..2c8c2fe 100644 --- a/src/main/java/com/example/springbootbookshop/dto/UserRegisterResponseDto.java +++ b/src/main/java/com/example/springbootbookshop/dto/UserRegisterResponseDto.java @@ -1,5 +1,8 @@ package com.example.springbootbookshop.dto; public record UserRegisterResponseDto(Long id, - String email) { + String email, + String firstName, + String lastName, + String shippingAddress) { } diff --git a/src/main/java/com/example/springbootbookshop/mapper/UserMapper.java b/src/main/java/com/example/springbootbookshop/mapper/UserMapper.java index e309123..f37fe27 100644 --- a/src/main/java/com/example/springbootbookshop/mapper/UserMapper.java +++ b/src/main/java/com/example/springbootbookshop/mapper/UserMapper.java @@ -1,8 +1,7 @@ package com.example.springbootbookshop.mapper; import com.example.springbootbookshop.dto.UserDto; -import com.example.springbootbookshop.dto.UserLoginResponseDto; -import com.example.springbootbookshop.dto.UserRegisterResponseDto; +import com.example.springbootbookshop.dto.UserRegistrationRequestDto; import com.example.springbootbookshop.entity.User; import org.mapstruct.InjectionStrategy; import org.mapstruct.Mapper; @@ -15,9 +14,5 @@ public interface UserMapper { UserDto toDto(User user); - User toUserModel(UserDto userDto); - - UserRegisterResponseDto toResponseDto(User user); - - UserLoginResponseDto toLoginResponseDto(User user); + User toUserModel(UserRegistrationRequestDto userDto); } diff --git a/src/main/java/com/example/springbootbookshop/service/UserService.java b/src/main/java/com/example/springbootbookshop/service/UserService.java index 3eb20d0..dcff2ca 100644 --- a/src/main/java/com/example/springbootbookshop/service/UserService.java +++ b/src/main/java/com/example/springbootbookshop/service/UserService.java @@ -1,10 +1,10 @@ package com.example.springbootbookshop.service; -import com.example.springbootbookshop.dto.UserRegisterResponseDto; +import com.example.springbootbookshop.dto.UserDto; import com.example.springbootbookshop.dto.UserRegistrationRequestDto; import com.example.springbootbookshop.exception.RegistrationException; public interface UserService { - UserRegisterResponseDto register(UserRegistrationRequestDto requestDto) + UserDto register(UserRegistrationRequestDto requestDto) throws RegistrationException; } diff --git a/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java b/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java index 2f2c2be..ede1a23 100644 --- a/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java +++ b/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java @@ -1,6 +1,6 @@ package com.example.springbootbookshop.service.impl; -import com.example.springbootbookshop.dto.UserRegisterResponseDto; +import com.example.springbootbookshop.dto.UserDto; import com.example.springbootbookshop.dto.UserRegistrationRequestDto; import com.example.springbootbookshop.entity.Role; import com.example.springbootbookshop.entity.RoleName; @@ -26,22 +26,17 @@ public class UserServiceImpl implements UserService { private final PasswordEncoder passwordEncoder; @Override - public UserRegisterResponseDto register(UserRegistrationRequestDto requestDto) + public UserDto register(UserRegistrationRequestDto requestDto) throws RegistrationException { if (userRepository.findByEmail(requestDto.email()).isPresent()) { throw new RegistrationException("User with email: " + requestDto.email() + " already exist"); } - User user = new User(); - user.setEmail(requestDto.email()); + User user = userMapper.toUserModel(requestDto); user.setPassword(passwordEncoder.encode(requestDto.password())); - user.setFirstName(requestDto.firstName()); - user.setLastName(requestDto.lastName()); - user.setShippingAddress(requestDto.shippingAddress()); user.setRoles(getUserRole()); - User savedUser = userRepository.save(user); - return userMapper.toResponseDto(savedUser); + return userMapper.toDto(userRepository.save(user)); } private Set getUserRole() { diff --git a/src/main/resources/db/changelog/changes/02-create-users-table.yaml b/src/main/resources/db/changelog/changes/02-create-users-table.yaml index f9e1628..e522f0f 100644 --- a/src/main/resources/db/changelog/changes/02-create-users-table.yaml +++ b/src/main/resources/db/changelog/changes/02-create-users-table.yaml @@ -38,4 +38,4 @@ databaseChangeLog: name: shipping_address type: VARCHAR(255) constraints: - nullable: true + nullable: true \ No newline at end of file diff --git a/src/main/resources/db/changelog/changes/03-create-roles-table.yaml b/src/main/resources/db/changelog/changes/03-create-roles-table.yaml index 83c01cb..c3ad4ab 100644 --- a/src/main/resources/db/changelog/changes/03-create-roles-table.yaml +++ b/src/main/resources/db/changelog/changes/03-create-roles-table.yaml @@ -18,4 +18,22 @@ databaseChangeLog: type: VARCHAR(255) constraints: unique: true - nullable: false \ No newline at end of file + nullable: false + - insert: + tableName: roles + columns: + - column: + name: name + value: USER + - insert: + tableName: roles + columns: + - column: + name: name + value: ADMIN + - insert: + tableName: roles + columns: + - column: + name: name + value: MANAGER \ No newline at end of file diff --git a/src/main/resources/db/changelog/changes/04-create-users-roles.yaml b/src/main/resources/db/changelog/changes/04-create-users-roles.yaml new file mode 100644 index 0000000..64bc0d1 --- /dev/null +++ b/src/main/resources/db/changelog/changes/04-create-users-roles.yaml @@ -0,0 +1,38 @@ +databaseChangeLog: + - changeSet: + id: create-roles-users-junction-table + author: your-author-name + changes: + - createTable: + tableName: users_roles + columns: + - column: + name: user_role_id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + - column: + name: role_id + type: BIGINT + constraints: + foreignKeyName: FK_role_id + - column: + name: user_id + type: BIGINT + constraints: + foreignKeyName: FK_user_id + + - addForeignKeyConstraint: + baseTableName: users_roles + baseColumnNames: user_id + referencedTableName: roles + referencedColumnNames: id + constraintName: FK_role_id + + - addForeignKeyConstraint: + baseTableName: users_roles + baseColumnNames: user_id + referencedTableName: users + referencedColumnNames: id + constraintName: FK_user_id \ No newline at end of file diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 89364df..63e8ca7 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -4,4 +4,6 @@ databaseChangeLog: - include: file: db\changelog\changes\02-create-users-table.yaml - include: - file: db\changelog\changes\03-create-roles-table.yaml \ No newline at end of file + file: db\changelog\changes\03-create-roles-table.yaml + - include: + file: db/changelog/changes/04-create-users-roles.yaml \ No newline at end of file