Skip to content

Commit

Permalink
Merge pull request #174 from gelecekbilimde/GBS-55/username-create
Browse files Browse the repository at this point in the history
GBS-55 | Username Created
  • Loading branch information
MenekseYuncu authored Oct 28, 2024
2 parents 45b40b4 + 4c51f2d commit c9e49b7
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 9 deletions.
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,10 @@ public class RegisterRequest {
@Size(min = 2, max = 25)
private String lastname;

@NotBlank
@Username
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,42 @@
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]{3,20}$";

@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 (value.length() <= 3 || value.length() >= 20) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("must be between 3 and 20 characters long")
.addConstraintViolation();
return false;
}

if (!lowerCasedValue.matches(USERNAME_REGEX)) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("must be alphanumeric")
.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;

@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,
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);

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);

0 comments on commit c9e49b7

Please sign in to comment.