Skip to content

Commit e851789

Browse files
committed
Update API
1 parent 7838eb4 commit e851789

File tree

4 files changed

+104
-81
lines changed

4 files changed

+104
-81
lines changed

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

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -66,88 +66,88 @@ public List<Product> getAllProducts(){
6666
}
6767

6868

69-
// @Operation(summary = "Create a Products")
70-
// @ApiResponses(value = {
71-
// @ApiResponse(
72-
// responseCode = "201",
73-
// description = "Successful Products creation",
74-
// content = {@Content(
75-
// mediaType = "application/json"
76-
// )}
77-
// ),
78-
// @ApiResponse(
79-
// responseCode = "406",
80-
// description = "Not Acceptable Post title exists",
81-
// content = @Content
82-
// )
83-
// })
84-
// @JsonView(Product.Detailed.class)
85-
// @PostMapping("/")
86-
// public ResponseEntity<Product> registerProduct( @Parameter(description="Object Type Product") @RequestBody Product product) throws IOException{
87-
// if (productService.existsProduct(product.getTitle())) {
88-
// return new ResponseEntity<Product>(product,HttpStatus.NOT_ACCEPTABLE);
89-
// }
90-
// product.setImg1(false);
91-
// product.setImg2(false);
92-
// product.setImg3(false);
93-
// productService.save(product);
94-
// product = productService.getProductByTitle(product.getTitle());
95-
// URI location = fromCurrentRequest().path("/{id}").buildAndExpand(product.getIdproduct()).toUri();
96-
// return ResponseEntity.created(location).body(product);
97-
// }
69+
@Operation(summary = "Create a Products")
70+
@ApiResponses(value = {
71+
@ApiResponse(
72+
responseCode = "201",
73+
description = "Successful Products creation",
74+
content = {@Content(
75+
mediaType = "application/json"
76+
)}
77+
),
78+
@ApiResponse(
79+
responseCode = "406",
80+
description = "Not Acceptable Post title exists",
81+
content = @Content
82+
)
83+
})
84+
@JsonView(Product.Detailed.class)
85+
@PostMapping("/")
86+
public ResponseEntity<Product> registerProduct( @Parameter(description="Object Type Product") @RequestBody Product product) throws IOException{
87+
if (productService.existsProduct(product.getNameproduct())) {
88+
return new ResponseEntity<Product>(product,HttpStatus.NOT_ACCEPTABLE);
89+
}
90+
product.setImg1(false);
91+
product.setImg2(false);
92+
product.setImg3(false);
93+
productService.save(product);
94+
product = productService.getProductByName(product.getNameproduct());
95+
URI location = fromCurrentRequest().path("/{id}").buildAndExpand(product.getIdproduct()).toUri();
96+
return ResponseEntity.created(location).body(product);
97+
}
9898

99-
// @Operation(summary = "Get a products by its id")
100-
// @ApiResponses(value = {
101-
// @ApiResponse(
102-
// responseCode = "200",
103-
// description = "Found the Product",
104-
// content = {@Content(
105-
// mediaType = "application/json"
106-
// )}
107-
// ),
108-
// @ApiResponse(
109-
// responseCode = "404",
110-
// description = "Product not found",
111-
// content = @Content
112-
// )
113-
// })
114-
// @JsonView(Product.Detailed.class)
115-
// @GetMapping("/{id}")
116-
// public ResponseEntity<Product> getProduct ( @Parameter(description="id of Product to be searched") @PathVariable int id) throws IOException{
117-
// Optional<Product> product = productService.getProductById(id);
118-
// if(!product.isEmpty()) {
119-
// return ResponseEntity.ok(product.get());
120-
// }else {
121-
// return ResponseEntity.notFound().build();
122-
// }
123-
// }
99+
@Operation(summary = "Get a products by its id")
100+
@ApiResponses(value = {
101+
@ApiResponse(
102+
responseCode = "200",
103+
description = "Found the Product",
104+
content = {@Content(
105+
mediaType = "application/json"
106+
)}
107+
),
108+
@ApiResponse(
109+
responseCode = "404",
110+
description = "Product not found",
111+
content = @Content
112+
)
113+
})
114+
@JsonView(Product.Detailed.class)
115+
@GetMapping("/{id}")
116+
public ResponseEntity<Product> getProduct ( @Parameter(description="id of Product to be searched") @PathVariable int id) throws IOException{
117+
Optional<Product> product = productService.getProductById(id);
118+
if(!product.isEmpty()) {
119+
return ResponseEntity.ok(product.get());
120+
}else {
121+
return ResponseEntity.notFound().build();
122+
}
123+
}
124124

125-
// @Operation(summary = "Create a Product")
126-
// @ApiResponses(value = {
127-
// @ApiResponse(
128-
// responseCode = "201",
129-
// description = "Successful Product creation",
130-
// content = {@Content(
131-
// mediaType = "application/json"
132-
// )}
133-
// ),
134-
// @ApiResponse(
135-
// responseCode = "404",
136-
// description = "Product not found",
137-
// content = @Content
138-
// )
139-
// })
140-
// @JsonView(Product.Detailed.class)
141-
// @DeleteMapping("/{id}")
142-
// public ResponseEntity<Product> deleteProduct( @Parameter(description="id of Product to be searched") @PathVariable int id){
143-
// Optional<Product> product = productService.getProductById(id);
144-
// if(product.isPresent()){
145-
// productService.deleteProductbyid(product.get().getIdproduct());
146-
// return ResponseEntity.ok(product.get());
147-
// }else {
148-
// return ResponseEntity.notFound().build();
149-
// }
150-
// }
125+
@Operation(summary = "Delete a Product")
126+
@ApiResponses(value = {
127+
@ApiResponse(
128+
responseCode = "201",
129+
description = "Successful Product delete",
130+
content = {@Content(
131+
mediaType = "application/json"
132+
)}
133+
),
134+
@ApiResponse(
135+
responseCode = "404",
136+
description = "Product not found",
137+
content = @Content
138+
)
139+
})
140+
@JsonView(Product.Detailed.class)
141+
@DeleteMapping("/{id}")
142+
public ResponseEntity<Product> deleteProduct( @Parameter(description="id of Product to be searched") @PathVariable int id){
143+
Optional<Product> product = productService.getProductById(id);
144+
if(product.isPresent()){
145+
productService.deleteProductApiById(product.get().getIdproduct());
146+
return ResponseEntity.ok(product.get());
147+
}else {
148+
return ResponseEntity.notFound().build();
149+
}
150+
}
151151

152152
// @Operation(summary = "Modify a Product")
153153
// @ApiResponses(value = {

backend/webandtech/src/main/java/webapp8/webandtech/repository/ProductRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public interface ProductRepository extends JpaRepository<Product, Integer>{
2020
public Page<Product> findByproductcategory(String productcategory, Pageable page);
2121
public List<Product> findByproductcategory(String productcategory);
2222
public Page<Product> findByproductbrand(Brand brandname, Pageable page);
23+
public boolean existsIdproductBynameproduct(String nameproduct);
2324

2425
//public boolean existsidproductBynameproduct(String nameproduct);
2526
}

backend/webandtech/src/main/java/webapp8/webandtech/service/ProductService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.List;
55
import java.util.NoSuchElementException;
6+
import java.util.Optional;
67

78
import org.hibernate.engine.jdbc.BlobProxy;
89
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,7 +32,14 @@ public class ProductService {
3132

3233

3334
//api methods
35+
public void deleteProductApiById(int id) {
36+
Product pref = productRepository.findByIdproduct(id);
37+
productRepository.deleteById(pref.getIdproduct());
38+
}
3439

40+
public Optional<Product> getProductById(int id) {
41+
return productRepository.findById(id);
42+
}
3543

3644
//normal methods
3745
public void save(Product product){
@@ -67,6 +75,10 @@ public Page<Product> getProductsPage(Pageable page) {
6775
public Product getProduct(int idproduct) {
6876
return productRepository.findById(idproduct).orElseThrow(() -> new NoSuchElementException("Product not found"));
6977
}
78+
79+
public Product getProductByName(String nameproduct) {
80+
return productRepository.findBynameproduct(nameproduct);
81+
}
7082

7183
public Page<Product> getComponentsPage(Pageable page){
7284

@@ -120,6 +132,9 @@ public Page<Product> getMorePhones(Pageable page){
120132
Page<Product> phones = productRepository.findByproductcategory("telefono",page);
121133
return phones;
122134
}
135+
public boolean existsProduct(String nameProduct) {
136+
return productRepository.existsIdproductBynameproduct(nameProduct);
137+
}
123138

124139
@Modifying
125140
public void modifyDataProduct(Product product, MultipartFile image1, MultipartFile image2, MultipartFile image3) throws IOException {
@@ -190,4 +205,6 @@ public void deleteProductById(int idproduct) {
190205
ratingRepository.deleteByIdproduct(prev);
191206
productRepository.deleteById(prev.getIdproduct());
192207
}
208+
209+
193210
}

backend/webandtech/target/classes/application.properties

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ spring.jpa.properties.hibernate.format_sql=true
1919
logging.level.org.hibernate.SQL=DEBUG
2020
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
2121

22-
spring.datasource.hikari.auto-commit=false
22+
springdoc.swagger-ui.path=/swagger-ui.html
23+
springdoc.packagesToScan=webapp8.webandtech.controller.api
24+
25+
jwt.secret=spiceandwolf
26+
27+
spring.datasource.hikari.auto-commit=false

0 commit comments

Comments
 (0)