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/Product.java b/src/main/java/com/booleanuk/api/Product.java new file mode 100644 index 0000000..5e876b3 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product.java @@ -0,0 +1,44 @@ +package com.booleanuk.api; + +public class Product { + private static int nextId = 1; + private final int id; + private String name; + private String category; + private int price; + + public Product(String name, String category, int price) { + this.id = nextId++; + this.name = name; + this.category = category; + this.price = price; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public String getCategory() { + return category; + } + + public int getPrice() { + return price; + } + + public void setName(String name) { + this.name = name; + } + + public void setCategory(String category) { + this.category = category; + } + + public void setPrice(int price) { + this.price = price; + } +} diff --git a/src/main/java/com/booleanuk/api/ProductController.java b/src/main/java/com/booleanuk/api/ProductController.java new file mode 100644 index 0000000..6dcb892 --- /dev/null +++ b/src/main/java/com/booleanuk/api/ProductController.java @@ -0,0 +1,77 @@ +package com.booleanuk.api; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository repo = new ProductRepository(); + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product createProduct(@RequestBody Product product) { + Product created = repo.create(product); + if (created == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); + } + return created; + } + + @GetMapping + public List getProducts(@RequestParam(required = false) String category) { + List result = new ArrayList<>(); + if (category != null) { + result = repo.getCategory(category); + if (result == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, + "No products of the provided category were found."); + } + } else { + result = repo.getAll(); + } + return result; + } + + @GetMapping("/{id}") + public Product getSpecificProduct(@PathVariable int id) { + Product product = repo.getProduct(id); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); + } + return product; + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product updateProduct(@PathVariable int id, @RequestBody Product updated) { + Product product = repo.update(id, updated); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); + } + return product; + } + + @DeleteMapping("/{id}") + public Product deleteProduct(@PathVariable int id) { + Product product = repo.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/ProductRepository.java b/src/main/java/com/booleanuk/api/ProductRepository.java new file mode 100644 index 0000000..fbf7eaa --- /dev/null +++ b/src/main/java/com/booleanuk/api/ProductRepository.java @@ -0,0 +1,82 @@ +package com.booleanuk.api; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +public class ProductRepository { + private List products = new ArrayList<>() { + { + add(new Product("How to build APIs", "Book", 1500)); + add(new Product("Grey bat", "Sports", 3000)); + } + }; + + List getAll() { + return products; + } + + List getCategory(String category) { + List filtered = new ArrayList<>(); + for (Product p : products) { + if (p.getCategory().toLowerCase().equals(category.toLowerCase())) { + filtered.add(p); + } + } + // return null if no items with this category + if (filtered.size() == 0) { + return null; + } + return filtered; + } + + Product getProduct(int id) { + for (Product p : products) { + if (p.getId() == id) { + return p; + } + } + return null; + } + + Product create(Product product) { + // check if product with same name exists + for (Product p : products) { + if (p.getName().equals(product.getName())) { + return null; + } + } + + products.add(product); + return product; + } + + Product update(int id, Product updated) { + Product product = getProduct(id); + if (product != null) { + // check if product with updated name already exists + for (Product p : products) { + if (p.getName().equals(updated.getName())) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, + "Product with provided name already exists."); + } + } + product.setName(updated.getName()); + product.setCategory(updated.getCategory()); + product.setPrice(updated.getPrice()); + } + return product; + } + + Product delete(int id) { + for (Product p : products) { + if (p.getId() == id) { + products.remove(p); + return p; + } + } + return null; + } +}