diff --git a/src/main/java/com/booleanuk/api/Main.java b/src/main/java/com/booleanuk/api/Main.java new file mode 100644 index 0000000..8e749e0 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -0,0 +1,11 @@ +package com.booleanuk.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } +} diff --git a/src/main/java/com/booleanuk/api/products/Product.java b/src/main/java/com/booleanuk/api/products/Product.java new file mode 100644 index 0000000..5910853 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/Product.java @@ -0,0 +1,28 @@ +package com.booleanuk.api.products; + +public class Product { + private static int nextId = 1; + + private int id; + private String name; + private String category; + private double price; + + public Product(String name, String category, double price) { + this.id = nextId++; + this.name = name; + this.category = category; + this.price = price; + } + + public int getId() { return id; } + + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public String getCategory() { return category; } + public void setCategory(String category) { this.category = category; } + + public double getPrice() { return price; } + public void setPrice(double price) { this.price = price; } +} diff --git a/src/main/java/com/booleanuk/api/products/ProductController.java b/src/main/java/com/booleanuk/api/products/ProductController.java new file mode 100644 index 0000000..38b9a27 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductController.java @@ -0,0 +1,44 @@ +package com.booleanuk.api.products; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; + +@RestController +@RequestMapping("/products") +public class ProductController { + private ProductRepository productRepository; + + public ProductController() { + this.productRepository = new ProductRepository(); + } + + @GetMapping + public ArrayList getAll() { + return this.productRepository.getAll(); + } + + @PostMapping("/auto") + public ResponseEntity createAutoProduct() { + Product autoProduct = new Product("Gaming Chair", "Furniture", 199.99); + Product savedProduct = productRepository.addProduct(autoProduct); + return ResponseEntity.status(HttpStatus.CREATED).body(savedProduct); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteProduct(@PathVariable int id) { + boolean deleted = productRepository.delete(id); + if (deleted) { + return ResponseEntity.status(HttpStatus.OK) + .body("Product with ID " + id + " was deleted successfully."); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body("Product with ID " + id + " not found."); + } + } + + + +} diff --git a/src/main/java/com/booleanuk/api/products/ProductRepository.java b/src/main/java/com/booleanuk/api/products/ProductRepository.java new file mode 100644 index 0000000..b7edc78 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -0,0 +1,58 @@ +package com.booleanuk.api.products; + +import java.util.ArrayList; + +public class ProductRepository { + private ArrayList products; + + public ProductRepository() { + this.products = new ArrayList<>(); + + this.products.add(new Product("Laptop", "Electronics", 899.99)); + this.products.add(new Product("T-shirt", "Clothing", 19.99)); + this.products.add(new Product("Coffee Mug", "Kitchen", 5.49)); + } + + public ArrayList getAll() { + return this.products; + } + + public Product getOne(int id) { + for (Product product : this.products) { + if (product.getId() == id) { + return product; + } + } + return null; + } + + public Product create(Product product) { + this.products.add(product); + return product; + } + + public Product update(int id, Product updatedProduct) { + Product existingProduct = getOne(id); + if (existingProduct == null) { + return null; + } + existingProduct.setName(updatedProduct.getName()); + existingProduct.setCategory(updatedProduct.getCategory()); + existingProduct.setPrice(updatedProduct.getPrice()); + return existingProduct; + } + + public boolean delete(int id) { + Product existingProduct = getOne(id); + if (existingProduct == null) { + return false; + } + this.products.remove(existingProduct); + return true; + } + + public Product addProduct(Product product) { + this.products.add(product); + return product; + } +}