diff --git a/src/main/java/com/booleanuk/api/bagels/Bagel.java b/src/main/java/com/booleanuk/api/bagels/Bagel.java deleted file mode 100644 index 77a24a6..0000000 --- a/src/main/java/com/booleanuk/api/bagels/Bagel.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.booleanuk.api.bagels; - -public class Bagel { - private int id; - private String type; - private int price; - - public Bagel(int id, String type, int price) { - this.id = id; - this.type = type; - this.price = price; - } - - public int getId() { - return id; - } - - public String getType() { - return type; - } - - public int getPrice() { - return price; - } -} diff --git a/src/main/java/com/booleanuk/api/bagels/BagelController.java b/src/main/java/com/booleanuk/api/bagels/BagelController.java deleted file mode 100644 index cce2764..0000000 --- a/src/main/java/com/booleanuk/api/bagels/BagelController.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.booleanuk.api.bagels; - -import java.util.List; - -public class BagelController { - BagelRepository repository; - - public BagelController(BagelRepository repository) { - this.repository = repository; - } - - public List getAll() { - return this.repository.findAll(); - } -} diff --git a/src/main/java/com/booleanuk/api/bagels/BagelRepository.java b/src/main/java/com/booleanuk/api/bagels/BagelRepository.java deleted file mode 100644 index 320ddba..0000000 --- a/src/main/java/com/booleanuk/api/bagels/BagelRepository.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.booleanuk.api.bagels; - -import java.util.ArrayList; -import java.util.List; - -public class BagelRepository { - private int idCounter = 1; - private List data = new ArrayList<>(); - - public void create(String type, int price) { - Bagel bagel = new Bagel(this.idCounter++, type, price); - this.data.add(bagel); - } - - public List findAll() { - return this.data; - } - - public Bagel find(int id) { - return this.data.stream() - .filter(bagel -> bagel.getId() == id) - .findFirst() - .orElseThrow(); - } -} diff --git a/src/main/java/com/booleanuk/api/products/Main.java b/src/main/java/com/booleanuk/api/products/Main.java new file mode 100644 index 0000000..a741936 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/Main.java @@ -0,0 +1,12 @@ +package com.booleanuk.api.products; + +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/products/controller/ProductController.java b/src/main/java/com/booleanuk/api/products/controller/ProductController.java new file mode 100644 index 0000000..aab0a29 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/controller/ProductController.java @@ -0,0 +1,42 @@ +package com.booleanuk.api.products.controller; + +import com.booleanuk.api.products.model.Product; +import com.booleanuk.api.products.model.ProductRepository; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("products") +public class ProductController { + ProductRepository repository; + + public ProductController(ProductRepository repository) { + this.repository = repository; + } + + @GetMapping + public List getAll(@RequestParam(required = false) String category) { + return this.repository.findAll(category); + } + + @GetMapping("{id}") + public Product getById(@PathVariable int id) { + return repository.find(id); + } + + @PostMapping + public Product createProduct(@RequestBody Product newProduct){ + return repository.addProduct(newProduct); + } + + @PutMapping("{id}") + public Product updateProduct(@PathVariable int id, @RequestBody String name){ + return repository.updateProduct(id, name); + } + + @DeleteMapping("{id}") + public boolean deleteProduct(@PathVariable int id){ + return repository.deleteProduct(id); + } +} diff --git a/src/main/java/com/booleanuk/api/products/model/Product.java b/src/main/java/com/booleanuk/api/products/model/Product.java new file mode 100644 index 0000000..62be14a --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/model/Product.java @@ -0,0 +1,41 @@ +package com.booleanuk.api.products.model; + +public class Product { + private int id; + private String name; + private String category; + private int price; + + public Product(int id, String name, String category, int price) { + this.id = id; + this.name = name; + this.category = category; + this.price = price; + } + + public int getId() { + return id; + } + + public String getCategory() { + return category; + } + + public int getPrice() { + return price; + } + + public String getName(){ return name; } + + 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/products/model/ProductRepository.java b/src/main/java/com/booleanuk/api/products/model/ProductRepository.java new file mode 100644 index 0000000..958391e --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/model/ProductRepository.java @@ -0,0 +1,69 @@ +package com.booleanuk.api.products.model; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Repository; +import org.springframework.web.server.ResponseStatusException; + +import java.util.ArrayList; +import java.util.List; + +@Repository +public class ProductRepository { + private int idCounter = 1; + private List data = new ArrayList<>(); + + public ProductRepository() { + Product product1 = new Product(this.idCounter++, "car", "utilities", 10000); + Product product2 = new Product(this.idCounter++, "house", "utilities", 200000); + Product product3 = new Product(this.idCounter++, "beer", "drink", 5); + this.data.add(product1); + this.data.add(product2); + this.data.add(product3); + } + + public List findAll(String category) { + if (category != null && !category.isEmpty()){ + List ls = this.data.stream().filter(n -> n.getCategory().equals(category)).toList(); + if (ls.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found."); + } + return ls; + } + return this.data; + } + + public Product find(int id) { + List product = this.data.stream() + .filter(prod -> prod.getId() == id) + .toList(); + if (product.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); + } + return product.getFirst(); + } + + public Product addProduct(Product product){ + if (data.stream().filter(it -> it.getName().equals(product.getName())).toList().isEmpty()){ + this.data.add(product); + throw new ResponseStatusException(HttpStatus.CREATED, "Create a new product"); + } + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); + } + + public Product updateProduct(int id, String name){ + Product product = find(id); + List existingProduct = data.stream().filter(it -> it.getName().equals(name)).toList(); + + if (!existingProduct.isEmpty()){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); + } + product.setName(name); + throw new ResponseStatusException(HttpStatus.CREATED, "Product successfully updated."); + } + + public boolean deleteProduct(int id){ + Product product = find(id); + this.data.remove(product); + throw new ResponseStatusException(HttpStatus.OK, "Product successfully deleted."); + } +}