-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathProductController.java
More file actions
93 lines (75 loc) · 2.93 KB
/
ProductController.java
File metadata and controls
93 lines (75 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.ironhack.controllers;
import com.ironhack.models.Product;
import com.ironhack.services.ProductService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
private void validateApiKey(String apiKey) {
if (!"123456".equals(apiKey)) {
throw new RuntimeException("Invalid API Key");
}
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product createProduct(@RequestHeader("API-Key") String apiKey, @Valid @RequestBody Product product) {
validateApiKey(apiKey);
return productService.addProduct(product);
}
@GetMapping
public List<Product> getAllProducts(@RequestHeader("API-Key") String apiKey) {
validateApiKey(apiKey);
return productService.getAllProducts();
}
@GetMapping("/{name}")
public Product getProductByName(@RequestHeader("API-Key") String apiKey, @PathVariable String name) {
validateApiKey(apiKey);
Optional<Product> product = productService.getProductByName(name);
if (product.isEmpty()) {
throw new RuntimeException("Product not found");
}
return product.get();
}
@PutMapping("/{name}")
public Product updateProduct(@RequestHeader("API-Key") String apiKey, @PathVariable String name, @Valid @RequestBody Product product) {
validateApiKey(apiKey);
Product updated = productService.updateProduct(name, product);
if (updated == null) {
throw new RuntimeException("Product not found");
}
return updated;
}
@DeleteMapping("/{name}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteProduct(@RequestHeader("API-Key") String apiKey, @PathVariable String name) {
validateApiKey(apiKey);
boolean deleted = productService.deleteProduct(name);
if (!deleted) {
throw new RuntimeException("Product not found");
}
}
@GetMapping("/category/{category}")
public List<Product> getProductsByCategory(@RequestHeader("API-Key") String apiKey, @PathVariable String category) {
validateApiKey(apiKey);
return productService.getProductsByCategory(category);
}
@GetMapping("/price")
public List<Product> getProductsByPriceRange(
@RequestHeader("API-Key") String apiKey,
@RequestParam double min,
@RequestParam double max) {
validateApiKey(apiKey);
if (min < 0 || max < 0 || min > max) {
throw new RuntimeException("Invalid price range");
}
return productService.getProductsByPriceRange(min, max);
}
}