diff --git a/settings.gradle b/settings.gradle index 0bc3ae4..55e2ff3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,8 @@ +pluginManagement { + repositories { + mavenCentral() + gradlePluginPortal() + } +} + rootProject.name = 'jave.api.mvc.in.memory' diff --git a/src/main/java/com/booleanuk/api/product/Main.java b/src/main/java/com/booleanuk/api/product/Main.java new file mode 100644 index 0000000..bc18173 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/Main.java @@ -0,0 +1,11 @@ +package com.booleanuk.api.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/product/controllers/ProductController.java b/src/main/java/com/booleanuk/api/product/controllers/ProductController.java new file mode 100644 index 0000000..dc55e87 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/controllers/ProductController.java @@ -0,0 +1,83 @@ +package com.booleanuk.api.product.controllers; + +import com.booleanuk.api.product.models.Product; +import com.booleanuk.api.product.repositories.ProductRepository; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository productRepository; + + public ProductController() { + this.productRepository = new ProductRepository(new ArrayList<>()); + } + + @GetMapping + public List getAll(@RequestParam(defaultValue="") String category){ + if(!category.isEmpty()){ + List filteredProd = this.productRepository.getAllByCategory(category); + if(filteredProd.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products with that category found"); + } + return filteredProd; + } + + List allProds = this.productRepository.getAll(); + if(allProds.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products found"); + } + return allProds; + } + + @GetMapping("{id}") + public Product getById(@PathVariable int id){ + Product prod = this.productRepository.getById(id); + nullCheck(prod, HttpStatus.NOT_FOUND, "Product not found."); + return prod; + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product addProduct(@RequestBody Product product){ + if(productRepository.checkNameConflict(product)){ + throw new ResponseStatusException(HttpStatus.CONFLICT, "Product with provided name already exists"); + } + return this.productRepository.add(product); + } + + @PutMapping("{id}") + public Product putProduct(@RequestBody Product product, @PathVariable int id){ + + if(productRepository.checkNameConflict(product)){ + throw new ResponseStatusException(HttpStatus.CONFLICT, "Product with provided name already exists"); + } + + Product foundProd = this.productRepository.getById(id); + nullCheck(foundProd, HttpStatus.NOT_FOUND, "Product not found."); + foundProd.setName(product.getName()); + foundProd.setCategory(product.getCategory()); + foundProd.setPrice(product.getPrice()); + + return foundProd; + } + + @DeleteMapping("{id}") + public Product deleteProduct(@PathVariable int id){ + Product removed = this.productRepository.removeProduct(id); + nullCheck(removed, HttpStatus.NOT_FOUND, "Product not found."); + return removed; + } + + private void nullCheck(Product product, HttpStatus status, String message){ + if(product == null){ + throw new ResponseStatusException(status, message); + } + } + +} diff --git a/src/main/java/com/booleanuk/api/product/models/Product.java b/src/main/java/com/booleanuk/api/product/models/Product.java new file mode 100644 index 0000000..5109547 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/models/Product.java @@ -0,0 +1,45 @@ +package com.booleanuk.api.product.models; + +public class Product { + private static int nextId; + private String name; + private String category; + private String price; + private int id; + + public Product(String name, String category, String price) { + this.name = name; + this.category = category; + this.price = price; + this.id = nextId; + nextId++; + } + + public String getName() { + return name; + } + + public String getCategory() { + return category; + } + + public String getPrice() { + return price; + } + + public int getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + + public void setCategory(String category) { + this.category = category; + } + + public void setPrice(String price) { + this.price = price; + } +} diff --git a/src/main/java/com/booleanuk/api/product/repositories/ProductRepository.java b/src/main/java/com/booleanuk/api/product/repositories/ProductRepository.java new file mode 100644 index 0000000..5fa0e33 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/repositories/ProductRepository.java @@ -0,0 +1,58 @@ +package com.booleanuk.api.product.repositories; + +import com.booleanuk.api.product.models.Product; + +import java.util.ArrayList; +import java.util.List; + +public class ProductRepository { + private List products; + + public ProductRepository(List products) { + this.products = products; + } + + public List getAll(){ + return this.products; + } + + public boolean checkNameConflict(Product product){ + for(Product curr : this.products){ + if(curr.getName().equals(product.getName())){ + return true; + } + } + return false; + } + + public Product add(Product product){ //Void for now, would return db return if connected to db + this.products.add(product); + return product; + } + + public Product getById(int id){ + for(Product product : this.products){ + if(product.getId() == id){ + return product; + } + } + return null; + } + + public Product removeProduct(int id){ + Product removeProd = this.getById(id); + this.products.remove(removeProd); + return removeProd; //Return null if not found + } + + public List getAllByCategory(String cat){ + List found = new ArrayList<>(); + for(Product curr : this.products){ + if(cat.equals(curr.getCategory())){ + found.add(curr); + } + } + return found; + + } +}