-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: zufar-sunagatov <[email protected]>
- Loading branch information
1 parent
f5be79c
commit 2f36c72
Showing
10 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/zufar/onlinestore/product/ProductsSumCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.zufar.onlinestore.product; | ||
|
||
import com.zufar.onlinestore.product.dto.ProductInfoDto; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collection; | ||
|
||
@Service | ||
public class ProductsSumCalculator { | ||
|
||
public BigDecimal calculate(final Collection<ProductInfoDto> products) { | ||
return products.stream() | ||
.map(productInfo -> productInfo.priceDetails().price()) | ||
.reduce(BigDecimal.ZERO, BigDecimal::add); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/zufar/onlinestore/product/converter/ProductInfoDtoConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.zufar.onlinestore.product.converter; | ||
|
||
import com.zufar.onlinestore.product.dto.PriceDetailsDto; | ||
import com.zufar.onlinestore.product.dto.ProductInfoDto; | ||
import com.zufar.onlinestore.product.entity.ProductInfo; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ProductInfoDtoConverter { | ||
|
||
public ProductInfoDto convertToDto(final ProductInfo entity) { | ||
PriceDetailsDto priceDetailsDto = new PriceDetailsDto(entity.getPrice(), entity.getCurrency()); | ||
|
||
return new ProductInfoDto( | ||
entity.getId(), | ||
entity.getDescription(), | ||
entity.getName(), | ||
priceDetailsDto | ||
); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/zufar/onlinestore/product/dto/PriceDetailsDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.zufar.onlinestore.product.dto; | ||
|
||
import jakarta.validation.constraints.DecimalMin; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public record PriceDetailsDto( | ||
@NotNull(message = "Price is mandatory") | ||
@DecimalMin(value = "0.0", message = "Price minimum value should be more than 0.0") | ||
BigDecimal price, | ||
|
||
@NotBlank(message = "Currency is mandatory") | ||
@Size(max = 55, message = "Currency length must be less than 55 characters") | ||
String currency | ||
) { | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/zufar/onlinestore/product/dto/ProductInfoDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.zufar.onlinestore.product.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
|
||
import java.util.UUID; | ||
|
||
public record ProductInfoDto( | ||
@NotNull(message = "Id is mandatory") | ||
UUID id, | ||
|
||
@NotBlank(message = "Name is mandatory") | ||
@Size(max = 100, message = "Name length must be less than 100 characters") | ||
String name, | ||
|
||
@NotBlank(message = "Description is mandatory") | ||
@Size(max = 1000, message = "Description length must be less than 1000 characters") | ||
String description, | ||
|
||
@NotNull(message = "Price details is mandatory") | ||
PriceDetailsDto priceDetails | ||
) { | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/zufar/onlinestore/product/endpoint/ProductsEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.zufar.onlinestore.product.endpoint; | ||
|
||
import com.zufar.onlinestore.product.converter.ProductInfoDtoConverter; | ||
import com.zufar.onlinestore.product.dto.ProductInfoDto; | ||
import com.zufar.onlinestore.product.entity.ProductInfo; | ||
import com.zufar.onlinestore.product.repository.ProductInfoRepository; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@Validated | ||
@RequestMapping(value = "/api/products") | ||
public class ProductsEndpoint { | ||
private final ProductInfoRepository productInfoRepository; | ||
private final ProductInfoDtoConverter productInfoDtoConverter; | ||
|
||
@GetMapping("/{id}") | ||
@ResponseBody | ||
public ResponseEntity<ProductInfoDto> getProductInfoById(@PathVariable("id") final String id) { | ||
log.info("Received request to get the ProductInfo with id - {}.", id); | ||
Optional<ProductInfo> ProductInfo = productInfoRepository.findById(Integer.parseInt(id)); | ||
if (ProductInfo.isEmpty()) { | ||
log.info("the ProductInfo with id - {} is absent.", id); | ||
return ResponseEntity.notFound() | ||
.build(); | ||
} | ||
ProductInfoDto ProductInfoDto = productInfoDtoConverter.convertToDto(ProductInfo.get()); | ||
log.info("the ProductInfo with id - {} was retrieved - {}.", id, ProductInfoDto); | ||
return ResponseEntity.ok() | ||
.body(ProductInfoDto); | ||
} | ||
|
||
@GetMapping | ||
@ResponseBody | ||
public ResponseEntity<Collection<ProductInfoDto>> getAllProducts() { | ||
log.info("Received request to get all ProductInfos"); | ||
Collection<ProductInfo> productInfoCollection = productInfoRepository.findAll(); | ||
if (productInfoCollection.isEmpty()) { | ||
log.info("All ProductInfos are absent."); | ||
return ResponseEntity.notFound() | ||
.build(); | ||
} | ||
Collection<ProductInfoDto> ProductInfos = productInfoCollection.stream() | ||
.map(productInfoDtoConverter::convertToDto) | ||
.toList(); | ||
|
||
log.info("All ProductInfos were retrieved - {}.", ProductInfos); | ||
return ResponseEntity.ok() | ||
.body(ProductInfos); | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/zufar/onlinestore/product/entity/ProductInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.zufar.onlinestore.product.entity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "product") | ||
public class ProductInfo { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
private UUID id; | ||
|
||
@Column(name = "name", nullable = false) | ||
private String name; | ||
|
||
@Column(name = "description", nullable = false) | ||
private String description; | ||
|
||
@Column(name = "price", nullable = false) | ||
private BigDecimal price; | ||
|
||
@Column(name = "currency", nullable = false) | ||
private String currency; | ||
|
||
@Column(name = "quantity", nullable = false) | ||
private Integer quantity; | ||
|
||
@Column(name = "active", nullable = false) | ||
private Boolean active; | ||
|
||
public ProductInfo() { | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) | ||
return true; | ||
if (object == null || getClass() != object.getClass()) | ||
return false; | ||
ProductInfo that = (ProductInfo) object; | ||
return active == that.active && | ||
Objects.equals(id, that.id) && | ||
Objects.equals(name, that.name) && | ||
Objects.equals(description, that.description) && | ||
Objects.equals(price, that.price) && | ||
Objects.equals(currency, that.currency) && | ||
Objects.equals(quantity, that.quantity); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, name, description, price, currency, quantity, active); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ProductInfo {" + | ||
"id=" + id + | ||
", name='" + name + '\'' + | ||
", description='" + description + '\'' + | ||
", price=" + price + | ||
", currency='" + currency + '\'' + | ||
", quantity=" + quantity + | ||
", active=" + active + | ||
'}'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/zufar/onlinestore/product/repository/ProductInfoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.zufar.onlinestore.product.repository; | ||
|
||
import com.zufar.onlinestore.product.entity.ProductInfo; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ProductInfoRepository extends JpaRepository<ProductInfo, Integer> { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/resources/db/changelog/19.07.2023.product-table-was-created.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CREATE TABLE IF NOT EXISTS product | ||
( | ||
id UUID, | ||
name VARCHAR(64) NOT NULL, | ||
description TEXT, | ||
price DECIMAL NOT NULL CHECK (price > 0), | ||
currency VARCHAR NOT NULL, | ||
quantity INT NOT NULL CHECK (quantity >= 0), | ||
active BOOLEAN NOT NULL, | ||
PRIMARY KEY (id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
databaseChangeLog: | ||
- include: | ||
file: 19.07.2023.product-table-was-created.sql |