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/controllers/ProductController.java b/src/main/java/com/booleanuk/api/controllers/ProductController.java new file mode 100644 index 0000000..309702a --- /dev/null +++ b/src/main/java/com/booleanuk/api/controllers/ProductController.java @@ -0,0 +1,65 @@ +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.List; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository theProducts; + + public ProductController(){ + this.theProducts = new ProductRepository(); + } + + @GetMapping + public List getAll(@RequestParam(name = "category", required = false, defaultValue = "") String category){ + if(category.isEmpty()) + return this.theProducts.findAll(); + List products = this.theProducts.findAll(category); + if (products.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of that category"); + } + return products; + } + + + @GetMapping("/{id}") + public Product getOne(@PathVariable(name="id") int id) { + Product product = this.theProducts.find(id); + if (product == null){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found!"); + } + return product; + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product post(@RequestBody Product product){ + if (this.theProducts.addProduct(product)) + return product; + throw new ResponseStatusException(HttpStatus.BAD_REQUEST); + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product update(@PathVariable (name = "id") int id, @RequestBody Product product) { + Product pr = theProducts.update(id, product); + if (pr == null) + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + return pr; + } + + @DeleteMapping("/{id}") + public Product delete(@PathVariable (name = "id") int id) { + Product pr = this.theProducts.delete(id); + if (pr == null) + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + return pr; + } +} 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..7737bea --- /dev/null +++ b/src/main/java/com/booleanuk/api/models/Product.java @@ -0,0 +1,46 @@ +package com.booleanuk.api.models; + +public class Product { + private static int nextId; + + private int id; + private String name; + private String category; + private int price; + + public Product(String name, String category, int price) { + 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; + } +} 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..c9b7227 --- /dev/null +++ b/src/main/java/com/booleanuk/api/repositories/ProductRepository.java @@ -0,0 +1,74 @@ +package com.booleanuk.api.repositories; + +import com.booleanuk.api.models.Product; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +import java.util.ArrayList; +import java.util.List; + +public class ProductRepository { + List products; + + public ProductRepository() { + this.products = new ArrayList<>(); + + products.add(new Product("How to buil APIs", "Book", 1500)); + } + + public boolean addProduct(Product product){ + for(Product pr: products){ + if (pr.getName().equals(product.getName())) + return false; + } + products.add(product); + return true; + } + + public List findAll() { + return products; + } + + public List findAll(String category) { + List foundPr = new ArrayList<>(); + for (Product pr: products){ + if (pr.getCategory().equals(category)) + foundPr.add(pr); + } + return foundPr; + } + + public Product find(int id) { + for (Product product : this.products) { + if (product.getId() == id) { + return product; + } + } + return null; + } + + public Product delete(int id){ + for (Product product : this.products) { + if (product.getId() == id) { + this.products.remove(product); + return product; + } + } + return null; + } + + public Product update(int id, Product product ){ + for (Product pr: products){ + if (pr.getName().equals(product.getName())) + throw new ResponseStatusException(HttpStatus.BAD_REQUEST); + } + Product product1 = find(id); + if (product1 != null){ + product1.setName(product.getName()); + product1.setCategory(product.getCategory()); + product1.setPrice(product.getPrice()); + return product1; + } + return null; + } +}