Skip to content

Commit 1ce58fb

Browse files
product-api: delete and save
1 parent 5d5bd1e commit 1ce58fb

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed
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 NewProductRequest(
6+
String name,
7+
String description,
8+
BigDecimal price,
9+
Integer stockLevel,
10+
String imageUrl
11+
) {
12+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ public class Product {
3737

3838
private Instant deletedAt;
3939

40+
public Product() {}
41+
42+
public Product(UUID id,
43+
String name,
44+
String description,
45+
BigDecimal price,
46+
String imageUrl,
47+
Integer stockLevel) {
48+
this.id = id;
49+
this.name = name;
50+
this.description = description;
51+
this.price = price;
52+
this.imageUrl = imageUrl;
53+
this.stockLevel = stockLevel;
54+
}
55+
4056
@PrePersist
4157
public void prePersist(){
4258
if(this.id == null) {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.amigoscode.product;
22

3-
import org.springframework.web.bind.annotation.GetMapping;
4-
import org.springframework.web.bind.annotation.PathVariable;
5-
import org.springframework.web.bind.annotation.RequestMapping;
6-
import org.springframework.web.bind.annotation.RestController;
3+
import org.springframework.web.bind.annotation.*;
74

85
import java.util.List;
96
import java.util.UUID;
@@ -27,4 +24,14 @@ public List<Product> getAllProducts() {
2724
public Product getProductById(@PathVariable("id") UUID id) {
2825
return productService.getProductById(id);
2926
}
27+
28+
@DeleteMapping("{id}")
29+
public void deleteProductById(@PathVariable("id") UUID id) {
30+
productService.deleteProductById(id);
31+
}
32+
33+
@PostMapping
34+
public UUID saveProduct(@RequestBody NewProductRequest product) {
35+
return productService.saveNewProduct(product);
36+
}
3037
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,28 @@ public Product getProductById(UUID id) {
2424
"product with id [" + id + "] not found"
2525
));
2626
}
27+
28+
public void deleteProductById(UUID id) {
29+
boolean exists = productRepository.existsById(id);
30+
if (!exists) {
31+
throw new ResourceNotFound(
32+
"product with id [" + id + "] not found"
33+
);
34+
}
35+
productRepository.deleteById(id);
36+
}
37+
38+
public UUID saveNewProduct(NewProductRequest product) {
39+
UUID id = UUID.randomUUID();
40+
Product newProduct = new Product(
41+
id,
42+
product.name(),
43+
product.description(),
44+
product.price(),
45+
product.imageUrl(),
46+
product.stockLevel()
47+
);
48+
productRepository.save(newProduct);
49+
return id;
50+
}
2751
}

0 commit comments

Comments
 (0)