Skip to content

Commit

Permalink
Merge pull request #86 from SAKPaaS/bugfix/occupancy-range
Browse files Browse the repository at this point in the history
Added Validators to occupancy reports
  • Loading branch information
r-franzke authored Apr 4, 2020
2 parents f3d41bd + d8da496 commit e5c7c77
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/de/sakpaas/backend/dto/Error400Dto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.sakpaas.backend.dto;

import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
public class Error400Dto {

private Date timestamp;
private final int status = 400;
private final String error = "Bad Request";
private String message;
private String path;

}
42 changes: 42 additions & 0 deletions src/main/java/de/sakpaas/backend/util/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.sakpaas.backend.util;

import de.sakpaas.backend.dto.Error400Dto;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

/**
* This ErrorHandler handles @Valid errors.
*/
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception,
HttpHeaders headers,
HttpStatus status, WebRequest request) {
Error400Dto error = new Error400Dto();
error.setTimestamp(new Date());
error.setPath(((ServletWebRequest) request).getRequest().getServletPath());

// Iterate over all errors and get the messages
String message = exception.getBindingResult()
.getFieldErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(", "));
error.setMessage(message);

return new ResponseEntity<>(error, headers, status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -70,7 +71,7 @@ public ResponseEntity<LocationDto> getById(@PathVariable("locationId") Long loca
}

@PostMapping(value = MAPPING_POST_OCCUPANCY)
public ResponseEntity<LocationDto> postNewOccupancy(@RequestBody OccupancyReportDto occupancyDto,
public ResponseEntity<LocationDto> postNewOccupancy(@Valid @RequestBody OccupancyReportDto occupancyDto,
@PathVariable("locationId") Long locationId) {
occupancyDto.setLocationId(locationId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;

@Data
@JsonPropertyOrder({"locationId", "occupancy", "clientType"})
public class OccupancyReportDto {

private Long locationId;

@NotNull(message = "'occupancy' can not be null.")
@DecimalMin(value = "0.0", message = "'occupancy' has to be between 0.0 and 1.0.")
@DecimalMax(value = "1.0", message = "'occupancy' has to be between 0.0 and 1.0.")
private Double occupancy;

private String clientType;

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -73,7 +74,7 @@ public ResponseEntity<LocationResultLocationDto> getById(@PathVariable("location
}

@PostMapping(value = MAPPING_POST_OCCUPANCY)
public ResponseEntity<LocationResultLocationDto> postNewOccupancy(@RequestBody OccupancyReportDto occupancyReportDto,
public ResponseEntity<LocationResultLocationDto> postNewOccupancy(@Valid @RequestBody OccupancyReportDto occupancyReportDto,
@PathVariable("locationId") Long locationId) {
occupancyReportDto.setLocationId(locationId);
Location location = locationService.getById(locationId).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;

@Data
@JsonPropertyOrder({"locationId", "occupancy", "clientType"})
public class OccupancyReportDto {

private Long locationId;

@NotNull(message = "'occupancy' can not be null.")
@DecimalMin(value = "0.0", message = "'occupancy' has to be between 0.0 and 1.0.")
@DecimalMax(value = "1.0", message = "'occupancy' has to be between 0.0 and 1.0.")
private Double occupancy;

private String clientType;

@JsonCreator
Expand Down

0 comments on commit e5c7c77

Please sign in to comment.