Skip to content

Commit

Permalink
Merge branch 'main' into jwt-impl
Browse files Browse the repository at this point in the history
  • Loading branch information
nklimovych authored May 14, 2024
2 parents b574464 + afcb489 commit 242d1ad
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/java/mate/academy/bookstore/model/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class Role implements GrantedAuthority {

@Column(nullable = false, unique = true)
@Enumerated(EnumType.STRING)
private RoleName role;
private RoleName name;

@Override
public String getAuthority() {
return "ROLE_" + role.name();
return "ROLE_" + name.name();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package mate.academy.bookstore.repository.role;

import java.util.Set;
import mate.academy.bookstore.model.Role;
import mate.academy.bookstore.model.RoleName;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface RoleRepository extends JpaRepository<Role, Long> {

@Query("SELECT r FROM Role r WHERE r.role IN :roles")
Set<Role> findAllByRoles(Set<RoleName> roles);
Role findByName(RoleName name);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mate.academy.bookstore.service.impl;

import java.util.Set;
import java.util.Collections;
import lombok.RequiredArgsConstructor;
import mate.academy.bookstore.dto.user.UserRegistrationRequestDto;
import mate.academy.bookstore.dto.user.UserRegistrationResponseDto;
Expand Down Expand Up @@ -33,7 +33,7 @@ public UserRegistrationResponseDto save(UserRegistrationRequestDto requestDto)
User user = userMapper.toModel(requestDto);
user.setEmail(email);
user.setPassword(passwordEncoder.encode(user.getPassword()));
user.setRoles(roleRepository.findAllByRoles(Set.of(RoleName.USER)));
user.setRoles(Collections.singleton(roleRepository.findByName(RoleName.USER)));
User savedUser = userRepository.save(user);
return userMapper.toDto(savedUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ databaseChangeLog:
constraints:
primaryKey: true
- column:
name: role
name: name
type: enum('USER', 'ADMIN')
constraints:
nullable: false
unique: true
unique: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ databaseChangeLog:
tableName: roles
columns:
- column:
name: role
name: name
value: 'ADMIN'
- insert:
tableName: roles
columns:
- column:
name: role
name: name
value: 'USER'
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ databaseChangeLog:
valueComputed: (SELECT id FROM users WHERE email = 'admin@gmail.com')
- column:
name: role_id
valueComputed: (SELECT id FROM roles WHERE role = 'ADMIN')
valueComputed: (SELECT id FROM roles WHERE name = 'ADMIN')

0 comments on commit 242d1ad

Please sign in to comment.