From 70ed58757c5753e2daa6e5e7ac7f0ac1fd324033 Mon Sep 17 00:00:00 2001 From: cldcr Date: Mon, 4 May 2026 21:13:58 +0200 Subject: [PATCH] lab5 --- .idea/.gitignore | 10 +++ .idea/lab-java-springboot-rest-api.iml | 9 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .../java/com/example/hellolab5/Customer.java | 41 ++++++++++++ .../hellolab5/GlobalExceptionHandler.java | 35 ++++++++++ .../hellolab5/HelloLab5Application.java | 13 ++++ .../java/com/example/hellolab5/Product.java | 41 ++++++++++++ .../controller/CustomerController.java | 46 +++++++++++++ .../controller/ProductController.java | 67 +++++++++++++++++++ .../hellolab5/service/ProductService.java | 40 +++++++++++ src/main/resources/application.properties | 1 + .../hellolab5/HelloLab5ApplicationTests.java | 13 ++++ 14 files changed, 336 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-java-springboot-rest-api.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/com/example/hellolab5/Customer.java create mode 100644 src/main/java/com/example/hellolab5/GlobalExceptionHandler.java create mode 100644 src/main/java/com/example/hellolab5/HelloLab5Application.java create mode 100644 src/main/java/com/example/hellolab5/Product.java create mode 100644 src/main/java/com/example/hellolab5/controller/CustomerController.java create mode 100644 src/main/java/com/example/hellolab5/controller/ProductController.java create mode 100644 src/main/java/com/example/hellolab5/service/ProductService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/test/java/com/example/hellolab5/HelloLab5ApplicationTests.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/lab-java-springboot-rest-api.iml b/.idea/lab-java-springboot-rest-api.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/lab-java-springboot-rest-api.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3653b1f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..43ec782 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/com/example/hellolab5/Customer.java b/src/main/java/com/example/hellolab5/Customer.java new file mode 100644 index 0000000..2725dce --- /dev/null +++ b/src/main/java/com/example/hellolab5/Customer.java @@ -0,0 +1,41 @@ +package com.example.hellolab5; + +import jakarta.validation.constraints.*; + +public class Customer { + @NotBlank //not blank = non vuoto + private String name; + + @Email @NotBlank + private String email;//(valid email format) + + @Min(18) + private int age; + + @NotBlank + private String address; + + // Costruttore senza argomenti + public Customer() {} + + // Costruttore completo + public Customer(String name, String email, int age, String address) { + this.name = name; + this.email = email; + this.age = age; + this.address = address; + } + + // Getter e Setter + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public String getEmail() { return email; } + public void setEmail(String email) { this.email = email; } + + public int getAge() { return age; } + public void setAge(int age) { this.age = age; } + + public String getAddress() { return address; } + public void setAddress(String address) { this.address = address; } +} \ No newline at end of file diff --git a/src/main/java/com/example/hellolab5/GlobalExceptionHandler.java b/src/main/java/com/example/hellolab5/GlobalExceptionHandler.java new file mode 100644 index 0000000..199e1e5 --- /dev/null +++ b/src/main/java/com/example/hellolab5/GlobalExceptionHandler.java @@ -0,0 +1,35 @@ +package com.example.hellolab5; + +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.*; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity> handleValidation(MethodArgumentNotValidException ex) { + Map errors = new HashMap<>(); + ex.getBindingResult().getFieldErrors().forEach(e -> errors.put(e.getField(), e.getDefaultMessage())); + return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(MissingRequestHeaderException.class) + public ResponseEntity handleMissingHeader(MissingRequestHeaderException ex) { + return new ResponseEntity<>("Missing API-Key header", HttpStatus.UNAUTHORIZED); + } + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleInvalidPrice(IllegalArgumentException ex) { + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(RuntimeException.class) + public ResponseEntity handleRuntime(RuntimeException ex) { + HttpStatus status = ex.getMessage().contains("not found") ? HttpStatus.NOT_FOUND : HttpStatus.UNAUTHORIZED; + return new ResponseEntity<>(ex.getMessage(), status); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/hellolab5/HelloLab5Application.java b/src/main/java/com/example/hellolab5/HelloLab5Application.java new file mode 100644 index 0000000..0ab35d5 --- /dev/null +++ b/src/main/java/com/example/hellolab5/HelloLab5Application.java @@ -0,0 +1,13 @@ +package com.example.hellolab5; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HelloLab5Application { + + public static void main(String[] args) { + SpringApplication.run(HelloLab5Application.class, args); + } + +} diff --git a/src/main/java/com/example/hellolab5/Product.java b/src/main/java/com/example/hellolab5/Product.java new file mode 100644 index 0000000..4a84e7f --- /dev/null +++ b/src/main/java/com/example/hellolab5/Product.java @@ -0,0 +1,41 @@ +package com.example.hellolab5; + +import jakarta.validation.constraints.*; + +public class Product { + @NotBlank @Size(min = 3) + private String name; + + @Positive + private double price; + + @NotBlank + private String category; + + @Positive + private int quantity; + + // Costruttore senza argomenti + public Product() {} + + // Costruttore completo + public Product(String name, double price, String category, int quantity) { + this.name = name; + this.price = price; + this.category = category; + this.quantity = quantity; + } + + // Getter e Setter + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public double getPrice() { return price; } + public void setPrice(double price) { this.price = price; } + + public String getCategory() { return category; } + public void setCategory(String category) { this.category = category; } + + public int getQuantity() { return quantity; } + public void setQuantity(int quantity) { this.quantity = quantity; } +} \ No newline at end of file diff --git a/src/main/java/com/example/hellolab5/controller/CustomerController.java b/src/main/java/com/example/hellolab5/controller/CustomerController.java new file mode 100644 index 0000000..b94fe1f --- /dev/null +++ b/src/main/java/com/example/hellolab5/controller/CustomerController.java @@ -0,0 +1,46 @@ +package com.example.hellolab5.controller; + +import com.example.hellolab5.Customer; +import jakarta.validation.Valid; +import org.springframework.http.ResponseEntity; +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 ResponseEntity create(@Valid @RequestBody Customer c) { + customers.add(c); + return ResponseEntity.ok("Customer created"); + } + + @GetMapping + public List getAll() { return customers; } + + @GetMapping("/{email}") + public Customer getByEmail(@PathVariable String email) { + return customers.stream().filter(c -> c.getEmail().equals(email)).findFirst() + .orElseThrow(() -> new RuntimeException("Customer not found")); + } + + @PutMapping("/{email}") + public void update(@PathVariable String email, @Valid @RequestBody Customer updated) { + getByEmail(email).setAddress(updated.getAddress()); + // Altri campi... + } + + @DeleteMapping("/{email}") + public void delete(@PathVariable String email) { + customers.removeIf(c -> c.getEmail().equals(email)); + } + + @GetMapping("/") + public String welcome() { + return "API is running! Use /products or /customers endpoints."; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/hellolab5/controller/ProductController.java b/src/main/java/com/example/hellolab5/controller/ProductController.java new file mode 100644 index 0000000..5386e75 --- /dev/null +++ b/src/main/java/com/example/hellolab5/controller/ProductController.java @@ -0,0 +1,67 @@ +package com.example.hellolab5.controller; + +import com.example.hellolab5.Product; +import com.example.hellolab5.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 service; + + public ProductController(ProductService service) { + this.service = service; + } + + @PostMapping + public ResponseEntity create(@RequestHeader("API-Key") String key, @Valid @RequestBody Product p) { + validateKey(key); + service.add(p); + return ResponseEntity.ok("Product created"); + } + + @GetMapping + public List getAll(@RequestHeader("API-Key") String key) { + validateKey(key); + return service.getAll(); + } + + @GetMapping("/{name}") + public Product getByName(@RequestHeader("API-Key") String key, @PathVariable String name) { + validateKey(key); + return service.getByName(name).orElseThrow(() -> new RuntimeException("Product not found")); + } + + @PutMapping("/{name}") + public ResponseEntity update(@RequestHeader("API-Key") String key, @PathVariable String name, @Valid @RequestBody Product p) { + validateKey(key); + service.update(name, p); + return ResponseEntity.ok("Product updated"); + } + + @DeleteMapping("/{name}") + public ResponseEntity delete(@RequestHeader("API-Key") String key, @PathVariable String name) { + validateKey(key); + service.delete(name); + return ResponseEntity.ok("Product deleted"); + } + + @GetMapping("/category/{category}") + public List getByCategory(@RequestHeader("API-Key") String key, @PathVariable String category) { + validateKey(key); + return service.getByCategory(category); + } + + @GetMapping("/price") + public List getByPrice(@RequestHeader("API-Key") String key, @RequestParam double min, @RequestParam double max) { + validateKey(key); + return service.getByPriceRange(min, max); + } + + private void validateKey(String key) { + if (!"123456".equals(key)) throw new RuntimeException("Invalid API-Key"); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/hellolab5/service/ProductService.java b/src/main/java/com/example/hellolab5/service/ProductService.java new file mode 100644 index 0000000..f582de6 --- /dev/null +++ b/src/main/java/com/example/hellolab5/service/ProductService.java @@ -0,0 +1,40 @@ +package com.example.hellolab5.service; + +import com.example.hellolab5.Product; +import org.springframework.stereotype.Service; +import java.util.*; +import java.util.stream.Collectors; + +@Service +public class ProductService { + private final List products = new ArrayList<>(); + + public void add(Product p) { products.add(p); } + + public List getAll() { return products; } + + public Optional getByName(String name) { + return products.stream().filter(p -> p.getName().equalsIgnoreCase(name)).findFirst(); + } + + public void update(String name, Product updated) { + getByName(name).ifPresent(p -> { + p.setPrice(updated.getPrice()); + p.setCategory(updated.getCategory()); + p.setQuantity(updated.getQuantity()); + }); + } + + public void delete(String name) { + products.removeIf(p -> p.getName().equalsIgnoreCase(name)); + } + + public List getByCategory(String category) { + return products.stream().filter(p -> p.getCategory().equalsIgnoreCase(category)).collect(Collectors.toList()); + } + + public List getByPriceRange(double min, double max) { + if (min > max) throw new IllegalArgumentException("Min range cannot be greater than max"); + return products.stream().filter(p -> p.getPrice() >= min && p.getPrice() <= max).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..21d7fa1 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=HelloLab5 diff --git a/src/test/java/com/example/hellolab5/HelloLab5ApplicationTests.java b/src/test/java/com/example/hellolab5/HelloLab5ApplicationTests.java new file mode 100644 index 0000000..84116e1 --- /dev/null +++ b/src/test/java/com/example/hellolab5/HelloLab5ApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.hellolab5; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class HelloLab5ApplicationTests { + + @Test + void contextLoads() { + } + +}