Skip to content

Commit

Permalink
containers set up
Browse files Browse the repository at this point in the history
  • Loading branch information
shelajev committed Jan 18, 2024
1 parent d4abaab commit 64bc0a2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ public void createProduct(CreateProductRequest request) {
}

public Optional<Product> getProductByCode(String code) {
// Optional<ProductEntity> productEntity = productRepository.findByCode(code);
// if (productEntity.isEmpty()) {
// return Optional.empty();
// }
// return productEntity.map(this::toProduct);
return Optional.empty();
Optional<ProductEntity> productEntity = productRepository.findByCode(code);
if (productEntity.isEmpty()) {
return Optional.empty();
}
return productEntity.map(this::toProduct);
}

public void uploadProductImage(String code, String imageName, InputStream inputStream) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ProductEntity, Long> {
Optional<ProductEntity> findByCode(String code);

public interface ProductRepository extends CosmosRepository<ProductEntity, String> {
@Modifying
@Query("update ProductEntity p set p.image = :image where p.code = :code")
void updateProductImage(@Param("code") String code, @Param("image") String image);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
15 changes: 15 additions & 0 deletions src/main/resources/db/migration/V1__catalog_tables.sql
Original file line number Diff line number Diff line change
@@ -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)
;

0 comments on commit 64bc0a2

Please sign in to comment.