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..387287d --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/Product.java @@ -0,0 +1,45 @@ +package com.booleanuk.api.products; + +public class Product { + private int id; + private String name; + private String category; + private int price; + private static int nextId = 1; + + public Product(String name, String category, int price) { + this.name = name; + this.category = category; + this.price = price; + this.id = nextId; + nextId++; + } + + 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..c12cb5a --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductController.java @@ -0,0 +1,72 @@ +package com.booleanuk.api.products; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.nio.channels.ScatteringByteChannel; +import java.util.List; +import java.util.NoSuchElementException; + +@RestController +@RequestMapping("products") + +public class ProductController { + private ProductRepository theProducts; + + public ProductController() { + this.theProducts = new ProductRepository(); + } + + @GetMapping + public List getAll(@RequestParam(required = false) String category) { + if (this.theProducts.getAll(category) == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found!"); + } + return this.theProducts.getAll(category); + } + + @PostMapping + public Product create(@RequestBody Product product) { + Product toReturn = this.theProducts.createProduct(product); + if (toReturn == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Already exists!"); + } + return toReturn; + } + + //try-catch not needed at all but I just wanted to try it + @GetMapping("/{id}") + public Product getOne(@PathVariable(name = "id") int id) { + try { + return this.theProducts.getOneProduct(id); + } catch (NoSuchElementException e) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); + } + } + + @PutMapping("/{id}") + public Product update(@PathVariable(name = "id") int id, @RequestBody Product product) { + try { + Product toReturn = this.theProducts.updateProduct(id, product); + if (toReturn == null) { + throw new IllegalArgumentException("Item already exists"); + } + return toReturn; + } catch (NoSuchElementException e) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); + } catch (IllegalArgumentException e) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); + } + } + + + @DeleteMapping("/{id}") + public Product delete(@PathVariable(name = "id") int id) { + try { + return this.theProducts.deleteProduct(id); + } catch (NoSuchElementException e) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); + } + } +} 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..afef04a --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -0,0 +1,81 @@ +package com.booleanuk.api.products; + +import jdk.jshell.spi.ExecutionControl; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.server.ResponseStatusException; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; + +public class ProductRepository { + private List products; + + public ProductRepository() { + this.products = new ArrayList<>(); + } + + public Product createProduct(Product product) { + for (Product sameNameProduct : this.products) { + if (product.getName().equals(sameNameProduct.getName())) { + return null; + } + } + this.products.add(product); + return product; + } + + public List getAll(String category){ + if (category == null) { + return this.products; + } + List matchingProducts = new ArrayList<>(); + for (Product product : this.products) { + if (product.getCategory().equals(category)) { + matchingProducts.add(product); + } + } + if (matchingProducts.isEmpty()) { + return null; + } + return matchingProducts; + } + + + public Product getOneProduct(int id) throws NoSuchElementException { + return this.products.stream() + .filter(product -> product.getId() == id) + .findFirst() + .orElseThrow(NoSuchElementException::new); + } + + public Product updateProduct(int id, Product product) { + try { + Product productToUpdate = this.getOneProduct(id); + for (Product sameNameProduct : this.products) { + if (product.getName().equals(sameNameProduct.getName())) { + return null; + } + } + productToUpdate.setName(product.getName()); + productToUpdate.setCategory(product.getCategory()); + productToUpdate.setPrice(product.getPrice()); + return productToUpdate; + } catch (NoSuchElementException e) { + throw e; + } + } + + + public Product deleteProduct(int id) { + try { + Product productToDelete = this.getOneProduct(id); + this.products.remove(productToDelete); + return productToDelete; + } catch (NoSuchElementException e) { + throw e; + } + } + +}