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..24fb323 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -0,0 +1,10 @@ +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); + } +} \ No newline at end of file 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..b1d61bd --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product/Product.java @@ -0,0 +1,47 @@ +package com.booleanuk.api.Product; + +public class Product { + private static int nextID = 1; + + private int id; + private String name; + private String category; + private int price; + + public Product(String name, String category, int price) { + this.id = nextID; + nextID++; + 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 int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } +} \ No newline at end of file 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..8af69f3 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product/ProductController.java @@ -0,0 +1,81 @@ +package com.booleanuk.api.Product; + +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 theProducts; + + public ProductController() { + this.theProducts = new ProductRepository(); + } + + @GetMapping + public List getAllProducts(@RequestParam(name = "category", required = false)String category ) { + List products; + if(category != null){ + products = this.theProducts.getAll(category); + } + else{ + products = this.theProducts.getAll(); + } + + + if (products.isEmpty()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"); + } + return products; + } + + + @GetMapping("/{id}") + public Product getOneProduct(@PathVariable(name = "id") int id) { + + if (this.theProducts.getOne(id) == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"); + } + return this.theProducts.getOne(id); + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product updateProduct(@PathVariable(name = "id") int id, @RequestBody Product newProduct){ + + if(this.theProducts.chechNameAvailable(newProduct.getName(), id)){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Name already exists"); + } + if (this.theProducts.updateProduct(id, newProduct) == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"); + } + return newProduct; + } + + @DeleteMapping("/{id}") + public Product removeProduct(@PathVariable(name = "id") int id){ + + Product product = this.theProducts.removeProduct(id); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"); + } + return product; + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product createProduct(@RequestBody Product product){ + + if(this.theProducts.chechNameAvailable(product.getName())){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Name already exists"); + } + if (this.theProducts.createProduct(product) == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"); + } + return product; + } +} \ No newline at end of file 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..181084c --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product/ProductRepository.java @@ -0,0 +1,84 @@ +package com.booleanuk.api.Product; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +public class ProductRepository { + + private ArrayList products; + + public ProductRepository(){ + this.products = new ArrayList<>(); + + this.products.add(new Product("Volvo c30 R-design", "Car", 100000)); + this.products.add(new Product("Nintendo", "Game console", 4000)); + } + + public ArrayList getAll(){ + return this.products; + } + + public Product getOne(int id){ + for(Product product : products){ + if(product.getId() == id){ + return product; + } + } + return null; + } + + public Product updateProduct(int id, Product updatedProduct){ + for(Product product : products){ + if(product.getId() == id){ + product.setName(updatedProduct.getName()); + product.setCategory(updatedProduct.getCategory()); + product.setPrice(updatedProduct.getPrice()); + return product; + } + } + return null; + } + + public Product removeProduct(int id){ + for(Product product : products){ + if(product.getId() == id){ + products.remove(product); + return product; + } + } + return null; + } + + public Product createProduct(Product product){ + products.add(product); + return product; + } + + public Boolean chechNameAvailable(String name){ + for (Product product : products){ + if(product.getName().equals(name)){ + return true; + } + } + return false; + } + + public Boolean chechNameAvailable(String name, int id){ + for (Product product : products){ + if(product.getName().equals(name) && product.getId() != id){ + return true; + } + } + return false; + } + + public List getAll(String category) { + List temp = new ArrayList<>(); + temp = this.products.stream() + .filter(product -> product.getCategory().equals(category)) + .toList(); + return temp; + } +} 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(); - } -}