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..cc04f69 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductController.java @@ -0,0 +1,70 @@ +package com.booleanuk.api.products; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository theProducts; + + public ProductController() { + this.theProducts = new ProductRepository(); + } + + @GetMapping + 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) { + 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) { + 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) { + 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 new file mode 100644 index 0000000..5230149 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -0,0 +1,75 @@ +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 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; + } + + 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) { + 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) { + 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; + } +}