Skip to content

Commit

Permalink
Merge pull request #600 from puzzle/feature/576-toaster-styling
Browse files Browse the repository at this point in the history
Feature/576 toaster styling and error msg's
  • Loading branch information
peggimann authored Nov 24, 2023
2 parents 4c02506 + 21ac830 commit 7572966
Show file tree
Hide file tree
Showing 60 changed files with 1,507 additions and 832 deletions.
5 changes: 5 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ private Constants() {

public static final String KEY_RESULT_TYPE_METRIC = "metric";
public static final String KEY_RESULT_TYPE_ORDINAL = "ordinal";
public static final String OBJECTIVE = "Objective";
public static final String KEY_RESULT = "KeyResult";
public static final String CHECK_IN = "Check-in";
public static final String ACTION = "Action";

}
24 changes: 24 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/OkrErrorAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ch.puzzle.okr;

import ch.puzzle.okr.models.OkrResponseStatusException;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Component
public class OkrErrorAttributes extends DefaultErrorAttributes {

@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);

Throwable throwable = getError(webRequest);
if (throwable instanceof OkrResponseStatusException exception) {
errorAttributes.put("errors", exception.getErrors());
}
return errorAttributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ public TeamController(TeamAuthorizationService teamAuthorizationService, TeamMap
this.teamMapper = teamMapper;
}

@Operation(summary = "Get Teams", description = "Get all Teams from db as well as all active objectives from chosen quarter")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Returned all Teams with active objective in quarter", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = TeamDto.class)) }), })
@Operation(summary = "Get Teams", description = "Get all Teams from db")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Returned all Teams", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = TeamDto.class)) }), })
@GetMapping
public List<TeamDto> getAllTeams(@RequestParam(value = "quarterId", required = false) Long quarterId) {
return teamAuthorizationService.getAllTeams().stream().map(team -> teamMapper.toDto(team, quarterId)).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package ch.puzzle.okr.converter;

import ch.puzzle.okr.models.ErrorMsg;
import ch.puzzle.okr.models.OkrResponseStatusException;
import ch.puzzle.okr.models.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.Map;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
Expand Down Expand Up @@ -38,7 +40,7 @@ public User convert(Jwt token) {
.withEmail(claims.get(email).toString()).build();
} catch (Exception e) {
logger.warn("can not convert user from claims {}", claims);
throw new ResponseStatusException(BAD_REQUEST, "can not convert user from token");
throw new OkrResponseStatusException(BAD_REQUEST, ErrorMsg.CONVERT_TOKEN, List.of("User"));
}
}
}
41 changes: 41 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/dto/ErrorDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ch.puzzle.okr.dto;

import java.util.List;
import java.util.Objects;

public class ErrorDto {
private final String errorKey;
private final List<String> params;

public ErrorDto(String errorKey, List<Object> params) {
this.errorKey = errorKey;
this.params = params.stream().map(Object::toString).toList();
}

public ErrorDto(String errorKey, String param) {
this(errorKey, List.of(param));
}

public String getErrorKey() {
return errorKey;
}

public List<String> getParams() {
return params;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ErrorDto errorDto = (ErrorDto) o;
return Objects.equals(errorKey, errorDto.errorKey) && Objects.equals(params, errorDto.params);
}

@Override
public int hashCode() {
return Objects.hash(errorKey, params);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ch.puzzle.okr.mapper;

import ch.puzzle.okr.dto.overview.*;
import ch.puzzle.okr.models.ErrorMsg;
import ch.puzzle.okr.models.OkrResponseStatusException;
import ch.puzzle.okr.models.overview.Overview;
import ch.puzzle.okr.service.business.OrganisationBusinessService;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -89,9 +90,8 @@ private OverviewKeyResultDto createKeyResultDto(Overview overview) {
} else if (Objects.equals(overview.getKeyResultType(), KEY_RESULT_TYPE_ORDINAL)) {
return createKeyResultOrdinalDto(overview);
} else {
throw new ResponseStatusException(BAD_REQUEST,
String.format("The key result type %s can not be converted to a metric or ordinal DTO",
overview.getKeyResultType()));
throw new OkrResponseStatusException(BAD_REQUEST, ErrorMsg.KEYRESULT_CONVERSION,
overview.getKeyResultType());
}
}

Expand Down
10 changes: 5 additions & 5 deletions backend/src/main/java/ch/puzzle/okr/models/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public class Action implements WriteableInterface {
@Version
private int version;

@NotNull(message = "Action must not be null")
@Size(max = 4096, message = "Attribute Action has a max length of 4096 characters")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Size(max = 4096, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String action;

@NotNull(message = "Priority must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
private int priority;

@NotNull(message = "IsChecked must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
private boolean isChecked;

@NotNull(message = "KeyResult must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@ManyToOne
private KeyResult keyResult;

Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/java/ch/puzzle/okr/models/Completed.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class Completed {
@Version
private int version;

@NotNull(message = "Objective must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@OneToOne
private Objective objective;

@Size(max = 4096, message = "Attribute comment has a max length of 4096 characters when completing an objective")
@Size(max = 4096, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String comment;

public Completed() {
Expand Down
34 changes: 34 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/models/ErrorMsg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ch.puzzle.okr.models;

public class ErrorMsg {
private ErrorMsg() {
}

public static final String UNAUTHORIZED = "UNAUTHORIZED";
public static final String NOT_FOUND = "NOT_FOUND";
public static final String KEYRESULT_CONVERSION = "KEYRESULT_CONVERSION";
public static final String ALREADY_EXISTS_SAME_NAME = "ALREADY_EXISTS_SAME_NAME";
public static final String CONVERT_TOKEN = "CONVERT_TOKEN";
public static final String DATA_HAS_BEEN_UPDATED = "DATA_HAS_BEEN_UPDATED";
// Model
public static final String MODEL_NULL = "MODEL_NULL";
public static final String MODEL_WITH_ID_NOT_FOUND = "MODEL_WITH_ID_NOT_FOUND";

// Attributes
public static final String ATTRIBUTE_NULL = "ATTRIBUTE_NULL";
public static final String ATTRIBUTE_NOT_NULL = "ATTRIBUTE_NOT_NULL";
public static final String ATTRIBUTE_CHANGED = "ATTRIBUTE_CHANGED";
public static final String ATTRIBUTE_NOT_BLANK = "ATTRIBUTE_NOT_BLANK";
public static final String ATTRIBUTE_NOT_VALID = "ATTRIBUTE_NOT_VALID";
public static final String ATTRIBUTE_SIZE_BETWEEN = "ATTRIBUTE_SIZE_BETWEEN_{min}_{max}";
public static final String ATTRIBUTE_SET_FORBIDDEN = "ATTRIBUTE_SET_FORBIDDEN";
public static final String ATTRIBUTE_NOT_SET = "ATTRIBUTE_NOT_SET";
public static final String ATTRIBUTE_CANNOT_CHANGE = "ATTRIBUTE_CANNOT_CHANGE";
public static final String ATTRIBUTE_MIN_VALUE = "ATTRIBUTE_MIN_VALUE_{value}";
public static final String ATTRIBUTE_MAX_VALUE = "ATTRIBUTE_MAX_VALUE_{value}";

public static final String NOT_AUTHORIZED_TO_READ = "NOT_AUTHORIZED_TO_READ";
public static final String NOT_AUTHORIZED_TO_WRITE = "NOT_AUTHORIZED_TO_WRITE";
public static final String NOT_AUTHORIZED_TO_DELETE = "NOT_AUTHORIZED_TO_DELETE";
public static final String TOKEN_NULL = "TOKEN_NULL";
}
18 changes: 9 additions & 9 deletions backend/src/main/java/ch/puzzle/okr/models/Objective.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ public class Objective implements WriteableInterface {
@Version
private int version;

@NotBlank(message = "Missing attribute title when saving objective")
@NotNull(message = "Attribute title can not be null when saving objective")
@Size(min = 2, max = 250, message = "Attribute title must have a length between 2 and 250 characters when saving objective")
@NotBlank(message = ErrorMsg.ATTRIBUTE_NOT_BLANK)
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Size(min = 2, max = 250, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String title;

@NotNull(message = "State must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Enumerated(EnumType.STRING)
private State state;

@Size(max = 4096, message = "Attribute description has a max length of 4096 characters when saving objective")
@Size(max = 4096, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String description;

@NotNull(message = "Team must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@ManyToOne
private Team team;

@NotNull(message = "Quarter must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@ManyToOne
private Quarter quarter;

@NotNull(message = "CreatedBy must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@ManyToOne
private User createdBy;

@NotNull(message = "CreatedOn must not be null")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
private LocalDateTime createdOn;

private LocalDateTime modifiedOn;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ch.puzzle.okr.models;

import ch.puzzle.okr.dto.ErrorDto;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

public class OkrResponseStatusException extends ResponseStatusException {

private final List<ErrorDto> errors;

public OkrResponseStatusException(HttpStatus status, String errorKey) {
this(status, errorKey, List.of());
}

public OkrResponseStatusException(HttpStatus status, String errorKey, List<Object> objectParams) {
this(status, List.of(new ErrorDto(errorKey, objectParams)));
}

public OkrResponseStatusException(HttpStatus status, String errorKey, String param) {
this(status, errorKey, List.of(param));
}

public OkrResponseStatusException(HttpStatus status, ErrorDto error) {
this(status, List.of(error));
}

public OkrResponseStatusException(HttpStatus status, List<ErrorDto> errors) {
super(status, errors.get(0).getErrorKey());
this.errors = errors;
}

public List<ErrorDto> getErrors() {
return errors;
}
}
6 changes: 3 additions & 3 deletions backend/src/main/java/ch/puzzle/okr/models/Quarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class Quarter {
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_quarter")
private Long id;

@NotNull(message = "Attribute label can not be null when saving quarter")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Column(unique = true)
private String label;

@NotNull(message = "Attribute startDate can not be null when saving quarter")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
private LocalDate startDate;

@NotNull(message = "Attribute endDate can not be null when saving quarter")
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
private LocalDate endDate;

public Quarter() {
Expand Down
10 changes: 5 additions & 5 deletions backend/src/main/java/ch/puzzle/okr/models/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class Team implements WriteableInterface {
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_team")
private Long id;

@NotBlank(message = ErrorMsg.ATTRIBUTE_NOT_BLANK)
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Size(min = 2, max = 250, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String name;

@Version
private int version;

@NotBlank(message = "Missing attribute name when saving team")
@NotNull(message = "Attribute name can not be null when saving team")
@Size(min = 2, max = 250, message = "Attribute name must have size between 2 and 250 characters when saving team")
private String name;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "team_organisation", joinColumns = @JoinColumn(name = "team_id"), inverseJoinColumns = @JoinColumn(name = "organisation_id"))
private List<Organisation> authorizationOrganisation;
Expand Down
26 changes: 13 additions & 13 deletions backend/src/main/java/ch/puzzle/okr/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ public class User implements WriteableInterface {
private int version;

@Column(unique = true)
@NotBlank(message = "Missing attribute username when saving user")
@NotNull(message = "Attribute username can not be null when saving user")
@Size(min = 2, max = 20, message = "Attribute username must have size between 2 and 20 characters when saving user")
@NotBlank(message = ErrorMsg.ATTRIBUTE_NOT_BLANK)
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Size(min = 2, max = 20, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String username;

@NotBlank(message = "Missing attribute firstname when saving user")
@NotNull(message = "Attribute firstname can not be null when saving user")
@Size(min = 2, max = 50, message = "Attribute firstname must have size between 2 and 50 characters when saving user")
@NotBlank(message = ErrorMsg.ATTRIBUTE_NOT_BLANK)
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Size(min = 2, max = 50, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String firstname;

@NotBlank(message = "Missing attribute lastname when saving user")
@NotNull(message = "Attribute lastname can not be null when saving user")
@Size(min = 2, max = 50, message = "Attribute lastname must have size between 2 and 50 characters when saving user")
@NotBlank(message = ErrorMsg.ATTRIBUTE_NOT_BLANK)
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Size(min = 2, max = 50, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String lastname;

@Email(message = "Attribute email should be valid when saving user")
@NotNull(message = "Attribute email can not be null when saving user")
@NotBlank(message = "Missing attribute email when saving user")
@Size(min = 2, max = 250, message = "Attribute email must have size between 2 and 250 characters when saving user")
@Email(message = ErrorMsg.ATTRIBUTE_NOT_VALID)
@NotBlank(message = ErrorMsg.ATTRIBUTE_NOT_BLANK)
@NotNull(message = ErrorMsg.ATTRIBUTE_NOT_NULL)
@Size(min = 2, max = 250, message = ErrorMsg.ATTRIBUTE_SIZE_BETWEEN)
private String email;

private transient boolean writeable;
Expand Down
Loading

0 comments on commit 7572966

Please sign in to comment.