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..b4c9200 --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/product/ProductController.java @@ -0,0 +1,68 @@ +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; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository theProducts; + + public ProductController() { + this.theProducts = new ProductRepository(); + } + + @GetMapping + @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) { + 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) { + 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) { + + 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) { + 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 new file mode 100644 index 0000000..f578494 --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/product/ProductRepository.java @@ -0,0 +1,73 @@ +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(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) { + for(Product product : this.products) { + if(product.getId() == id) { + return product; + } + } + return null; + } + + 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) { + + 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 true; + } + + public Product delete(int id) { + Product theProduct = this.getOne(id); + if(theProduct == null) { + return theProduct; + } + this.products.remove(theProduct); + return theProduct; + } +}