File tree Expand file tree Collapse file tree 4 files changed +63
-4
lines changed
src/main/java/com/amigoscode/product Expand file tree Collapse file tree 4 files changed +63
-4
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff line change 11package 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
85import java .util .List ;
96import 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments