13
13
import org .springframework .core .io .InputStreamResource ;
14
14
import org .springframework .core .io .Resource ;
15
15
import org .springframework .data .domain .PageRequest ;
16
+ import org .springframework .data .domain .Sort ;
16
17
import org .springframework .http .HttpHeaders ;
17
18
import org .springframework .http .HttpStatus ;
18
19
import org .springframework .http .ResponseEntity ;
36
37
import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
37
38
import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
38
39
import webapp8 .webandtech .model .Product ;
40
+ import webapp8 .webandtech .model .Rating ;
39
41
import webapp8 .webandtech .service .ProductService ;
42
+ import webapp8 .webandtech .service .RatingService ;
40
43
41
44
@ RestController
42
45
@ CrossOrigin
@@ -46,6 +49,9 @@ public class ProductsRestController {
46
49
47
50
@ Autowired
48
51
private ProductService productService ;
52
+
53
+ @ Autowired
54
+ private RatingService ratingService ;
49
55
50
56
@ Operation (summary = "Get New Six Products" )
51
57
@ ApiResponses (value = {
@@ -415,4 +421,115 @@ public ResponseEntity<Object> uploadImage3( @Parameter(description="id of Produc
415
421
}
416
422
}
417
423
424
+ @ Operation (summary = "Get a peripheral" )
425
+ @ ApiResponses (value = {
426
+ @ ApiResponse (
427
+ responseCode = "200" ,
428
+ description = "Found the Product" ,
429
+ content = {@ Content (
430
+ mediaType = "application/json"
431
+ )}
432
+ ),
433
+ @ ApiResponse (
434
+ responseCode = "404" ,
435
+ description = "Product not found" ,
436
+ content = @ Content
437
+ )
438
+ })
439
+ @ JsonView (Product .Detailed .class )
440
+ @ GetMapping ("/peripherals" )
441
+ public List <Product > getPeripheralsPage ( @ Parameter (description ="page" ) @ RequestParam (required = false ) String page , @ Parameter (description ="typeProduct" ) @ RequestParam (required = false ) String typeProduct ) throws IOException {
442
+
443
+ if (typeProduct != null ) {
444
+ return productService .getMoreProductType (PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idproduct" ).ascending ()), typeProduct ).getContent ();
445
+ }else {
446
+ return productService .getPeripheralsPage (PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idproduct" ).ascending ())).getContent ();
447
+ }
448
+ }
449
+ @ Operation (summary = "Get a component" )
450
+ @ ApiResponses (value = {
451
+ @ ApiResponse (
452
+ responseCode = "200" ,
453
+ description = "Found the Product" ,
454
+ content = {@ Content (
455
+ mediaType = "application/json"
456
+ )}
457
+ ),
458
+ @ ApiResponse (
459
+ responseCode = "404" ,
460
+ description = "Product not found" ,
461
+ content = @ Content
462
+ )
463
+ })
464
+ @ JsonView (Product .Detailed .class )
465
+ @ GetMapping ("/components" )
466
+ public List <Product > getComponentsPage ( @ Parameter (description ="page" ) @ RequestParam (required = false ) String page , @ Parameter (description ="typeProduct" ) @ RequestParam (required = false ) String typeProduct ) throws IOException {
467
+
468
+ if (typeProduct != null ) {
469
+ return productService .getMoreProductType (PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idproduct" ).ascending ()), typeProduct ).getContent ();
470
+ }else {
471
+ return productService .getComponentsPage (PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idproduct" ).ascending ())).getContent ();
472
+ }
473
+ }
474
+
475
+ @ Operation (summary = "Get a phone" )
476
+ @ ApiResponses (value = {
477
+ @ ApiResponse (
478
+ responseCode = "200" ,
479
+ description = "Found the Product" ,
480
+ content = {@ Content (
481
+ mediaType = "application/json"
482
+ )}
483
+ ),
484
+ @ ApiResponse (
485
+ responseCode = "404" ,
486
+ description = "Product not found" ,
487
+ content = @ Content
488
+ )
489
+ })
490
+ @ JsonView (Product .Detailed .class )
491
+ @ GetMapping ("/phones" )
492
+ public List <Product > getPhonesPage ( @ Parameter (description ="page" ) @ RequestParam (required = false ) String page , @ Parameter (description ="typeProduct" ) @ RequestParam (required = false ) String typeProduct ) throws IOException {
493
+
494
+ if (typeProduct != null ) {
495
+ return productService .getMoreProductType (PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idproduct" ).ascending ()), typeProduct ).getContent ();
496
+ }else {
497
+ return productService .getPhonesPage (PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idproduct" ).ascending ())).getContent ();
498
+ }
499
+ }
500
+
501
+ @ Operation (summary = "Ratings" )
502
+ @ ApiResponses (value = {
503
+ @ ApiResponse (
504
+ responseCode = "200" ,
505
+ description = "Found the Image Product" ,
506
+ content = {@ Content (
507
+ mediaType = "application/json"
508
+ )}
509
+ ),
510
+ @ ApiResponse (
511
+ responseCode = "404" ,
512
+ description = "Product not found" ,
513
+ content = @ Content
514
+ ),
515
+ @ ApiResponse (
516
+ responseCode = "204" ,
517
+ description = "Image not found" ,
518
+ content = @ Content
519
+ )
520
+ })
521
+ @ GetMapping ("/{id}/ratings" )
522
+ public List <Rating > getProductRatings ( @ Parameter (description ="id of Product to be searched" ) @ PathVariable int id , @ Parameter (description ="page" ) @ RequestParam (required = false ) String page ) throws SQLException {
523
+ Optional <Product > product = productService .getProductById (id );
524
+ if (!product .isEmpty ()) {
525
+ List <Rating > productList = ratingService .getMoreRating (PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idproduct" ).ascending ()), product .get ()).getContent ();
526
+ return productList ;
527
+ }else {
528
+ return null ;
529
+ }
530
+
531
+ }
532
+
533
+
534
+
418
535
}
0 commit comments