Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Add missing validation (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamPanDonte authored Jan 11, 2023
1 parent 92cf59e commit 6cfac66
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 75 deletions.
18 changes: 2 additions & 16 deletions src/main/java/dev/vernite/vernite/status/CreateStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,62 +35,48 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
* Class containing information needed to create new status entity.
* Has required constraints annotated using Java Bean Validation.
*/
@ToString
@Data
@NoArgsConstructor
@EqualsAndHashCode
@AllArgsConstructor
public class CreateStatus {

/**
* Name for new status. Must contain at least one non-whitespace character.
*/
@Setter
@Getter
@Size(min = 1, max = 50, message = "status name must be shorter than 50 characters")
@NotBlank(message = "status name must contain at least one non-whitespace character")
private String name;

/**
* Color for new status. Must be a non negative number.
*/
@Setter
@Getter
@NotNull(message = "status color must be specified")
@PositiveOrZero(message = "status color must be a non negative number")
private Integer color;

/**
* Order for new status. Must be a non negative number.
*/
@Setter
@Getter
@NotNull(message = "status order must be specified")
@PositiveOrZero(message = "status order must be a non negative number")
private Integer ordinal;

/**
* Flag indicating if new status is begin status. Must be a boolean value.
*/
@Setter
@Getter
@NotNull(message = "begin status must be specified")
private Boolean begin;

/**
* Flag indicating if new status is final status. Must be a boolean value.
*/
@Setter
@Getter
@JsonProperty("final")
@NotNull(message = "final status must be specified")
private Boolean isFinal;
Expand Down
29 changes: 10 additions & 19 deletions src/main/java/dev/vernite/vernite/status/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
import jakarta.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonIgnore;

import dev.vernite.vernite.common.exception.ConflictStateException;
import dev.vernite.vernite.project.Project;
import dev.vernite.vernite.task.Task;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.OnDelete;
Expand All @@ -57,49 +58,35 @@
/**
* Entity representing a status of a task in project workflow.
*/
@Data
@Entity
@ToString
@NoArgsConstructor
@EqualsAndHashCode
public class Status {

@Id
@Setter
@Getter
@GeneratedValue(strategy = GenerationType.IDENTITY)
@PositiveOrZero(message = "status id must be non negative number")
private long id;

@Getter
@Column(nullable = false, length = 50)
@Size(min = 1, max = 50, message = "project name must be shorter than 50 characters")
@NotBlank(message = "project name must contain at least one non-whitespace character")
private String name;

@Setter
@Getter
@Column(nullable = false)
@PositiveOrZero(message = "status color must be non negative number")
private int color;

@Setter
@Getter
@Column(nullable = false)
@PositiveOrZero(message = "status color must be non negative number")
private int ordinal;

@Setter
@Getter
@Column(nullable = false)
private boolean isBegin;

@Setter
@Getter
@Column(nullable = false)
private boolean isFinal;

@Setter
@Getter
@JsonIgnore
@ToString.Exclude
@EqualsAndHashCode.Exclude
Expand All @@ -108,8 +95,6 @@ public class Status {
@NotNull(message = "status must belong to a project")
private Project project;

@Setter
@Getter
@JsonIgnore
@ToString.Exclude
@EqualsAndHashCode.Exclude
Expand Down Expand Up @@ -145,6 +130,9 @@ public Status(String name, int color, int ordinal, boolean isFinal, boolean isBe
*/
public Status(Project project, CreateStatus create) {
this(create.getName(), create.getColor(), create.getOrdinal(), create.getIsFinal(), create.getBegin(), project);
if (this.isBegin() && this.isFinal()) {
throw new ConflictStateException("status cannot be begin and final at the same time");
}
}

/**
Expand All @@ -168,6 +156,9 @@ public void update(UpdateStatus update) {
if (update.getIsFinal() != null) {
setFinal(update.getIsFinal());
}
if (this.isBegin() && this.isFinal()) {
throw new ConflictStateException("status cannot be begin and final at the same time");
}
}

/**
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/dev/vernite/vernite/status/UpdateStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,60 +34,45 @@

import dev.vernite.vernite.common.constraints.NullOrNotBlank;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
* Class containing information needed to update status entity.
* Has required constraints annotated using Java Bean Validation.
* It performs partial update using only present fields.
*/
@ToString
@Data
@NoArgsConstructor
@EqualsAndHashCode
@AllArgsConstructor
public class UpdateStatus {

/**
* New name for status. Must contain at least one non-whitespace character.
*/
@Setter
@Getter
@Size(min = 1, max = 50, message = "status name must be shorter than 50 characters")
@NullOrNotBlank(message = "status name must contain at least one non-whitespace character")
private String name;

/**
* New color for status. Must be a non negative number.
*/
@Setter
@Getter
@PositiveOrZero(message = "status color must be a non negative number")
private Integer color;

/**
* New order for status. Must be a non negative number.
*/
@Setter
@Getter
@PositiveOrZero(message = "status order must be a non negative number")
private Integer ordinal;

/**
* Flag indicating if status is begin status. Must be a boolean value.
*/
@Setter
@Getter
private Boolean begin;

/**
* Flag indicating if status is final status. Must be a boolean value.
*/
@Setter
@Getter
@JsonProperty("final")
private Boolean isFinal;

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/dev/vernite/vernite/task/TaskRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TaskRequest {

@Schema(maxLength = 100, minLength = 1, description = "The name of the task. Trailing and leading whitespace are removed.")
private Optional<String> name = Optional.empty();
@Schema(description = "The description of the task. Trailing and leading whitespace are removed.")
@Schema(maxLength = 100, description = "The description of the task. Trailing and leading whitespace are removed.")
private Optional<String> description = Optional.empty();
@Schema(description = "New status of the task.")
private Optional<Long> statusId = Optional.empty();
Expand Down Expand Up @@ -133,6 +133,9 @@ public void setDescription(String description) {
if (description == null) {
throw new FieldErrorException("description", NULL_VALUE);
}
if (description.length() > 1000) {
throw new FieldErrorException("description", "too long");
}
this.description = Optional.of(description.trim());
}

Expand Down
23 changes: 3 additions & 20 deletions src/main/java/dev/vernite/vernite/user/auth/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Setter;

Expand Down Expand Up @@ -301,7 +302,7 @@ public Future<User> login(@Parameter(hidden = true) User loggedUser, @RequestBod
@Operation(summary = "Modify user account", description = "This method edits the account.")
@ApiResponse(responseCode = "200", description = "User after changes.")
@PutMapping("/edit")
public User edit(@NotNull @Parameter(hidden = true) User loggedUser, @RequestBody EditAccountRequest req) {
public User edit(@NotNull @Parameter(hidden = true) User loggedUser, @RequestBody @Valid EditAccountRequest req) {
if (req.getAvatar() != null) {
loggedUser.setAvatar(req.getAvatar());
}
Expand Down Expand Up @@ -351,29 +352,11 @@ public ResponseEntity<Void> verify(@Parameter(hidden = true) User loggedUser, @P
@ApiResponse(responseCode = "403", description = "User is already logged or invalid captcha.", content = @Content())
@ApiResponse(responseCode = "422", description = "Username or email is already taken.", content = @Content())
@PostMapping("/register")
public Future<User> register(@Parameter(hidden = true) User loggedUser, @RequestBody RegisterRequest req,
public Future<User> register(@Parameter(hidden = true) User loggedUser, @RequestBody @Valid RegisterRequest req,
HttpServletRequest request, HttpServletResponse response) {
if (loggedUser != null) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "already logged");
}
if (req.getEmail() == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing email");
}
if (req.getName() == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing name");
}
if (req.getPassword() == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing password");
}
if (req.getSurname() == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing surname");
}
if (req.getUsername() == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing username");
}
if (req.getCaptcha() == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing captcha");
}
if (req.getUsername().indexOf('@') != -1) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid character in username");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,59 @@

package dev.vernite.vernite.user.auth;

import dev.vernite.vernite.common.constraints.NullOrNotBlank;
import jakarta.validation.constraints.PositiveOrZero;
import jakarta.validation.constraints.Size;
import lombok.Data;

/**
* Class containing information needed to edit user entity.
* Has required constraints annotated using Java Bean Validation.
*/
@Data
public class EditAccountRequest {

/**
* Avatar for user. Must be a valid URL.
*/
@NullOrNotBlank(message = "avatar must be specified")
private String avatar;

/**
* Name for user. Must be at least 2 characters long.
*/
@NullOrNotBlank(message = "name must be specified")
@Size(min = 2, max = 100, message = "name must be at least 2 characters long and shorter than 100 characters")
private String name;

/**
* Surname for user. Must be at least 2 characters long.
*/
@NullOrNotBlank(message = "surname must be specified")
@Size(min = 2, max = 100, message = "surname must be at least 2 characters long and shorter than 100 characters")
private String surname;

/**
* Language for user.
*/
@Size(max = 5, message = "language must be shorter than 5 characters")
private String language;

/**
* Date format for user.
*/
@Size(max = 10, message = "date format must be shorter than 10 characters")
private String dateFormat;

/**
* Time format for user.
*/
@Size(max = 10, message = "time format must be shorter than 10 characters")
private String timeFormat;

/**
* First day of week for user.
*/
@PositiveOrZero(message = "first day of week must be a positive number")
private Integer firstDayOfWeek;
}
Loading

0 comments on commit 6cfac66

Please sign in to comment.