From f3c5687060de0f64c2d3b02035fa23fe32b6c1f3 Mon Sep 17 00:00:00 2001 From: Jonas Finborud Nyman Date: Mon, 20 Jan 2025 12:01:22 +0100 Subject: [PATCH 1/2] Core Completed --- .../booleanuk/api/bagels/product/Main.java | 11 ++++ .../booleanuk/api/bagels/product/Product.java | 49 ++++++++++++++++++ .../api/bagels/product/ProductController.java | 40 +++++++++++++++ .../api/bagels/product/ProductRepository.java | 50 +++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 src/main/java/com/booleanuk/api/bagels/product/Main.java create mode 100644 src/main/java/com/booleanuk/api/bagels/product/Product.java create mode 100644 src/main/java/com/booleanuk/api/bagels/product/ProductController.java create mode 100644 src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java diff --git a/src/main/java/com/booleanuk/api/bagels/product/Main.java b/src/main/java/com/booleanuk/api/bagels/product/Main.java new file mode 100644 index 0000000..33255d5 --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/product/Main.java @@ -0,0 +1,11 @@ +package com.booleanuk.api.bagels.product; + +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/bagels/product/Product.java b/src/main/java/com/booleanuk/api/bagels/product/Product.java new file mode 100644 index 0000000..6a0586f --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/product/Product.java @@ -0,0 +1,49 @@ +package com.booleanuk.api.bagels.product; + +public class Product { + private static int nextId = 1; + 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 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; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/src/main/java/com/booleanuk/api/bagels/product/ProductController.java b/src/main/java/com/booleanuk/api/bagels/product/ProductController.java new file mode 100644 index 0000000..595524c --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/product/ProductController.java @@ -0,0 +1,40 @@ +package com.booleanuk.api.bagels.product; + +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository theProducts; + + public ProductController() { + this.theProducts = new ProductRepository(); + } + + @GetMapping + public ArrayList getAllProducts() { + return this.theProducts.getAll(); + } + + @GetMapping("{id}") + public Product getOneProduct(@PathVariable int id) { + return this.theProducts.getOne(id); + } + + @PostMapping + public Product createProduct(@RequestBody Product product) { + return this.theProducts.create(product); + } + + @PutMapping("{id}") + public Product updateProduct(@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/bagels/product/ProductRepository.java b/src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java new file mode 100644 index 0000000..46ed3f9 --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java @@ -0,0 +1,50 @@ +package com.booleanuk.api.bagels.product; + +import java.util.ArrayList; + +public class ProductRepository { + private ArrayList products; + + public ProductRepository() { + this.products = new ArrayList<>(); + } + + 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 product) { + for(int i = 0; i < this.products.size(); i++) { + if(this.products.get(i).getId() == id) { + this.products.get(i).setName(product.getName()); + this.products.get(i).setCategory(product.getCategory()); + this.products.get(i).setPrice(product.getPrice()); + return this.products.get(i); + } + } + return null; + } + + public Product delete(int id) { + for(int i = 0; i < this.products.size(); i++) { + if(this.products.get(i).getId() == id) { + return this.products.remove(i); + } + } + return null; + } +} From 73246327b676bc3692ef09f0dc7f9e4818a8a471 Mon Sep 17 00:00:00 2001 From: Jonas Finborud Nyman Date: Mon, 20 Jan 2025 14:57:27 +0100 Subject: [PATCH 2/2] Extension Completed --- .../api/bagels/product/ProductController.java | 40 ++++++++++++--- .../api/bagels/product/ProductRepository.java | 51 ++++++++++++++----- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/booleanuk/api/bagels/product/ProductController.java b/src/main/java/com/booleanuk/api/bagels/product/ProductController.java index 595524c..b4c9200 100644 --- a/src/main/java/com/booleanuk/api/bagels/product/ProductController.java +++ b/src/main/java/com/booleanuk/api/bagels/product/ProductController.java @@ -1,6 +1,8 @@ package com.booleanuk.api.bagels.product; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; import java.util.ArrayList; @@ -14,27 +16,53 @@ public ProductController() { } @GetMapping - public ArrayList getAllProducts() { - return this.theProducts.getAll(); + @ResponseBody + public ArrayList getAllProducts(@RequestParam(required = false) String category) { + ArrayList newList = this.theProducts.getAll(category); + if(newList.isEmpty() && category != null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found."); + } + return newList; } @GetMapping("{id}") public Product getOneProduct(@PathVariable int id) { - return this.theProducts.getOne(id); + Product theProduct = this.theProducts.getOne(id); + if(theProduct == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); + } + return theProduct; } @PostMapping public Product createProduct(@RequestBody Product product) { - return this.theProducts.create(product); + Product newProduct = this.theProducts.create(product); + if(newProduct == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); + } + return newProduct; } @PutMapping("{id}") public Product updateProduct(@PathVariable int id, @RequestBody Product product) { - return this.theProducts.update(id, product); + + if(!this.theProducts.checkIfNameIsUnique(product)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists"); + } + + Product theProduct = this.theProducts.update(id, product); + if(theProduct == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); + } + return theProduct; } @DeleteMapping("{id}") public Product deleteProduct(@PathVariable int id) { - return this.theProducts.delete(id); + Product theProduct = this.theProducts.delete(id); + if(theProduct == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + return theProduct; } } diff --git a/src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java b/src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java index 46ed3f9..f578494 100644 --- a/src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java +++ b/src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java @@ -9,8 +9,17 @@ public ProductRepository() { this.products = new ArrayList<>(); } - public ArrayList getAll() { - return this.products; + public ArrayList getAll(String category) { + if(category == null) { + return this.products; + } + ArrayList newList = new ArrayList<>(); + for(Product p : this.products) { + if(p.getCategory().equalsIgnoreCase(category)) { + newList.add(p); + } + } + return newList; } public Product getOne(int id) { @@ -23,28 +32,42 @@ public Product getOne(int id) { } public Product create(Product product) { + for(Product p : this.products) { + if(p.getName().equalsIgnoreCase(product.getName())) { + return null; + } + } this.products.add(product); return product; } public Product update(int id, Product product) { - for(int i = 0; i < this.products.size(); i++) { - if(this.products.get(i).getId() == id) { - this.products.get(i).setName(product.getName()); - this.products.get(i).setCategory(product.getCategory()); - this.products.get(i).setPrice(product.getPrice()); - return this.products.get(i); + + Product theProduct = this.getOne(id); + if(theProduct == null) { + return theProduct; + } + theProduct.setName(product.getName()); + theProduct.setCategory(product.getCategory()); + theProduct.setPrice(product.getPrice()); + return theProduct; + } + + public boolean checkIfNameIsUnique(Product product) { + for(Product p : this.products) { + if(p.getName().equalsIgnoreCase(product.getName())) { + return false; } } - return null; + return true; } public Product delete(int id) { - for(int i = 0; i < this.products.size(); i++) { - if(this.products.get(i).getId() == id) { - return this.products.remove(i); - } + Product theProduct = this.getOne(id); + if(theProduct == null) { + return theProduct; } - return null; + this.products.remove(theProduct); + return theProduct; } }