From 7695c697d75dfe0205756da385fedaf6bfeb69b8 Mon Sep 17 00:00:00 2001 From: Mattias Hedbom Date: Mon, 20 Jan 2025 11:50:51 +0100 Subject: [PATCH 1/2] Core exercise completed --- src/main/java/com/booleanuk/api/Main.java | 10 ++++ .../java/com/booleanuk/api/bagels/Bagel.java | 25 -------- .../booleanuk/api/bagels/BagelController.java | 15 ----- .../booleanuk/api/bagels/BagelRepository.java | 25 -------- .../com/booleanuk/api/products/Product.java | 47 +++++++++++++++ .../api/products/ProductController.java | 43 ++++++++++++++ .../api/products/ProductRepository.java | 59 +++++++++++++++++++ 7 files changed, 159 insertions(+), 65 deletions(-) create mode 100644 src/main/java/com/booleanuk/api/Main.java delete mode 100644 src/main/java/com/booleanuk/api/bagels/Bagel.java delete mode 100644 src/main/java/com/booleanuk/api/bagels/BagelController.java delete mode 100644 src/main/java/com/booleanuk/api/bagels/BagelRepository.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..dec70f5 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -0,0 +1,10 @@ +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/bagels/Bagel.java b/src/main/java/com/booleanuk/api/bagels/Bagel.java deleted file mode 100644 index 77a24a6..0000000 --- a/src/main/java/com/booleanuk/api/bagels/Bagel.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.booleanuk.api.bagels; - -public class Bagel { - private int id; - private String type; - private int price; - - public Bagel(int id, String type, int price) { - this.id = id; - this.type = type; - this.price = price; - } - - public int getId() { - return id; - } - - public String getType() { - return type; - } - - public int getPrice() { - return price; - } -} diff --git a/src/main/java/com/booleanuk/api/bagels/BagelController.java b/src/main/java/com/booleanuk/api/bagels/BagelController.java deleted file mode 100644 index cce2764..0000000 --- a/src/main/java/com/booleanuk/api/bagels/BagelController.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.booleanuk.api.bagels; - -import java.util.List; - -public class BagelController { - BagelRepository repository; - - public BagelController(BagelRepository repository) { - this.repository = repository; - } - - public List getAll() { - return this.repository.findAll(); - } -} diff --git a/src/main/java/com/booleanuk/api/bagels/BagelRepository.java b/src/main/java/com/booleanuk/api/bagels/BagelRepository.java deleted file mode 100644 index 320ddba..0000000 --- a/src/main/java/com/booleanuk/api/bagels/BagelRepository.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.booleanuk.api.bagels; - -import java.util.ArrayList; -import java.util.List; - -public class BagelRepository { - private int idCounter = 1; - private List data = new ArrayList<>(); - - public void create(String type, int price) { - Bagel bagel = new Bagel(this.idCounter++, type, price); - this.data.add(bagel); - } - - public List findAll() { - return this.data; - } - - public Bagel find(int id) { - return this.data.stream() - .filter(bagel -> bagel.getId() == id) - .findFirst() - .orElseThrow(); - } -} 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..9919dff --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/Product.java @@ -0,0 +1,47 @@ +package com.booleanuk.api.products; + +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 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 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..c4adf18 --- /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 getProductById(@PathVariable int id){ + return this.theProducts.getProductById(id); + } + + @PutMapping("{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product updateProductById(@PathVariable int id, @RequestBody Product product){ + return this.theProducts.updateProductById(id, product); + } + + @DeleteMapping("{id}") + public Product deleteProductById(@PathVariable int id){ + return this.theProducts.deleteProductById(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..2c8ef67 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -0,0 +1,59 @@ +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<>(){{ + add(new Product("Harry Potter and the goblet of fire", "Book", 10)); + add(new Product("Antilop", "Chair", 59)); + add(new Product("Hobbit", "Book", 15)); + }}; + } + + public List getAll(){ + return this.products; + } + + public Product create(Product newProduct){ + this.products.add(newProduct); + return newProduct; + } + + public Product getProductById(int id){ + Product product = null; + for(Product aProduct : this.products){ + if(id == aProduct.getId()){ + product = aProduct; + return product; + } + } + return product; + } + + public Product updateProductById(int id, Product productToUpdate){ + for(Product aProduct : this.products){ + if(aProduct.getId() == id){ + aProduct.setName(productToUpdate.getName()); + aProduct.setCategory(productToUpdate.getCategory()); + aProduct.setPrice(productToUpdate.getPrice()); + + return aProduct; + } + } + return null; + } + + public Product deleteProductById(int id){ + for(Product aProduct : this.products){ + if(aProduct.getId() == id){ + this.products.remove(aProduct); + return aProduct; + } + } + return null; + } +} From f77ac93fca12e4c2087bc48c2d1c7ec73fb7a13c Mon Sep 17 00:00:00 2001 From: Mattias Hedbom Date: Mon, 20 Jan 2025 14:46:53 +0100 Subject: [PATCH 2/2] Extension exercise completed. --- .../api/products/ProductController.java | 44 +++++++++++++++++-- .../api/products/ProductRepository.java | 25 +++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/booleanuk/api/products/ProductController.java b/src/main/java/com/booleanuk/api/products/ProductController.java index c4adf18..d3ca0c2 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; @@ -14,30 +15,67 @@ public ProductController(){ this.theProducts = new ProductRepository(); } + @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 listByCategory = this.theProducts.getAllProductsByCategory(category); + if(listByCategory.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There are no products that have the entered category!"); + } + return listByCategory; } + @PostMapping @ResponseStatus(HttpStatus.CREATED) public Product createProduct(@RequestBody Product product){ + Product productToCreate = this.theProducts.isNameAlreadyContainedInList(product); + if(productToCreate == null){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with the same name already exists in the list!"); + } + return this.theProducts.create(product); } + @GetMapping("{id}") public Product getProductById(@PathVariable int id){ - return this.theProducts.getProductById(id); + Product productToGet = this.theProducts.getProductById(id); + if(productToGet == null){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There is no product with the entered ID!"); + } + return productToGet; } + @PutMapping("{id}") @ResponseStatus(HttpStatus.CREATED) public Product updateProductById(@PathVariable int id, @RequestBody Product product){ + Product aProduct = this.theProducts.getProductById(id); + if(aProduct == null){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There is no product with the entered ID!"); + } + + aProduct = this.theProducts.isNameAlreadyContainedInList(id, product); + if(aProduct == null){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with the same name already exists in the list!"); + } + return this.theProducts.updateProductById(id, product); } + @DeleteMapping("{id}") public Product deleteProductById(@PathVariable int id){ + Product productToDelete = this.theProducts.getProductById(id); + if(productToDelete == null){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There is no product with the entered ID!"); + } + return this.theProducts.deleteProductById(id); } } diff --git a/src/main/java/com/booleanuk/api/products/ProductRepository.java b/src/main/java/com/booleanuk/api/products/ProductRepository.java index 2c8ef67..5e8ac62 100644 --- a/src/main/java/com/booleanuk/api/products/ProductRepository.java +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -56,4 +56,29 @@ public Product deleteProductById(int id){ } return null; } + + public Product isNameAlreadyContainedInList(Product productToCheck){ + for(Product aProduct : this.products){ + if(aProduct.getName().equals(productToCheck.getName())){ + productToCheck = null; + return productToCheck; + } + } + return productToCheck; + } + + public Product isNameAlreadyContainedInList(int id, Product productToCheck){ + for(Product aProduct : this.products){ + if(aProduct.getName().equals(productToCheck.getName()) && aProduct.getId() != id){ + productToCheck = null; + return productToCheck; + } + } + return productToCheck; + } + + public List getAllProductsByCategory(String category){ + return this.products.stream() + .filter(s -> s.getCategory().equals(category)).toList(); + } }