Skip to content

Commit

Permalink
Data validation using @Valid annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizerpwn committed May 10, 2024
1 parent aa2d041 commit 94ab2d7
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 6 deletions.
6 changes: 6 additions & 0 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<artifactId>poi</artifactId>
<version>5.2.5</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public ResponseEntity<?> deleteAddress(@PathVariable Long addressId) {
@PatchMapping("/{addressId}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Edit address", description = "Edit address based on request body and address ID")
public ResponseEntity<?> updateAddress(@PathVariable Long addressId, @RequestBody AddressDto updatedAddressDto) {
public ResponseEntity<?> updateAddress(@PathVariable Long addressId, @Valid @RequestBody AddressDto updatedAddressDto) {
try {
Optional<Address> optionalExistingAddress = addressService.getAddressById(addressId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ResponseEntity<?> createProduct(@RequestBody @Valid ProductDto productDto
@PatchMapping("/{productId}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Edit product", description = "Edit product based on request body and product ID")
public ResponseEntity<?> updateProduct(@PathVariable Long productId, @RequestBody ProductDto updatedProductDto) {
public ResponseEntity<?> updateProduct(@PathVariable Long productId, @Valid @RequestBody ProductDto updatedProductDto) {
try {
Optional<Product> updatedProduct = productService.updateProduct(productId, updatedProductDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ResponseEntity<?> getUserById(@PathVariable Long userId) {
@ApiResponse(responseCode = "400", description = "Bad request", content = @Content),
@ApiResponse(responseCode = "500", description = "Internal server error", content = @Content)
})
public ResponseEntity<?> registerUser(@RequestBody(required = false) @Valid UserDto userDto, HttpServletResponse response) {
public ResponseEntity<?> registerUser(@RequestBody @Valid UserDto userDto, HttpServletResponse response) {
try {
return authService.registerUser(userDto, response);
} catch (Exception e) {
Expand Down Expand Up @@ -155,7 +155,7 @@ public ResponseEntity<?> loginUser(@Validated @RequestBody LoginRequest request,
@ApiResponse(responseCode = "404", description = "User not found"),
@ApiResponse(responseCode = "500", description = "An error occurred while deleting user")
})
public ResponseEntity<?> updateUser(@PathVariable Long userId, @RequestBody UserDto updatedUserDto) {
public ResponseEntity<?> updateUser(@PathVariable Long userId, @Valid @RequestBody UserDto updatedUserDto) {
try {
Optional<User> updatedUser = userService.updateUser(userId, updatedUserDto);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ibrahimokic.ordermanagement.domain.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -17,17 +18,21 @@ public class AddressDto {

@JsonProperty("street")
@NotNull
@NotBlank
private String street;

@JsonProperty("zip_code")
@NotNull
@NotBlank
private String zip;

@JsonProperty("city")
@NotNull
@NotBlank
private String city;

@JsonProperty("country")
@NotNull
@NotBlank
private String country;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -23,15 +24,18 @@ public class OrderDto {
private Long userId;

@JsonProperty("order_date")
@NotNull
private LocalDate orderDate;

@JsonProperty("total_amount")
private BigDecimal totalAmount;

@JsonProperty("delivery_address")
@NotNull
private AddressDto deliveryAddress;

@JsonProperty("source_address")
@NotNull
private AddressDto sourceAddress;

@JsonProperty("items")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ibrahimokic.ordermanagement.domain.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -22,9 +23,11 @@ public class OrderItemDto {

@JsonProperty("quantity")
@NotNull
@NotBlank(message = "Quantity is required")
private Integer quantity;

@JsonProperty("item_price")
@NotNull
@NotBlank(message = "Item price is required")
private BigDecimal itemPrice;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -22,10 +23,12 @@ public class ProductDto {

@JsonProperty("product_name")
@NotNull
@NotBlank(message = "Product name is required")
private String productName;

@JsonProperty("price")
@NotNull
@NotBlank(message = "Price is required")
private BigDecimal price;

@JsonProperty("available_from")
Expand All @@ -39,5 +42,6 @@ public class ProductDto {
private LocalDate availableUntil;

@JsonProperty("available_quantity")
@NotBlank(message = "Available quantity is required")
private int availableQuantity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Past;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;

Expand All @@ -21,14 +25,18 @@ public class UserDto {

@JsonProperty("username")
@NotNull
@NotBlank(message = "Username is required")
private String username;

@JsonProperty(value = "password", access = JsonProperty.Access.WRITE_ONLY)
@NotNull
@NotBlank(message = "Password is required")
private String password;

@JsonProperty("email")
@NotNull
@Email
@NotBlank(message = "Email is required")
private String email;

@JsonProperty("role")
Expand All @@ -37,14 +45,18 @@ public class UserDto {

@JsonProperty("first_name")
@NotNull
@NotBlank(message = "First name is required")
private String firstName;

@JsonProperty("last_name")
@NotNull
@NotBlank(message = "Last name is required")
private String lastName;

@JsonProperty("birth_date")
@NotNull
@Past(message = "Birth date must be in the past")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthDate;

@JsonProperty("address")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

public interface AuthService {
public ResponseEntity<?> loginUser(@Validated @RequestBody LoginRequest request, HttpServletResponse response);
public ResponseEntity<User> registerUser(@RequestBody(required = false) @Valid UserDto userDto, HttpServletResponse response);
public ResponseEntity<User> registerUser(@RequestBody @Valid UserDto userDto, HttpServletResponse response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Optional<User> updateUser(Long userId, UserDto updatedUserDto) {

if (optionalExistingUser.isPresent()) {
User existingUser = optionalExistingUser.get();
BeanUtils.copyProperties(updatedUserDto, existingUser);
BeanUtils.copyProperties(updatedUserDto, existingUser, "userId");

userRepository.save(existingUser);
return Optional.of(existingUser);
Expand All @@ -74,6 +74,7 @@ public Optional<User> updateUser(Long userId, UserDto updatedUserDto) {
}
}


@Override
public boolean deleteUser(Long userId) {
try {
Expand Down

0 comments on commit 94ab2d7

Please sign in to comment.