Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GBS-55 | Username Created #174

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.gelecekbilimde.scienceplatform.common.util.validation.Username;
import org.gelecekbilimde.scienceplatform.user.model.enums.UserDegree;
import org.gelecekbilimde.scienceplatform.user.model.enums.UserGender;

Expand All @@ -28,6 +29,11 @@ public class RegisterRequest {
@Size(min = 2, max = 25)
private String lastname;

@NotBlank
@Username
@Size(min = 3, max = 20)
agitrubard marked this conversation as resolved.
Show resolved Hide resolved
private String username;

@NotBlank
@Email
@Size(min = 8, max = 255)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.gelecekbilimde.scienceplatform.auth.model.request.VerifyRequest;
import org.gelecekbilimde.scienceplatform.auth.port.RoleReadPort;
import org.gelecekbilimde.scienceplatform.auth.service.RegistrationService;
import org.gelecekbilimde.scienceplatform.user.exception.UsernameAlreadyTakenException;
import org.gelecekbilimde.scienceplatform.user.model.User;
import org.gelecekbilimde.scienceplatform.user.model.UserVerification;
import org.gelecekbilimde.scienceplatform.user.model.enums.UserStatus;
Expand Down Expand Up @@ -48,9 +49,14 @@ public void register(RegisterRequest request) {
Role role = roleReadPort.findByName(RoleName.USER)
.orElseThrow(() -> new RoleNotFoundByNameException(RoleName.USER.name()));

if (userReadPort.existsByUsername(request.getUsername())) {
throw new UsernameAlreadyTakenException(request.getUsername());
}

User user = User.builder()
.firstName(request.getFirstname())
.lastName(request.getLastname())
.username(request.getUsername().toLowerCase())
.email(request.getEmail())
.birthDate(request.getBirthDate())
.biography(request.getBiography())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gelecekbilimde.scienceplatform.common.util.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UsernameValidator.class)
public @interface Username {

String message() default "must be valid";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.gelecekbilimde.scienceplatform.common.util.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.util.StringUtils;

class UsernameValidator implements ConstraintValidator<Username, String> {

private static final String USERNAME_REGEX = "^[a-zA-Z0-9]$";

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (!StringUtils.hasText(value)) {
return true;
}

String lowerCasedValue = value.toLowerCase();

if (!value.trim().equals(value)) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("name must not start or end with whitespace")
.addConstraintViolation();
return false;
}

if (!lowerCasedValue.matches(USERNAME_REGEX)) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("Username must be alphanumeric")
agitrubard marked this conversation as resolved.
Show resolved Hide resolved
.addConstraintViolation();
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gelecekbilimde.scienceplatform.user.exception;

import org.gelecekbilimde.scienceplatform.common.exception.AbstractConflictException;

import java.io.Serial;

public final class UsernameAlreadyTakenException extends AbstractConflictException {

@Serial
private static final long serialVersionUID = -3365493595680402581L;

public UsernameAlreadyTakenException(String username) {
super(String.format("'%s' username already taken", username));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class User extends BaseDomainModel {
private String password;
private String firstName;
private String lastName;
private String username;
private String avatarPath;
private String biography;
private LocalDate birthDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class UserEntity extends BaseEntity {
@Column(name = "last_name")
private String lastName;

@Column(name = "username")
private String username;
MenekseYuncu marked this conversation as resolved.
Show resolved Hide resolved

@Column(name = "avatar_path")
private String avatar;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface UserReadPort {

boolean existsByEmail(String email);

boolean existsByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public boolean existsByEmail(final String email) {
}


@Override
public boolean existsByUsername(String username) {
return userRepository.existsByUsername(username);
}


@Override
public User save(User user) {
final UserEntity userEntity = userToEntityMapper.map(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface UserRepository extends JpaRepository<UserEntity, String> {
Optional<UserEntity> findByEmail(String email);

boolean existsByEmail(String email);

boolean existsByUsername(String username);
}
3 changes: 2 additions & 1 deletion src/main/resources/db/migration/V1__ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ create table if not exists gb_user
(
id varchar(36) not null primary key,
role_id varchar(36) not null,
email varchar(255) not null,
email varchar(255) not null unique,
password varchar(255) not null,
first_name varchar(25) not null,
last_name varchar(25) not null,
username varchar(20) not null unique,
agitrubard marked this conversation as resolved.
Show resolved Hide resolved
avatar_path varchar(255),
biography text,
birth_date date,
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/db/migration/V2__dml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -274,30 +274,30 @@ values (0, null, 'Bilim', 'Bilim kategorisidir.', 'bilim', 'flask-conical',
'gelecekbilimde', current_timestamp);


insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('22afc9b4-807f-4eb2-b286-788631d1ed55', current_date, '[email protected]',
'FEMALE', 'Test', 'Yönetici',
'FEMALE', 'Test', 'Yönetici', ' administrator12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'c147b5c2-87f7-4bb7-a165-368f639d8c3c', 'gelecekbilimde', current_timestamp);
MenekseYuncu marked this conversation as resolved.
Show resolved Hide resolved

insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('99af408c-bec9-4cf2-a5ea-218b12b88a50', current_date, '[email protected]',
'FEMALE', 'Test', 'Moderatör',
'FEMALE', 'Test', 'Moderatör', 'moderator12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'1ed82a25-d348-4576-b4e6-1f2a7c430ca7', 'gelecekbilimde', current_timestamp);

insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('fee95298-952d-4d1c-81dd-ae5a96b964e5', current_date, '[email protected]',
'MALE', 'Test', 'Yazar',
'MALE', 'Test', 'Yazar', 'author12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'4d98a76c-9841-4aea-b296-2f27aa610b6c', 'gelecekbilimde', current_timestamp);

insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('233d4054-e7b9-43ba-8b26-ca9254df78cd', current_date, '[email protected]',
'MALE', 'Test', 'Kullanıcı',
'MALE', 'Test', 'Kullanıcı', 'users12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'e3a1a32d-fcd7-46f0-bb2b-201df6b2b808', 'gelecekbilimde', current_timestamp);
MenekseYuncu marked this conversation as resolved.
Show resolved Hide resolved
Loading