Skip to content

Commit

Permalink
mvn fix
Browse files Browse the repository at this point in the history
  • Loading branch information
don-bigdad committed Oct 25, 2023
1 parent f4af5d0 commit a0fd9e1
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/example/springbootbookshop/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -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<Role> roles) {
String email,
String firstName,
String lastName,
String shippingAddress) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.springbootbookshop.dto;

public record UserRegisterResponseDto(Long id,
String email) {
String email,
String firstName,
String lastName,
String shippingAddress) {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Role> getUserRole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ databaseChangeLog:
name: shipping_address
type: VARCHAR(255)
constraints:
nullable: true
nullable: true
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ databaseChangeLog:
type: VARCHAR(255)
constraints:
unique: true
nullable: false
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
38 changes: 38 additions & 0 deletions src/main/resources/db/changelog/changes/04-create-users-roles.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
file: db\changelog\changes\03-create-roles-table.yaml
- include:
file: db/changelog/changes/04-create-users-roles.yaml

0 comments on commit a0fd9e1

Please sign in to comment.