From 64bc0a223172090fdffdb1edf23afcbfbd125bef Mon Sep 17 00:00:00 2001 From: shelajev Date: Thu, 18 Jan 2024 00:12:23 -0500 Subject: [PATCH] containers set up --- pom.xml | 8 ++--- .../internal/DefaultProductService.java | 11 ++++--- .../domain/internal/ProductEntity.java | 30 +++++++++++++------ .../domain/internal/ProductRepository.java | 11 +++++-- .../domain/models/CreateProductRequest.java | 2 +- .../catalog/domain/models/Product.java | 2 +- .../db/migration/V1__catalog_tables.sql | 15 ++++++++++ 7 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 src/main/resources/db/migration/V1__catalog_tables.sql diff --git a/pom.xml b/pom.xml index 3f27a67..9747232 100644 --- a/pom.xml +++ b/pom.xml @@ -44,10 +44,10 @@ org.springframework.boot spring-boot-starter-actuator - - - - + + org.springframework.boot + spring-boot-starter-data-jpa + org.postgresql postgresql diff --git a/src/main/java/com/testcontainers/catalog/domain/internal/DefaultProductService.java b/src/main/java/com/testcontainers/catalog/domain/internal/DefaultProductService.java index c5c7245..cf152c1 100644 --- a/src/main/java/com/testcontainers/catalog/domain/internal/DefaultProductService.java +++ b/src/main/java/com/testcontainers/catalog/domain/internal/DefaultProductService.java @@ -46,12 +46,11 @@ public void createProduct(CreateProductRequest request) { } public Optional getProductByCode(String code) { -// Optional productEntity = productRepository.findByCode(code); -// if (productEntity.isEmpty()) { -// return Optional.empty(); -// } -// return productEntity.map(this::toProduct); - return Optional.empty(); + Optional productEntity = productRepository.findByCode(code); + if (productEntity.isEmpty()) { + return Optional.empty(); + } + return productEntity.map(this::toProduct); } public void uploadProductImage(String code, String imageName, InputStream inputStream) { diff --git a/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java b/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java index b55654b..542da3f 100644 --- a/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java +++ b/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java @@ -1,30 +1,42 @@ package com.testcontainers.catalog.domain.internal; -import com.azure.spring.data.cosmos.core.mapping.Container; +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 jakarta.validation.constraints.DecimalMin; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import java.math.BigDecimal; -//@Entity -//@Table(name = "products") -@Container(containerName = "person", ru = "400") -public class ProductEntity { +@Entity +@Table(name = "products") +class ProductEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(nullable = false, unique = true) + @NotEmpty(message = "Product code must not be null/empty") private String code; + @NotEmpty(message = "Product name must not be null/empty") + @Column(nullable = false) private String name; private String description; private String image; - private Double price; + @NotNull(message = "Product price must not be null") @DecimalMin("0.1") + @Column(nullable = false) + private BigDecimal price; public ProductEntity() {} - public ProductEntity(Long id, String code, String name, String description, String image, Double price) { + public ProductEntity(Long id, String code, String name, String description, String image, BigDecimal price) { this.id = id; this.code = code; this.name = name; @@ -73,11 +85,11 @@ public void setImage(String image) { this.image = image; } - public Double getPrice() { + public BigDecimal getPrice() { return price; } - public void setPrice(Double price) { + public void setPrice(BigDecimal price) { this.price = price; } } diff --git a/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java b/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java index 630af61..09e5ec3 100644 --- a/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java +++ b/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java @@ -1,8 +1,15 @@ package com.testcontainers.catalog.domain.internal; import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; -import com.azure.spring.data.cosmos.repository.CosmosRepository; +interface ProductRepository extends JpaRepository { + Optional findByCode(String code); -public interface ProductRepository extends CosmosRepository { + @Modifying + @Query("update ProductEntity p set p.image = :image where p.code = :code") + void updateProductImage(@Param("code") String code, @Param("image") String image); } diff --git a/src/main/java/com/testcontainers/catalog/domain/models/CreateProductRequest.java b/src/main/java/com/testcontainers/catalog/domain/models/CreateProductRequest.java index 4e64d4e..18f1aab 100644 --- a/src/main/java/com/testcontainers/catalog/domain/models/CreateProductRequest.java +++ b/src/main/java/com/testcontainers/catalog/domain/models/CreateProductRequest.java @@ -6,4 +6,4 @@ import java.math.BigDecimal; public record CreateProductRequest( - @NotEmpty String code, @NotEmpty String name, String description, @NotNull @Positive Double price) {} + @NotEmpty String code, @NotEmpty String name, String description, @NotNull @Positive BigDecimal price) {} diff --git a/src/main/java/com/testcontainers/catalog/domain/models/Product.java b/src/main/java/com/testcontainers/catalog/domain/models/Product.java index 0c56c99..829ab9f 100644 --- a/src/main/java/com/testcontainers/catalog/domain/models/Product.java +++ b/src/main/java/com/testcontainers/catalog/domain/models/Product.java @@ -3,4 +3,4 @@ import java.math.BigDecimal; public record Product( - Long id, String code, String name, String description, String imageUrl, Double price, boolean available) {} + Long id, String code, String name, String description, String imageUrl, BigDecimal price, boolean available) {} diff --git a/src/main/resources/db/migration/V1__catalog_tables.sql b/src/main/resources/db/migration/V1__catalog_tables.sql new file mode 100644 index 0000000..153aa00 --- /dev/null +++ b/src/main/resources/db/migration/V1__catalog_tables.sql @@ -0,0 +1,15 @@ +create table products +( + id bigserial primary key, + code varchar not null unique, + name varchar not null, + description varchar, + image varchar, + price numeric not null +); + +insert into products(code, name, description, image, price) values + ('P101','Product P101','Product P101 description', null, 34.0), + ('P102','Product P102','Product P102 description', null, 25.0), + ('P103','Product P103','Product P103 description', null, 15.0) +; \ No newline at end of file