From 0326d4d3542cf8a69953f429d99b9a1eda0b510a Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 18 Apr 2026 15:47:13 +0200 Subject: [PATCH 1/3] lab end --- .../SpringBootRestapiApplication.java | 13 ++++ .../controller/CustomerController.java | 50 ++++++++++++++ .../controller/ProductController.java | 68 +++++++++++++++++++ .../exception/GlobalExceptionHandler.java | 29 ++++++++ .../springbootrestapi/model/Customer.java | 61 +++++++++++++++++ .../springbootrestapi/model/Product.java | 62 +++++++++++++++++ .../service/ProductService.java | 66 ++++++++++++++++++ src/main/resources/application.properties | 1 + src/main/resources/request.http | 14 ++++ .../SpringBootRestapiApplicationTests.java | 13 ++++ 10 files changed, 377 insertions(+) create mode 100644 src/main/java/com/workshop/springbootrestapi/SpringBootRestapiApplication.java create mode 100644 src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java create mode 100644 src/main/java/com/workshop/springbootrestapi/controller/ProductController.java create mode 100644 src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/workshop/springbootrestapi/model/Customer.java create mode 100644 src/main/java/com/workshop/springbootrestapi/model/Product.java create mode 100644 src/main/java/com/workshop/springbootrestapi/service/ProductService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/request.http create mode 100644 src/test/java/com/workshop/springbootrestapi/SpringBootRestapiApplicationTests.java diff --git a/src/main/java/com/workshop/springbootrestapi/SpringBootRestapiApplication.java b/src/main/java/com/workshop/springbootrestapi/SpringBootRestapiApplication.java new file mode 100644 index 0000000..02e18f8 --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/SpringBootRestapiApplication.java @@ -0,0 +1,13 @@ +package com.workshop.springbootrestapi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootRestapiApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootRestapiApplication.class, args); + } + +} diff --git a/src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java b/src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java new file mode 100644 index 0000000..9754546 --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java @@ -0,0 +1,50 @@ +package com.workshop.springbootrestapi.controller; + +import com.workshop.springbootrestapi.model.Customer; +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/customers") +public class CustomerController { + + private final List customers = new ArrayList<>(); + + @PostMapping + public Customer create(@Valid @RequestBody Customer customer) { + customers.add(customer); + return customer; + } + + @GetMapping + public List getAll() { + return customers; + } + + @GetMapping("/{email}") + public Customer getByEmail(@PathVariable String email) { + return customers.stream() + .filter(c -> c.getEmail().equalsIgnoreCase(email)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Customer not found")); + } + + @PutMapping("/{email}") + public Customer update(@PathVariable String email, @RequestBody Customer updated) { + Customer customer = getByEmail(email); + + customer.setName(updated.getName()); + customer.setAge(updated.getAge()); + customer.setAddress(updated.getAddress()); + + return customer; + } + + @DeleteMapping("/{email}") + public void delete(@PathVariable String email) { + customers.removeIf(c -> c.getEmail().equalsIgnoreCase(email)); + } +} diff --git a/src/main/java/com/workshop/springbootrestapi/controller/ProductController.java b/src/main/java/com/workshop/springbootrestapi/controller/ProductController.java new file mode 100644 index 0000000..3ef2d3b --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/controller/ProductController.java @@ -0,0 +1,68 @@ +package com.workshop.springbootrestapi.controller; + +import com.workshop.springbootrestapi.model.Product; +import com.workshop.springbootrestapi.service.ProductService; +import jakarta.validation.Valid; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/products") +public class ProductController { + + private final ProductService productService; + private static final String API_KEY = "123456"; + + public ProductController(ProductService productService) { + this.productService = productService; + } + + private void validateApiKey(String apiKey) { + if (!API_KEY.equals(apiKey)) { + throw new RuntimeException("Invalid API Key"); + } + } + + @PostMapping + public ResponseEntity createProduct( + @RequestHeader("API-Key") String apiKey, + @Valid @RequestBody Product product) { + + validateApiKey(apiKey); + return ResponseEntity.ok(productService.addProduct(product)); + } + + @GetMapping + public List getAll() { + return productService.getAllProducts(); + } + + @GetMapping("/{name}") + public Product getByName(@PathVariable String name) { + return productService.getProductByName(name); + } + + @PutMapping("/{name}") + public Product update(@PathVariable String name, @RequestBody Product product) { + return productService.updateProduct(name, product); + } + + @DeleteMapping("/{name}") + public void delete(@PathVariable String name) { + productService.deleteProduct(name); + } + + @GetMapping("/category/{category}") + public List getByCategory(@PathVariable String category) { + return productService.getProductsByCategory(category); + } + + @GetMapping("/price") + public List getByPriceRange( + @RequestParam double min, + @RequestParam double max) { + return productService.getProductsByPriceRange(min, max); + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java b/src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..d495cac --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java @@ -0,0 +1,29 @@ +package com.workshop.springbootrestapi.exception; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleValidationErrors(MethodArgumentNotValidException ex) { + + Map errors = new HashMap<>(); + + ex.getBindingResult().getFieldErrors() + .forEach(error -> + errors.put(error.getField(), error.getDefaultMessage())); + + return ResponseEntity.badRequest().body(errors); + } + + @ExceptionHandler(RuntimeException.class) + public ResponseEntity handleRuntime(RuntimeException ex) { + return ResponseEntity.status(404).body(ex.getMessage()); + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/model/Customer.java b/src/main/java/com/workshop/springbootrestapi/model/Customer.java new file mode 100644 index 0000000..b9db992 --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/model/Customer.java @@ -0,0 +1,61 @@ +package com.workshop.springbootrestapi.model; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; + +public class Customer { + + @NotBlank(message = "Name cannot be blank") + private String name; + + @Email(message = "Email must be valid") + private String email; + + @Min(value = 18, message = "Age must be at least 18") + private int age; + + @NotBlank(message = "Address cannot be blank") + private String address; + + public Customer() {} + + public Customer(String name, String email, int age, String address) { + this.name = name; + this.email = email; + this.age = age; + this.address = address; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public int getAge() { + return age; + } + + public String getAddress() { + return address; + } + + public void setName(String name) { + this.name = name; + } + + public void setEmail(String email) { + this.email = email; + } + + public void setAge(int age) { + this.age = age; + } + + public void setAddress(String address) { + this.address = address; + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/model/Product.java b/src/main/java/com/workshop/springbootrestapi/model/Product.java new file mode 100644 index 0000000..6fb3710 --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/model/Product.java @@ -0,0 +1,62 @@ +package com.workshop.springbootrestapi.model; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Positive; + +import java.math.BigDecimal; + +public class Product { + + @NotBlank(message = "Name cannot be blank") + private String name; + + @Positive(message = "Price must be positive") + private BigDecimal price; + + @NotBlank(message = "Category cannot be blank") + private String category; + + @Positive(message = "Quantity must be positive") + private int quantity; + + public Product() {} + + public Product(String name, BigDecimal price, String category, int quantity) { + this.name = name; + this.price = price; + this.category = category; + this.quantity = quantity; + } + + public String getName() { + return name; + } + + public BigDecimal getPrice() { + return price; + } + + public String getCategory() { + return category; + } + + public int getQuantity() { + return quantity; + } + + public void setName(String name) { + this.name = name; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public void setCategory(String category) { + this.category = category; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/service/ProductService.java b/src/main/java/com/workshop/springbootrestapi/service/ProductService.java new file mode 100644 index 0000000..56b929b --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/service/ProductService.java @@ -0,0 +1,66 @@ +package com.workshop.springbootrestapi.service; + +import com.workshop.springbootrestapi.model.Product; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class ProductService { + + private final List products = new ArrayList<>(List.of( + new Product("iPhone 15", new BigDecimal("999.99"), "Mobile", 10), + new Product("Samsung Galaxy S24", new BigDecimal("899.99"), "Mobile", 15), + new Product("MacBook Pro", new BigDecimal("1999.99"), "Laptop", 5), + new Product("iPad Air", new BigDecimal("649.99"), "Tablet", 8), + new Product("Sony WH-1000XM5", new BigDecimal("399.99"), "Audio", 20) + )); + + public Product addProduct(Product product) { + products.add(product); + return product; + } + + public List getAllProducts() { + return products; + } + + public Product getProductByName(String name) { + return products.stream() + .filter(p -> p.getName().equalsIgnoreCase(name)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Product not found")); + } + + public Product updateProduct(String name, Product updatedProduct) { + Product product = getProductByName(name); + + product.setName(updatedProduct.getName()); + product.setPrice(updatedProduct.getPrice()); + product.setCategory(updatedProduct.getCategory()); + product.setQuantity(updatedProduct.getQuantity()); + + return product; + } + + public void deleteProduct(String name) { + Product product = getProductByName(name); + products.remove(product); + } + + public List getProductsByCategory(String category) { + return products.stream() + .filter(p -> p.getCategory().equalsIgnoreCase(category)) + .collect(Collectors.toList()); + } + + public List getProductsByPriceRange(double min, double max) { + return products.stream() + .filter(p -> p.getPrice().compareTo(BigDecimal.valueOf(min)) >= 0 + && p.getPrice().compareTo(BigDecimal.valueOf(max)) <= 0) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..0ec7673 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=SpringBootRESTAPI diff --git a/src/main/resources/request.http b/src/main/resources/request.http new file mode 100644 index 0000000..cfaf6e9 --- /dev/null +++ b/src/main/resources/request.http @@ -0,0 +1,14 @@ +POST http://localhost:8080/customers +Content-Type: application/json + +{ + "name": "Laura", + "email": "laura@email.com", + "age": 25, + "address": "Montequinto" +} + +### +GET http://localhost:8080/customers + + diff --git a/src/test/java/com/workshop/springbootrestapi/SpringBootRestapiApplicationTests.java b/src/test/java/com/workshop/springbootrestapi/SpringBootRestapiApplicationTests.java new file mode 100644 index 0000000..714f69f --- /dev/null +++ b/src/test/java/com/workshop/springbootrestapi/SpringBootRestapiApplicationTests.java @@ -0,0 +1,13 @@ +package com.workshop.springbootrestapi; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringBootRestapiApplicationTests { + + @Test + void contextLoads() { + } + +} From 5af74ed422e68f9a9786a97d125239fb7985cf45 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 18 Apr 2026 16:10:08 +0200 Subject: [PATCH 2/3] lab end --- .../controller/ProductController.java | 33 +++++++++++--- .../exception/GlobalExceptionHandler.java | 26 ++++++++--- .../exception/ProductNotFoundException.java | 7 +++ .../springbootrestapi/model/Customer.java | 1 - .../springbootrestapi/model/Product.java | 3 ++ .../service/CustomerService.java | 44 +++++++++++++++++++ .../service/ProductService.java | 13 ++++-- 7 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/workshop/springbootrestapi/exception/ProductNotFoundException.java create mode 100644 src/main/java/com/workshop/springbootrestapi/service/CustomerService.java diff --git a/src/main/java/com/workshop/springbootrestapi/controller/ProductController.java b/src/main/java/com/workshop/springbootrestapi/controller/ProductController.java index 3ef2d3b..c6a1257 100644 --- a/src/main/java/com/workshop/springbootrestapi/controller/ProductController.java +++ b/src/main/java/com/workshop/springbootrestapi/controller/ProductController.java @@ -26,7 +26,7 @@ private void validateApiKey(String apiKey) { } @PostMapping - public ResponseEntity createProduct( + public ResponseEntity create( @RequestHeader("API-Key") String apiKey, @Valid @RequestBody Product product) { @@ -35,34 +35,55 @@ public ResponseEntity createProduct( } @GetMapping - public List getAll() { + public List getAll(@RequestHeader("API-Key") String apiKey) { + validateApiKey(apiKey); return productService.getAllProducts(); } @GetMapping("/{name}") - public Product getByName(@PathVariable String name) { + public Product getByName( + @RequestHeader("API-Key") String apiKey, + @PathVariable String name) { + + validateApiKey(apiKey); return productService.getProductByName(name); } @PutMapping("/{name}") - public Product update(@PathVariable String name, @RequestBody Product product) { + public Product update( + @RequestHeader("API-Key") String apiKey, + @PathVariable String name, + @RequestBody Product product) { + + validateApiKey(apiKey); return productService.updateProduct(name, product); } @DeleteMapping("/{name}") - public void delete(@PathVariable String name) { + public void delete( + @RequestHeader("API-Key") String apiKey, + @PathVariable String name) { + + validateApiKey(apiKey); productService.deleteProduct(name); } @GetMapping("/category/{category}") - public List getByCategory(@PathVariable String category) { + public List getByCategory( + @RequestHeader("API-Key") String apiKey, + @PathVariable String category) { + + validateApiKey(apiKey); return productService.getProductsByCategory(category); } @GetMapping("/price") public List getByPriceRange( + @RequestHeader("API-Key") String apiKey, @RequestParam double min, @RequestParam double max) { + + validateApiKey(apiKey); return productService.getProductsByPriceRange(min, max); } } \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java b/src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java index d495cac..08b8d79 100644 --- a/src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/workshop/springbootrestapi/exception/GlobalExceptionHandler.java @@ -1,7 +1,9 @@ package com.workshop.springbootrestapi.exception; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingRequestHeaderException; import org.springframework.web.bind.annotation.*; import java.util.HashMap; @@ -11,19 +13,31 @@ public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) - public ResponseEntity handleValidationErrors(MethodArgumentNotValidException ex) { + public ResponseEntity handleValidation(MethodArgumentNotValidException ex) { Map errors = new HashMap<>(); ex.getBindingResult().getFieldErrors() - .forEach(error -> - errors.put(error.getField(), error.getDefaultMessage())); + .forEach(err -> errors.put(err.getField(), err.getDefaultMessage())); return ResponseEntity.badRequest().body(errors); } - @ExceptionHandler(RuntimeException.class) - public ResponseEntity handleRuntime(RuntimeException ex) { - return ResponseEntity.status(404).body(ex.getMessage()); + @ExceptionHandler(MissingRequestHeaderException.class) + public ResponseEntity handleMissingHeader(MissingRequestHeaderException ex) { + return ResponseEntity.status(HttpStatus.FORBIDDEN) + .body("API Key is required"); + } + + @ExceptionHandler(ProductNotFoundException.class) + public ResponseEntity handleProductNotFound(ProductNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgument(IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); } } \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/exception/ProductNotFoundException.java b/src/main/java/com/workshop/springbootrestapi/exception/ProductNotFoundException.java new file mode 100644 index 0000000..8c51cb6 --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/exception/ProductNotFoundException.java @@ -0,0 +1,7 @@ +package com.workshop.springbootrestapi.exception; + +public class ProductNotFoundException extends RuntimeException { + public ProductNotFoundException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/model/Customer.java b/src/main/java/com/workshop/springbootrestapi/model/Customer.java index b9db992..4be889c 100644 --- a/src/main/java/com/workshop/springbootrestapi/model/Customer.java +++ b/src/main/java/com/workshop/springbootrestapi/model/Customer.java @@ -18,7 +18,6 @@ public class Customer { @NotBlank(message = "Address cannot be blank") private String address; - public Customer() {} public Customer(String name, String email, int age, String address) { this.name = name; diff --git a/src/main/java/com/workshop/springbootrestapi/model/Product.java b/src/main/java/com/workshop/springbootrestapi/model/Product.java index 6fb3710..347f1f2 100644 --- a/src/main/java/com/workshop/springbootrestapi/model/Product.java +++ b/src/main/java/com/workshop/springbootrestapi/model/Product.java @@ -2,12 +2,14 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Positive; +import jakarta.validation.constraints.Size; import java.math.BigDecimal; public class Product { @NotBlank(message = "Name cannot be blank") + @Size(min = 3, message = "Name must be at least 3 characters") private String name; @Positive(message = "Price must be positive") @@ -28,6 +30,7 @@ public Product(String name, BigDecimal price, String category, int quantity) { this.quantity = quantity; } + public String getName() { return name; } diff --git a/src/main/java/com/workshop/springbootrestapi/service/CustomerService.java b/src/main/java/com/workshop/springbootrestapi/service/CustomerService.java new file mode 100644 index 0000000..fb74b76 --- /dev/null +++ b/src/main/java/com/workshop/springbootrestapi/service/CustomerService.java @@ -0,0 +1,44 @@ +package com.workshop.springbootrestapi.service; + +import com.workshop.springbootrestapi.model.Customer; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class CustomerService { + + private final List customers = new ArrayList<>(); + + public Customer addCustomer(Customer customer) { + customers.add(customer); + return customer; + } + + public List getAllCustomers() { + return customers; + } + + public Customer getCustomerByEmail(String email) { + return customers.stream() + .filter(c -> c.getEmail().equalsIgnoreCase(email)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Customer not found: " + email)); + } + + public Customer updateCustomer(String email, Customer updatedCustomer) { + Customer customer = getCustomerByEmail(email); + + customer.setName(updatedCustomer.getName()); + customer.setAge(updatedCustomer.getAge()); + customer.setAddress(updatedCustomer.getAddress()); + + return customer; + } + + public void deleteCustomer(String email) { + Customer customer = getCustomerByEmail(email); + customers.remove(customer); + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/springbootrestapi/service/ProductService.java b/src/main/java/com/workshop/springbootrestapi/service/ProductService.java index 56b929b..b4b37b1 100644 --- a/src/main/java/com/workshop/springbootrestapi/service/ProductService.java +++ b/src/main/java/com/workshop/springbootrestapi/service/ProductService.java @@ -1,5 +1,6 @@ package com.workshop.springbootrestapi.service; +import com.workshop.springbootrestapi.exception.ProductNotFoundException; import com.workshop.springbootrestapi.model.Product; import org.springframework.stereotype.Service; @@ -13,10 +14,10 @@ public class ProductService { private final List products = new ArrayList<>(List.of( new Product("iPhone 15", new BigDecimal("999.99"), "Mobile", 10), - new Product("Samsung Galaxy S24", new BigDecimal("899.99"), "Mobile", 15), + new Product("Samsung S24", new BigDecimal("899.99"), "Mobile", 15), new Product("MacBook Pro", new BigDecimal("1999.99"), "Laptop", 5), new Product("iPad Air", new BigDecimal("649.99"), "Tablet", 8), - new Product("Sony WH-1000XM5", new BigDecimal("399.99"), "Audio", 20) + new Product("Sony Headphones", new BigDecimal("399.99"), "Audio", 20) )); public Product addProduct(Product product) { @@ -32,7 +33,8 @@ public Product getProductByName(String name) { return products.stream() .filter(p -> p.getName().equalsIgnoreCase(name)) .findFirst() - .orElseThrow(() -> new RuntimeException("Product not found")); + .orElseThrow(() -> + new ProductNotFoundException("Product not found: " + name)); } public Product updateProduct(String name, Product updatedProduct) { @@ -58,6 +60,11 @@ public List getProductsByCategory(String category) { } public List getProductsByPriceRange(double min, double max) { + + if (min > max) { + throw new IllegalArgumentException("Min price cannot be greater than max price"); + } + return products.stream() .filter(p -> p.getPrice().compareTo(BigDecimal.valueOf(min)) >= 0 && p.getPrice().compareTo(BigDecimal.valueOf(max)) <= 0) From 837f4b006a89c8c1d6e35608d6cc9ed61f7f6ef5 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 18 Apr 2026 16:14:48 +0200 Subject: [PATCH 3/3] lab end --- .../controller/CustomerController.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java b/src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java index 9754546..62a691d 100644 --- a/src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java +++ b/src/main/java/com/workshop/springbootrestapi/controller/CustomerController.java @@ -1,50 +1,50 @@ package com.workshop.springbootrestapi.controller; import com.workshop.springbootrestapi.model.Customer; +import com.workshop.springbootrestapi.service.CustomerService; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/customers") public class CustomerController { - private final List customers = new ArrayList<>(); + private final CustomerService customerService; + public CustomerController(CustomerService customerService) { + this.customerService = customerService; + } + + // CREATE @PostMapping public Customer create(@Valid @RequestBody Customer customer) { - customers.add(customer); - return customer; + return customerService.addCustomer(customer); } + // GET ALL @GetMapping public List getAll() { - return customers; + return customerService.getAllCustomers(); } + // GET BY EMAIL @GetMapping("/{email}") public Customer getByEmail(@PathVariable String email) { - return customers.stream() - .filter(c -> c.getEmail().equalsIgnoreCase(email)) - .findFirst() - .orElseThrow(() -> new RuntimeException("Customer not found")); + return customerService.getCustomerByEmail(email); } + // UPDATE @PutMapping("/{email}") - public Customer update(@PathVariable String email, @RequestBody Customer updated) { - Customer customer = getByEmail(email); - - customer.setName(updated.getName()); - customer.setAge(updated.getAge()); - customer.setAddress(updated.getAddress()); - - return customer; + public Customer update(@PathVariable String email, + @RequestBody Customer customer) { + return customerService.updateCustomer(email, customer); } + // DELETE @DeleteMapping("/{email}") public void delete(@PathVariable String email) { - customers.removeIf(c -> c.getEmail().equalsIgnoreCase(email)); + customerService.deleteCustomer(email); } -} +} \ No newline at end of file