Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ build/

### VS Code ###
.vscode/
.env
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.25.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand All @@ -49,6 +53,12 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>me.paulschwarz</groupId>
<artifactId>spring-dotenv</artifactId>
<version>4.0.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webclient-test</artifactId>
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/net/hackyourfuture/hyfshop/product/B2Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.hackyourfuture.hyfshop.product;

import java.net.URI;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;


@Configuration
public class B2Config {

@Value("${b2.endpoint}") private String endpoint;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice that you use these values as config.

@Value("${b2.region}") private String region;
@Value("${b2.access-key}") private String accessKey;
@Value("${b2.secret-key}") private String secretKey;

@Bean
public S3Client s3Client() {
return S3Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)))
.endpointOverride(URI.create(endpoint))
.region(Region.of(region))
.build();
}

@Bean
public S3Presigner s3Presigner() {
return S3Presigner.builder()
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)))
.endpointOverride(URI.create(endpoint))
.region(Region.of(region))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.hackyourfuture.hyfshop.product;

public class FileService {
}
2 changes: 2 additions & 0 deletions src/main/java/net/hackyourfuture/hyfshop/product/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Setter;

import java.math.BigDecimal;
import java.util.Map;

@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -17,4 +18,5 @@ public class Product {
private BigDecimal price;
private String category;
private String imageUrl;
private Map<String, Object> details;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Repository;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import static org.springframework.web.servlet.function.RequestPredicates.param;

@Repository
@AllArgsConstructor
public class ProductRepository {
private final JdbcClient jdbcClient;
private static final ObjectMapper objectMapper = new ObjectMapper();

public static final RowMapper<Product> PRODUCT_ROW_MAPPER = (rs, _) -> {
var product = new Product();
Expand All @@ -18,20 +26,31 @@ public class ProductRepository {
product.setPrice(rs.getBigDecimal("price"));
product.setCategory(rs.getString("category"));
product.setImageUrl(rs.getString("image_url"));
try {
String json = rs.getString("details");
if (json != null) {
product.setDetails(objectMapper.readValue(json,
new TypeReference<Map<String, Object>>() {
}
));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return product;
};

public List<Product> getAllProducts() {
return jdbcClient
.sql("SELECT id, title, price, category, image_url FROM products")
.sql("SELECT id, title, price, category, image_url, details FROM products")
.query(PRODUCT_ROW_MAPPER)
.list();

}

public Product findById(int id) {
return jdbcClient
.sql("SELECT id, title, price, category, image_url FROM products WHERE id = :id")
.sql("SELECT id, title, price, category, image_url, details FROM products WHERE id = :id")
.param("id", id)
.query(PRODUCT_ROW_MAPPER)
.single();
Expand All @@ -49,12 +68,25 @@ public void setImageUrl(int id, String imageUrl) {
}

public List<Product> findByColor(String color) {
// TODO: Implement
throw new UnsupportedOperationException("Not implemented yet");
return jdbcClient.sql("""
SELECT id, title, price, category, image_url, details
FROM products
WHERE details ->> 'color' = :color
LIMIT 10
""")
.param("color", color)
.query(PRODUCT_ROW_MAPPER)
.list();
}

public Product setSize(int id, String size) {
// TODO: Implement
throw new UnsupportedOperationException("Not implemented yet");
public void setSize(int id, String size) {
jdbcClient.sql("""
UPDATE products
SET details = jsonb_set(details, '{size}', to_jsonb(:size::text))
WHERE id = :id
""")
.param("id", id)
.param("size", size)
.update();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ProductService {
private final ProductRepository productRepository;
private final FileService fileService;

public List<ProductResponse> getAllProducts() {
return productRepository.getAllProducts().stream().map(ProductResponse::from).toList();
Expand All @@ -26,14 +28,22 @@ public ProductResponse setProductSize(int id, String size) {
}

public ProductResponse setProductImage(int id, MultipartFile file) {
// TODO: Implement
// call ProductRepository.setImageUrl() afterwards with the new URL
throw new UnsupportedOperationException("Not implemented yet");
try {
String key = fileService.upload(file);
productRepository.setImageUrl(id, key);
return ProductResponse.from(productRepository.findById(id));
} catch (Exception e) {
throw new RuntimeException("Failed to upload product image", e);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to log the exception.

}
}

public ProductResponse deleteProductImage(int id) {
// TODO: Implement
// call ProductRepository.setImageUrl() to set the image url to null
throw new UnsupportedOperationException("Not implemented yet");
Product product = productRepository.findById(id);
String key = product.getImageUrl();
if (key != null) {
fileService.delete(key);
}
productRepository.setImageUrl(id, null);
return ProductResponse.from(productRepository.findById(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import net.hackyourfuture.hyfshop.product.Product;

import java.math.BigDecimal;
import java.util.Map;

public record ProductResponse (
int id,
String title,
BigDecimal price,
String category,
String imageUrl
String imageUrl,
Map<String, Object> details
){
public static ProductResponse from(Product product) {
return new ProductResponse(
product.getId(),
product.getTitle(),
product.getPrice(),
product.getCategory(),
product.getImageUrl()
product.getImageUrl(),
product.getDetails()
);
}
}
19 changes: 15 additions & 4 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
spring:
application:
name: hyf_shop

datasource:
url: '${DB_URL}'
username: '${DB_USERNAME}'
password: '${DB_PASSWORD}'
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: org.postgresql.Driver

b2:
endpoint: ${B2_ENDPOINT}
region: ${B2_REGION}
bucket: ${B2_BUCKET_NAME}
access-key: ${B2_ACCESS_KEY}
secret-key: ${B2_SECRET_KEY}
public-url: https://f003.backblazeb2.com/file/${B2_BUCKET_NAME}

server:
port: '8080'
port: 8080