Skip to content

Commit 42c89dc

Browse files
product-api: update sol
1 parent d0ad718 commit 42c89dc

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/main/java/com/amigoscode/product/Product.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class Product {
3737

3838
private Instant deletedAt;
3939

40-
public Product() {}
40+
public Product() {
41+
}
4142

4243
public Product(UUID id,
4344
String name,
@@ -54,16 +55,16 @@ public Product(UUID id,
5455
}
5556

5657
@PrePersist
57-
public void prePersist(){
58-
if(this.id == null) {
58+
public void prePersist() {
59+
if (this.id == null) {
5960
this.id = UUID.randomUUID();
6061
}
6162
this.createdAt = Instant.now();
6263
this.updatedAt = Instant.now();
6364
}
6465

6566
@PreUpdate
66-
public void preUpdate(){
67+
public void preUpdate() {
6768
this.updatedAt = Instant.now();
6869
}
6970

src/main/java/com/amigoscode/product/ProductController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public void deleteProductById(@PathVariable("id") UUID id) {
3434
public UUID saveProduct(@RequestBody NewProductRequest product) {
3535
return productService.saveNewProduct(product);
3636
}
37+
38+
@PutMapping("{id}")
39+
public void updateProduct(@PathVariable UUID id,
40+
@RequestBody UpdateProductRequest request) {
41+
productService.updateProduct(id, request);
42+
}
3743
}

src/main/java/com/amigoscode/product/ProductService.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.amigoscode.exception.ResourceNotFound;
44
import org.springframework.stereotype.Service;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PutMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
58

69
import java.util.List;
710
import java.util.UUID;
@@ -68,4 +71,30 @@ private Function<Product, ProductResponse> mapToResponse() {
6871
);
6972
}
7073

74+
75+
public void updateProduct(UUID id,
76+
UpdateProductRequest updateRequest) {
77+
Product product = productRepository.findById(id)
78+
.orElseThrow(() -> new ResourceNotFound(
79+
"product with id [" + id + "] not found"
80+
));
81+
82+
if (updateRequest.name() != null && !updateRequest.name().equals(product.getName())) {
83+
product.setName(updateRequest.name());
84+
}
85+
if (updateRequest.description() != null && !updateRequest.description().equals(product.getDescription())) {
86+
product.setDescription(updateRequest.description());
87+
}
88+
if (updateRequest.price() != null && !updateRequest.price().equals(product.getPrice())) {
89+
product.setPrice(updateRequest.price());
90+
}
91+
if (updateRequest.imageUrl() != null && !updateRequest.imageUrl().equals(product.getImageUrl())) {
92+
product.setImageUrl(updateRequest.imageUrl());
93+
}
94+
if (updateRequest.stockLevel() != null && !updateRequest.stockLevel().equals(product.getStockLevel())) {
95+
product.setStockLevel(updateRequest.stockLevel());
96+
}
97+
98+
productRepository.save(product);
99+
}
71100
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.amigoscode.product;
2+
3+
import java.math.BigDecimal;
4+
5+
public record UpdateProductRequest(
6+
String name,
7+
String description,
8+
String imageUrl,
9+
BigDecimal price,
10+
Integer stockLevel
11+
) {
12+
}

0 commit comments

Comments
 (0)