88import org .springframework .http .HttpStatus ;
99import org .springframework .http .ResponseEntity ;
1010import org .springframework .web .bind .annotation .CrossOrigin ;
11+ import org .springframework .web .bind .annotation .DeleteMapping ;
1112import org .springframework .web .bind .annotation .GetMapping ;
1213import org .springframework .web .bind .annotation .PathVariable ;
1314import org .springframework .web .bind .annotation .RequestMapping ;
1819import com .dj .springeccom .Model .Product ;
1920import com .dj .springeccom .Service .ProductService ;
2021import org .springframework .web .bind .annotation .PostMapping ;
22+ import org .springframework .web .bind .annotation .PutMapping ;
2123
2224
2325@ RestController
2426@ RequestMapping ("/api" )
25- @ CrossOrigin (origins = "https://special-sniffle-q7wjwgg9pw4rf99wj -5173.app.github.dev/" )
27+ @ CrossOrigin (origins = "https://solid-pancake-q7wjwgg94vrvhx69 -5173.app.github.dev/" )
2628public class ProductController {
2729
2830 @ Autowired
@@ -47,7 +49,7 @@ public ResponseEntity<byte[]> getImageByProductId(@PathVariable int productId){
4749 public ResponseEntity <?> addProduct (@ RequestPart Product product ,@ RequestPart MultipartFile imageFile ) {
4850 Product savedProduct = null ;
4951 try {
50- savedProduct = service .addProduct (product ,imageFile );
52+ savedProduct = service .addOrUpdateProduct (product ,imageFile );
5153 return new ResponseEntity <>(savedProduct ,HttpStatus .CREATED );
5254 } catch (IOException e ){
5355 return new ResponseEntity <>(e .getMessage (),HttpStatus .INTERNAL_SERVER_ERROR );
@@ -61,5 +63,26 @@ public ResponseEntity<Product> getProductById(@PathVariable int id) {
6163 .orElseGet (() -> ResponseEntity .status (HttpStatus .NOT_FOUND ).build ());
6264 }
6365
66+ @ PutMapping ("/product/{id}" )
67+ public ResponseEntity <String > updateProdcut (@ PathVariable int id , @ RequestPart Product product ,@ RequestPart MultipartFile imageFile ){
68+ Product updatedProduct = null ;
69+ try {
70+ updatedProduct = service .addOrUpdateProduct (product ,imageFile );
71+ return new ResponseEntity <>("Updated" ,HttpStatus .OK );
72+ }catch (IOException e ){
73+ return new ResponseEntity <>(e .getMessage (),HttpStatus .BAD_REQUEST );
74+ }
75+ }
76+
77+ @ DeleteMapping ("/product/{id}" )
78+ public ResponseEntity <String > deleteProduct (@ PathVariable int id ){
79+ Optional <Product > product = service .getProductById (id );
80+ if (product .isPresent ()){
81+ service .deleteProduct (id );
82+ return new ResponseEntity <>("Deleted" ,HttpStatus .OK );
83+ }
84+ return new ResponseEntity <>("Product Not Found" ,HttpStatus .NOT_FOUND );
85+ }
86+
6487
6588}
0 commit comments