From cbedb2774ec1a3cc72ef7ef9e39c97e678a33445 Mon Sep 17 00:00:00 2001 From: Frida Anselin Date: Mon, 20 Jan 2025 13:44:31 +0100 Subject: [PATCH 1/2] Complete core --- src/main/java/com/booleanuk/api/Main.java | 11 ++++ .../com/booleanuk/api/products/Product.java | 49 ++++++++++++++++++ .../api/products/ProductController.java | 43 ++++++++++++++++ .../api/products/ProductRepository.java | 50 +++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 src/main/java/com/booleanuk/api/Main.java create mode 100644 src/main/java/com/booleanuk/api/products/Product.java create mode 100644 src/main/java/com/booleanuk/api/products/ProductController.java create mode 100644 src/main/java/com/booleanuk/api/products/ProductRepository.java 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..ade8414 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/Product.java @@ -0,0 +1,49 @@ +package com.booleanuk.api.products; + +public class Product { + private static int nextId = 0; + private int id; + private String name; + private String category; + private int price; + + public Product(String name, String category, int price) { + this.id = nextId; + nextId++; + this.name = name; + this.category = category; + this.price = price; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = 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 int getPrice() { + return price; + } + + public void setPrice(int 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..b09a8fb --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductController.java @@ -0,0 +1,43 @@ +package com.booleanuk.api.products; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository theProducts; + + public ProductController() { + this.theProducts = new ProductRepository(); + } + + @GetMapping + public List getAllProducts() { + return this.theProducts.getAll(); + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product createProduct(@RequestBody Product product) { + return this.theProducts.create(product); + } + + @GetMapping("/{id}") + public Product getAProduct(@PathVariable int id) { + return this.theProducts.getOne(id); + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product updateAproduct(@PathVariable int id, @RequestBody Product product) { + return this.theProducts.update(id, product); + } + + @DeleteMapping("/{id}") + public Product deleteProduct(@PathVariable int id) { + return this.theProducts.delete(id); + } +} 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..0be1d55 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -0,0 +1,50 @@ +package com.booleanuk.api.products; + +import java.util.ArrayList; +import java.util.List; + +public class ProductRepository { + private List products; + + public ProductRepository() { + this.products = new ArrayList<>(); + + this.products.add(new Product("Bagel", "Food", 10)); + this.products.add(new Product("Tea", "Food", 12)); + this.products.add(new Product("Harry Potter", "Book", 100)); + } + + public List getAll() { + return this.products; + } + + public Product create(Product product) { + this.products.add(product); + return product; + } + + public Product getOne(int id) { + for (Product product : this.products) { + if (id == product.getId()) { + return product; + } + } + return null; + } + + public Product update(int id, Product product) { + for (Product pToUpdate : products) { + if (id == pToUpdate.getId()) { + this.products.get(id).setName(product.getName()); + this.products.get(id).setCategory(product.getCategory()); + this.products.get(id).setPrice(product.getPrice()); + return this.products.get(id); + } + } + return null; + } + + public Product delete(int id) { + return this.products.remove(id); + } +} From 81089472b164936c0fe47d264dc5864f6023e94e Mon Sep 17 00:00:00 2001 From: Frida Anselin Date: Mon, 20 Jan 2025 16:43:38 +0100 Subject: [PATCH 2/2] Complete extension --- .../api/products/ProductController.java | 37 ++++++++++++++++--- .../api/products/ProductRepository.java | 33 +++++++++++++++-- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/booleanuk/api/products/ProductController.java b/src/main/java/com/booleanuk/api/products/ProductController.java index b09a8fb..cc04f69 100644 --- a/src/main/java/com/booleanuk/api/products/ProductController.java +++ b/src/main/java/com/booleanuk/api/products/ProductController.java @@ -2,6 +2,7 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; import java.util.List; @@ -15,29 +16,55 @@ public ProductController() { } @GetMapping - public List getAllProducts() { - return this.theProducts.getAll(); + public List getAllProducts(@RequestParam(name="category", required = false) String category) { + if (category == null) { + return this.theProducts.getAll(); + } + + List pCategory = this.theProducts.getProductsInCategory(category); + if (pCategory == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found."); + } + return pCategory; } @PostMapping @ResponseStatus(HttpStatus.CREATED) public Product createProduct(@RequestBody Product product) { + if (this.theProducts.isNameInList(product)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists"); + } return this.theProducts.create(product); } @GetMapping("/{id}") public Product getAProduct(@PathVariable int id) { - return this.theProducts.getOne(id); + Product product = this.theProducts.getOne(id); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + return product; } @PutMapping("/{id}") @ResponseStatus(HttpStatus.CREATED) public Product updateAproduct(@PathVariable int id, @RequestBody Product product) { - return this.theProducts.update(id, product); + if (theProducts.isNameInList(product)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists"); + } + Product pToUpdate = this.theProducts.update(id, product); + if (pToUpdate == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + return product; } @DeleteMapping("/{id}") public Product deleteProduct(@PathVariable int id) { - return this.theProducts.delete(id); + Product product = this.theProducts.delete(id); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + return product; } } diff --git a/src/main/java/com/booleanuk/api/products/ProductRepository.java b/src/main/java/com/booleanuk/api/products/ProductRepository.java index 0be1d55..5230149 100644 --- a/src/main/java/com/booleanuk/api/products/ProductRepository.java +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -14,6 +14,19 @@ public ProductRepository() { this.products.add(new Product("Harry Potter", "Book", 100)); } + public List getProductsInCategory(String category) { + List pCategory = new ArrayList<>(); + for (Product product : this.products) { + if (product.getCategory().toLowerCase().equals(category)) { + pCategory.add(product); + } + } + if (!pCategory.isEmpty()) { + return pCategory; + } + return null; + } + public List getAll() { return this.products; } @@ -33,18 +46,30 @@ public Product getOne(int id) { } public Product update(int id, Product product) { - for (Product pToUpdate : products) { - if (id == pToUpdate.getId()) { + Product pToUpdate = getOne(id); + if (pToUpdate != null) { this.products.get(id).setName(product.getName()); this.products.get(id).setCategory(product.getCategory()); this.products.get(id).setPrice(product.getPrice()); return this.products.get(id); } - } return null; } public Product delete(int id) { - return this.products.remove(id); + Product pToDelete = getOne(id); + if (pToDelete != null) { + return this.products.remove(id); + } + return null; + } + + public boolean isNameInList(Product product) { + for (Product pToFind : this.products) { + if (pToFind.getName().equals(product.getName())) { + return true; + } + } + return false; } }