Skip to content

Commit

Permalink
Merge pull request #80 from CMIPT/feature-kaiser
Browse files Browse the repository at this point in the history
Finish i18n.
  • Loading branch information
luckygalaxy666 authored Oct 22, 2024
2 parents c09a8e0 + 7666758 commit 0ee82e0
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 238 deletions.
13 changes: 4 additions & 9 deletions src/main/java/edu/cmipt/gcs/controller/RepositoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ public ResponseEntity<RepositoryVO> updateRepository(
&& !repository
.repositoryName()
.equals(repositoryService.getById(id).getRepositoryName())) {
throw new GenericException(
ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED,
"update repository name is not implemented");
throw new GenericException(ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED);
}
if (!repositoryService.updateById(new RepositoryPO(repository))) {
throw new GenericException(ErrorCodeEnum.REPOSITORY_UPDATE_FAILED, repository);
Expand Down Expand Up @@ -216,17 +214,14 @@ public void checkRepositoryNameValidity(
min = ValidationConstant.MIN_REPOSITORY_NAME_LENGTH,
max = ValidationConstant.MAX_REPOSITORY_NAME_LENGTH,
message =
"REPOSITORYDTO_REPOSITORYNAME_SIZE"
+ " {RepositoryDTO.repositoryName.Size}")
"{Size.repositoryController#checkRepositoryNameValidity.repositoryName}")
@NotBlank(
message =
"REPOSITORYDTO_REPOSITORYNAME_NOTBLANK"
+ " {RepositoryDTO.repositoryName.NotBlank}")
"{NotBlank.repositoryController#checkRepositoryNameValidity.repositoryName}")
@Pattern(
regexp = ValidationConstant.REPOSITORY_NAME_PATTERN,
message =
"REPOSITORYNAME_PATTERN_MISMATCH"
+ " {REPOSITORYNAME_PATTERN_MISMATCH}")
"{Pattern.repositoryController#checkRepositoryNameValidity.repositoryName}")
String repositoryName,
@RequestParam("userId") Long userId) {
QueryWrapper<RepositoryPO> queryWrapper = new QueryWrapper<>();
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/edu/cmipt/gcs/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ public List<RepositoryVO> pageUserRepository(
})
public void checkEmailValidity(
@RequestParam("email")
@Email(message = "USERDTO_EMAIL_EMAIL {UserDTO.email.Email}")
@NotBlank(message = "USERDTO_EMAIL_NOTBLANK {UserDTO.email.NotBlank}")
@Email(message = "{Email.userController#checkEmailValidity.email}")
@NotBlank(message = "{NotBlank.userController#checkEmailValidity.email}")
String email) {
QueryWrapper<UserPO> wrapper = new QueryWrapper<UserPO>();
wrapper.eq("email", email);
Expand Down Expand Up @@ -295,11 +295,11 @@ public void checkUsernameValidity(
@Size(
min = ValidationConstant.MIN_USERNAME_LENGTH,
max = ValidationConstant.MAX_USERNAME_LENGTH,
message = "USERDTO_USERNAME_SIZE {UserDTO.username.Size}")
@NotBlank(message = "USERDTO_USERNAME_NOTBLANK {UserDTO.username.NotBlank}")
message = "{Size.userController#checkUsernameValidity.username}")
@NotBlank(message = "{NotBlank.userController#checkUsernameValidity.username}")
@Pattern(
regexp = ValidationConstant.USERNAME_PATTERN,
message = "USERNAME_PATTERN_MISMATCH {USERNAME_PATTERN_MISMATCH}")
message = "{Pattern.userController#checkUsernameValidity.username}")
String username) {
QueryWrapper<UserPO> wrapper = new QueryWrapper<UserPO>();
wrapper.eq("username", username);
Expand Down Expand Up @@ -332,10 +332,10 @@ public void checkPasswordValidity(
@Size(
min = ValidationConstant.MIN_PASSWORD_LENGTH,
max = ValidationConstant.MAX_PASSWORD_LENGTH,
message = "USERDTO_PASSWORD_SIZE {UserDTO.password.Size}")
@NotBlank(message = "USERDTO_PASSWORD_NOTBLANK {UserDTO.password.NotBlank}")
message = "{Size.userController#checkPasswordValidity.password}")
@NotBlank(message = "{NotBlank.userController#checkPasswordValidity.password}")
@Pattern(
regexp = ValidationConstant.PASSWORD_PATTERN,
message = "PASSWORD_PATTERN_MISMATCH {PASSWORD_PATTERN_MISMATCH}")
message = "{Pattern.userController#checkPasswordValidity.password}")
String password) {}
}
35 changes: 1 addition & 34 deletions src/main/java/edu/cmipt/gcs/enumeration/ErrorCodeEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@ public enum ErrorCodeEnum {
// This should be ignored, this is to make the ordinal of the enum start from 1
ZERO_PLACEHOLDER,

USERDTO_ID_NULL("UserDTO.id.Null"),
USERDTO_ID_NOTNULL("UserDTO.id.NotNull"),
USERDTO_USERNAME_SIZE("UserDTO.username.Size"),
USERDTO_USERNAME_NOTBLANK("UserDTO.username.NotBlank"),
USERDTO_EMAIL_NOTBLANK("UserDTO.email.NotBlank"),
USERDTO_EMAIL_EMAIL("UserDTO.email.Email"),
USERDTO_USERPASSWORD_SIZE("UserDTO.userPassword.Size"),
USERDTO_USERPASSWORD_NOTBLANK("UserDTO.userPassword.NotBlank"),

USERSIGNINDTO_USERNAME_NOTBLANK("UserSignInDTO.username.NotBlank"),
USERSIGNINDTO_USERPASSWORD_NOTBLANK("UserSignInDTO.userPassword.NotBlank"),

USERNAME_PATTERN_MISMATCH("USERNAME_PATTERN_MISMATCH"),
PASSWORD_PATTERN_MISMATCH("PASSWORD_PATTERN_MISMATCH"),
VALIDATION_ERROR("VALIDATION_ERROR"),

USERNAME_ALREADY_EXISTS("USERNAME_ALREADY_EXISTS"),
EMAIL_ALREADY_EXISTS("EMAIL_ALREADY_EXISTS"),
Expand All @@ -35,19 +22,6 @@ public enum ErrorCodeEnum {
USER_UPDATE_FAILED("USER_UPDATE_FAILED"),
USER_DELETE_FAILED("USER_DELETE_FAILED"),

REPOSITORYDTO_ID_NULL("RepositoryDTO.id.Null"),
REPOSITORYDTO_ID_NOTNULL("RepositoryDTO.id.NotNull"),
REPOSITORYDTO_REPOSITORYNAME_SIZE("RepositoryDTO.repositoryName.Size"),
REPOSITORYDTO_REPOSITORYNAME_NOTBLANK("RepositoryDTO.repositoryName.NotBlank"),
REPOSITORYDTO_REPOSITORYDESCRIPTION_SIZE("RepositoryDTO.repositoryDescription.Size"),
REPOSITORYDTO_STAR_NULL("RepositoryDTO.star.Null"),
REPOSITORYDTO_STAR_MIN("RepositoryDTO.star.Min"),
REPOSITORYDTO_FORK_NULL("RepositoryDTO.fork.Null"),
REPOSITORYDTO_FORK_MIN("RepositoryDTO.fork.Min"),
REPOSITORYDTO_WATCHER_NULL("RepositoryDTO.watcher.Null"),
REPOSITORYDTO_WATCHER_MIN("RepositoryDTO.watcher.Min"),

REPOSITORYNAME_PATTERN_MISMATCH("REPOSITORYNAME_PATTERN_MISMATCH"),
REPOSITORY_NOT_FOUND("REPOSITORY_NOT_FOUND"),
REPOSITORY_ALREADY_EXISTS("REPOSITORY_ALREADY_EXISTS"),
REPOSITORY_CREATE_FAILED("REPOSITORY_CREATE_FAILED"),
Expand All @@ -59,13 +33,6 @@ public enum ErrorCodeEnum {
COLLABORATION_ALREADY_EXISTS("COLLABORATION_ALREADY_EXISTS"),
COLLABORATION_NOT_FOUND("COLLABORATION_NOT_FOUND"),

SSHKEYDTO_ID_NULL("SshKeyDTO.id.Null"),
SSHKEYDTO_ID_NOTNULL("SshKeyDTO.id.NotNull"),
SSHKEYDTO_NAME_NOTBLANK("SshKeyDTO.name.NotBlank"),
SSHKEYDTO_NAME_SIZE("SshKeyDTO.name.Size"),
SSHKEYDTO_PUBLICKEY_NOTBLANK("SshKeyDTO.publicKey.NotBlank"),
SSHKEYDTO_PUBLICKEY_SIZE("SshKeyDTO.publicKey.Size"),

SSH_KEY_UPLOAD_FAILED("SSH_KEY_UPLOAD_FAILED"),
SSH_KEY_UPDATE_FAILED("SSH_KEY_UPDATE_FAILED"),
SSH_KEY_DELETE_FAILED("SSH_KEY_DELETE_FAILED"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.cmipt.gcs.enumeration.ErrorCodeEnum;
import edu.cmipt.gcs.pojo.error.ErrorVO;
import edu.cmipt.gcs.util.MessageSourceUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
Expand Down Expand Up @@ -37,7 +38,10 @@ public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorVO> handleMethodArgumentNotValidException(
MethodArgumentNotValidException e, HttpServletRequest request) {
return handleValidationException(e.getFieldError().getDefaultMessage(), request);
var fieldError = e.getBindingResult().getFieldError();
return handleValidationException(
MessageSourceUtil.getMessage(fieldError.getCodes()[0], fieldError.getArguments()),
request);
}

/**
Expand Down Expand Up @@ -108,13 +112,8 @@ public ResponseEntity<ErrorVO> handleException(Exception e, HttpServletRequest r
}

private ResponseEntity<ErrorVO> handleValidationException(
String codeAndMessage, HttpServletRequest request) {
int firstSpaceIndex = codeAndMessage.indexOf(" ");
// There must be a space and not at the end of the message
assert firstSpaceIndex != -1;
assert firstSpaceIndex != codeAndMessage.length() - 1;
var exception = new GenericException(codeAndMessage.substring(firstSpaceIndex + 1));
exception.setCode(ErrorCodeEnum.valueOf(codeAndMessage.substring(0, firstSpaceIndex)));
return handleGenericException(exception, request);
String message, HttpServletRequest request) {
return handleGenericException(
new GenericException(ErrorCodeEnum.VALIDATION_ERROR, message), request);
}
}
28 changes: 6 additions & 22 deletions src/main/java/edu/cmipt/gcs/pojo/repository/RepositoryDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
@Schema(description = "Repository Data Transfer Object")
public record RepositoryDTO(
@Schema(description = "Repository ID")
@Null(
groups = CreateGroup.class,
message = "REPOSITORYDTO_ID_NULL {RepositoryDTO.id.Null}")
@NotNull(
groups = UpdateGroup.class,
message = "REPOSITORYDTO_ID_NOTNULL {RepositoryDTO.id.NotNull}")
@Null(groups = CreateGroup.class)
@NotNull(groups = UpdateGroup.class)
String id,
@Schema(
description = "Repository Name",
Expand All @@ -29,29 +25,17 @@ public record RepositoryDTO(
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_REPOSITORY_NAME_LENGTH,
max = ValidationConstant.MAX_REPOSITORY_NAME_LENGTH,
message =
"REPOSITORYDTO_REPOSITORYNAME_SIZE"
+ " {RepositoryDTO.repositoryName.Size}")
@NotBlank(
groups = CreateGroup.class,
message =
"REPOSITORYDTO_REPOSITORYNAME_NOTBLANK"
+ " {RepositoryDTO.repositoryName.NotBlank}")
max = ValidationConstant.MAX_REPOSITORY_NAME_LENGTH)
@NotBlank(groups = CreateGroup.class)
@Pattern(
regexp = ValidationConstant.REPOSITORY_NAME_PATTERN,
groups = {CreateGroup.class, UpdateGroup.class},
message =
"REPOSITORYNAME_PATTERN_MISMATCH {REPOSITORYNAME_PATTERN_MISMATCH}")
groups = {CreateGroup.class, UpdateGroup.class})
String repositoryName,
@Schema(description = "Repository Description")
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_REPOSITORY_DESCRIPTION_LENGTH,
max = ValidationConstant.MAX_REPOSITORY_DESCRIPTION_LENGTH,
message =
"REPOSITORYDTO_REPOSITORYDESCRIPTION_SIZE"
+ " {RepositoryDTO.repositoryDescription.Size}")
max = ValidationConstant.MAX_REPOSITORY_DESCRIPTION_LENGTH)
String repositoryDescription,
@Schema(description = "Whether or Not Private Repo", example = "false")
Boolean isPrivate) {}
20 changes: 6 additions & 14 deletions src/main/java/edu/cmipt/gcs/pojo/ssh/SshKeyDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,20 @@
@Schema(description = "SSH Key Data Transfer Object")
public record SshKeyDTO(
@Schema(description = "SSH Key ID")
@Null(groups = CreateGroup.class, message = "SSHKEYDTO_ID_NULL {SshKeyDTO.id.Null}")
@NotNull(
groups = UpdateGroup.class,
message = "SSHKEYDTO_ID_NOTNULL {SshKeyDTO.id.NotNull}")
@Null(groups = CreateGroup.class)
@NotNull(groups = UpdateGroup.class)
String id,
@Schema(description = "Name", example = "My SSH Key")
@NotBlank(
groups = {CreateGroup.class, UpdateGroup.class},
message = "SSHKEYDTO_NAME_NOTBLANK {SshKeyDTO.name.NotBlank}")
@NotBlank(groups = {CreateGroup.class, UpdateGroup.class})
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_NAME_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_NAME_LENGTH,
message = "SSHKEYDTO_NAME_SIZE {SshKeyDTO.name.Size}")
max = ValidationConstant.MAX_SSH_KEY_NAME_LENGTH)
String name,
@Schema(description = "Public Key")
@NotBlank(
groups = CreateGroup.class,
message = "SSHKEYDTO_PUBLICKEY_NOTBLANK {SshKeyDTO.publicKey.NotBlank}")
@NotBlank(groups = CreateGroup.class)
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_PUBLICKEY_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_PUBLICKEY_LENGTH,
message = "SSHKEYDTO_PUBLICKEY_SIZE {SshKeyDTO.publicKey.Size}")
max = ValidationConstant.MAX_SSH_KEY_PUBLICKEY_LENGTH)
String publicKey) {}
34 changes: 10 additions & 24 deletions src/main/java/edu/cmipt/gcs/pojo/user/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
@Schema(description = "User Data Transfer Object")
public record UserDTO(
@Schema(description = "User ID")
@Null(groups = CreateGroup.class, message = "USERDTO_ID_NULL {UserDTO.id.Null}")
@NotNull(
groups = UpdateGroup.class,
message = "USERDTO_ID_NOTNULL {UserDTO.id.NotNull}")
@Null(groups = CreateGroup.class)
@NotNull(groups = UpdateGroup.class)
// The Long can not be expressed correctly in json, so use String instead
String id,
@Schema(
Expand All @@ -34,26 +32,18 @@ public record UserDTO(
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_USERNAME_LENGTH,
max = ValidationConstant.MAX_USERNAME_LENGTH,
message = "USERDTO_USERNAME_SIZE {UserDTO.username.Size}")
@NotBlank(
groups = {CreateGroup.class},
message = "USERDTO_USERNAME_NOTBLANK {UserDTO.username.NotBlank}")
max = ValidationConstant.MAX_USERNAME_LENGTH)
@NotBlank(groups = {CreateGroup.class})
@Pattern(
regexp = ValidationConstant.USERNAME_PATTERN,
groups = {CreateGroup.class, UpdateGroup.class},
message = "USERNAME_PATTERN_MISMATCH {USERNAME_PATTERN_MISMATCH}")
groups = {CreateGroup.class, UpdateGroup.class})
String username,
@Schema(
description = "Email",
requiredMode = Schema.RequiredMode.REQUIRED,
example = "[email protected]")
@Email(
groups = {CreateGroup.class, UpdateGroup.class},
message = "USERDTO_EMAIL_EMAIL {UserDTO.email.Email}")
@NotBlank(
groups = {CreateGroup.class},
message = "USERDTO_EMAIL_NOTBLANK {UserDTO.email.NotBlank}")
@Email(groups = {CreateGroup.class, UpdateGroup.class})
@NotBlank(groups = {CreateGroup.class})
String email,
@Schema(
description = "User Password (Unencrypted)",
Expand All @@ -62,13 +52,9 @@ public record UserDTO(
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_PASSWORD_LENGTH,
max = ValidationConstant.MAX_PASSWORD_LENGTH,
message = "USERDTO_USERPASSWORD_SIZE {UserDTO.userPassword.Size}")
@NotBlank(
groups = {CreateGroup.class},
message = "USERDTO_USERPASSWORD_NOTBLANK {UserDTO.userPassword.NotBlank}")
max = ValidationConstant.MAX_PASSWORD_LENGTH)
@NotBlank(groups = {CreateGroup.class})
@Pattern(
regexp = ValidationConstant.PASSWORD_PATTERN,
groups = {CreateGroup.class, UpdateGroup.class},
message = "PASSWORD_PATTERN_MISMATCH {PASSWORD_PATTERN_MISMATCH}")
groups = {CreateGroup.class, UpdateGroup.class})
String userPassword) {}
9 changes: 2 additions & 7 deletions src/main/java/edu/cmipt/gcs/pojo/user/UserSignInDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ public record UserSignInDTO(
description = "Username",
requiredMode = Schema.RequiredMode.REQUIRED,
example = "admin")
@NotBlank(
message =
"USERSIGNINDTO_USERNAME_NOTBLANK {UserSignInDTO.username.NotBlank}")
@NotBlank
String username,
@Schema(
description = "User Password (Unencrypted)",
requiredMode = Schema.RequiredMode.REQUIRED,
example = "123456")
@NotBlank(
message =
"USERSIGNINDTO_USERPASSWORD_NOTBLANK"
+ " {UserSignInDTO.userPassword.NotBlank}")
@NotBlank
String userPassword) {}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public boolean save(RepositoryPO repositoryPO) {
repositoryPO.getUserId(),
repositoryPO.getIsPrivate())) {
logger.error("Failed to create repository in gitolite");
throw new GenericException(
ErrorCodeEnum.REPOSITORY_CREATE_FAILED,
"Failed to create repository in gitolite");
throw new GenericException(ErrorCodeEnum.REPOSITORY_CREATE_FAILED, repositoryPO);
}
return true;
}
Expand All @@ -60,9 +58,7 @@ public boolean removeById(Serializable id) {
repositoryPO.getUserId(),
repositoryPO.getIsPrivate())) {
logger.error("Failed to remove repository from gitolite");
throw new GenericException(
ErrorCodeEnum.REPOSITORY_DELETE_FAILED,
"Failed to remove repository from gitolite");
throw new GenericException(ErrorCodeEnum.REPOSITORY_DELETE_FAILED, repositoryPO);
}
return true;
}
Expand Down
Loading

0 comments on commit 0ee82e0

Please sign in to comment.