Skip to content

Commit 6f5cf2e

Browse files
committed
Update Productrestcontroller
1 parent 4fa3aca commit 6f5cf2e

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

backend/webandtech/src/main/java/webapp8/webandtech/controller/api/products/ProductsRestController.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.core.io.InputStreamResource;
1414
import org.springframework.core.io.Resource;
1515
import org.springframework.data.domain.PageRequest;
16+
import org.springframework.data.domain.Sort;
1617
import org.springframework.http.HttpHeaders;
1718
import org.springframework.http.HttpStatus;
1819
import org.springframework.http.ResponseEntity;
@@ -36,7 +37,9 @@
3637
import io.swagger.v3.oas.annotations.responses.ApiResponse;
3738
import io.swagger.v3.oas.annotations.responses.ApiResponses;
3839
import webapp8.webandtech.model.Product;
40+
import webapp8.webandtech.model.Rating;
3941
import webapp8.webandtech.service.ProductService;
42+
import webapp8.webandtech.service.RatingService;
4043

4144
@RestController
4245
@CrossOrigin
@@ -46,6 +49,9 @@ public class ProductsRestController {
4649

4750
@Autowired
4851
private ProductService productService;
52+
53+
@Autowired
54+
private RatingService ratingService;
4955

5056
@Operation(summary = "Get New Six Products")
5157
@ApiResponses(value = {
@@ -415,4 +421,115 @@ public ResponseEntity<Object> uploadImage3( @Parameter(description="id of Produc
415421
}
416422
}
417423

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+
418535
}

0 commit comments

Comments
 (0)