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/Product.java b/src/main/java/com/booleanuk/api/Product/Product.java new file mode 100644 index 0000000..d2bfa11 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product/Product.java @@ -0,0 +1,30 @@ +package com.booleanuk.api.Product; + +public class Product { + private int id; + private String name; + private String category; + private Integer price; + + public Product(int id, String name, String category, Integer price){ + this.id = id; + this.name = name; + this.category = category; + this.price = price; + + } + + public int getId() { return 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 Integer getPrice() { return price; } + + public void setPrice(Integer price) {this.price = price; } +} diff --git a/src/main/java/com/booleanuk/api/Product/ProductController.java b/src/main/java/com/booleanuk/api/Product/ProductController.java new file mode 100644 index 0000000..4b68bb9 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product/ProductController.java @@ -0,0 +1,83 @@ +package com.booleanuk.api.Product; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import com.booleanuk.api.Product.ProductRepository; +import org.springframework.web.server.ResponseStatusException; +import java.util.List; + +@RestController +@RequestMapping("/products") +public class ProductController { + ProductRepository repository; + + public ProductController() { + this.repository = new ProductRepository(); + } + + @GetMapping + public List getAll() { + List products = this.repository.findAll(); + if (products.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found"); + } + return products; + } + + + @PostMapping + public Product addProduct(@RequestBody Product product){ + List items = this.getAll(); + + boolean exists = items.stream() + .anyMatch(item -> item.getName().equals(product.getName())); + + if (exists) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); + } + + return this.repository.create(product.getName(), product.getCategory(), product.getPrice()); + } + + @GetMapping("{id}") + public Product findId(@PathVariable int id) { + Product product = this.repository.find(id); + + if (product == null){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + + return product; + } + + @PutMapping("{id}") + public Product updateProduct(@PathVariable int id, @RequestBody Product product){ + + Product existing = this.repository.find(id); + + if (existing == null){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + + boolean nameExists = this.repository.findAll().stream() + .anyMatch(p -> !(p.getId() == id) && p.getName().equals(product.getName())); + + if (nameExists) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); + } + + return this.repository.updateProduct(id, product); + } + + @DeleteMapping("/{id}") + public Product deleteProduct(@PathVariable int id){ + Product delete = this.repository.deleteProduct(id); + + if (delete == null){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + + return delete; + } + +} diff --git a/src/main/java/com/booleanuk/api/Product/ProductRepository.java b/src/main/java/com/booleanuk/api/Product/ProductRepository.java new file mode 100644 index 0000000..e323a2d --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product/ProductRepository.java @@ -0,0 +1,43 @@ +package com.booleanuk.api.Product; + +import java.util.*; + +public class ProductRepository { + private int idCounter = 1; + private List data = new ArrayList<>(); + + public ProductRepository(){ + this.data.add(new Product(0, "Eple", "Frukt", 10)); + } + + + public Product create(String name, String category, int price) { + Product product = new Product(this.idCounter++, name, category, price); + this.data.add(product); + return product; + } + + public List findAll() { return this.data; } + + public Product find (int id) { + return this.data.stream() + .filter(product -> product.getId() == id) + .findFirst() + .orElse(null); + } + + public Product deleteProduct(int id){ + Product product = this.find(id); + if (product == null) return null; + this.data.remove(product); + return product; + } + + public Product updateProduct(int id, Product product){ + Product product1 = this.find(id); + product1.setName(product.getName()); + product1.setCategory(product.getCategory()); + product1.setPrice(product.getPrice()); + return product1; + } +}