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..e6436b8 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -0,0 +1,12 @@ +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/controllers/ProductController.java b/src/main/java/com/booleanuk/api/controllers/ProductController.java new file mode 100644 index 0000000..ab3f058 --- /dev/null +++ b/src/main/java/com/booleanuk/api/controllers/ProductController.java @@ -0,0 +1,67 @@ +package com.booleanuk.api.controllers; + +import com.booleanuk.api.models.Product; +import com.booleanuk.api.repositories.ProductRepository; +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 productRepository; + + public ProductController() { + this.productRepository = new ProductRepository(); + } + + @GetMapping + public ArrayList getAll() { + return productRepository.findAll(); + } + + @GetMapping("/{id}") + public Product getOne(@PathVariable int id) { + Product product = productRepository.find(id); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found!!!!"); // 404 basically + } + + return product; + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product create(@RequestBody Product product) { + if (productRepository.getProducts().contains(product)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Not cool."); + } + productRepository.create(product); + return product; + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product update(@PathVariable (name = "id") int id, @RequestBody Product product) { + if (id < productRepository.getProducts().size()) { + productRepository.getProducts().get(id).setType(product.getType()); + productRepository.getProducts().get(id).setCategory(product.getCategory()); + productRepository.getProducts().get(id).setPrice(product.getPrice()); + return productRepository.getProducts().get(id); + } + return null; + } + + @DeleteMapping("/{id}") + public Product delete(@PathVariable (name = "id") int id) { + if (id < productRepository.getProducts().size()) { + Product remove = productRepository.find(id); + productRepository.updateId(id); + return productRepository.getProducts().remove(remove.getId()); + } + return null; + } +} diff --git a/src/main/java/com/booleanuk/api/models/Product.java b/src/main/java/com/booleanuk/api/models/Product.java new file mode 100644 index 0000000..5a400da --- /dev/null +++ b/src/main/java/com/booleanuk/api/models/Product.java @@ -0,0 +1,60 @@ +package com.booleanuk.api.models; + +public class Product { + + private static int nextId = 0; + private int id; + private String type; + private String category; + private int price; + + public Product(int id, String type, String category, int price) { + this.id = nextId ++; + this.type = type; + this.category = category; + this.price = price; + } + + public static int getNextId() { + return nextId; + } + + public static void setNextId(int nextId) { + Product.nextId = nextId; + } + + public int getId() { + + return id; + } + + public String getType() { + + return type; + } + + public int getPrice() { + + return price; + } + + public void setType(String type) { + this.type = type; + } + + public void setId(int id) { + this.id = id; + } + + public String getCategory() { + return category; + } + + 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/repositories/ProductRepository.java b/src/main/java/com/booleanuk/api/repositories/ProductRepository.java new file mode 100644 index 0000000..3615f7c --- /dev/null +++ b/src/main/java/com/booleanuk/api/repositories/ProductRepository.java @@ -0,0 +1,48 @@ +package com.booleanuk.api.repositories; + +import com.booleanuk.api.bagels.Bagel; +import com.booleanuk.api.models.Product; + +import java.util.ArrayList; + +public class ProductRepository { + + private ArrayList products; + private int id; + + public ProductRepository() { + this.products = new ArrayList<>(); + + //this.products.add(new Product(this.id++, "Bagel", "Food", 20)); + //this.products.add(new Product(this.id ++, "Coffee", "Food", 15)); + } + + public void create(Product product) { + this.products.add(product); + } + + public ArrayList getProducts() { + return products; + } + + public void setProducts(ArrayList products) { + this.products = products; + } + + public ArrayList findAll() { + return this.products; + } + + public Product find(int id) { + return this.products.stream() + .filter(product -> product.getId() == id) + .findFirst() + .orElseThrow(); + } + + public void updateId(int id) { + for (int i = id; i < products.size() - 1; i ++) { + products.get(i+1).setId(i); + } + } +}