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..e6436b8 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -0,0 +1,12 @@ +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/products/Product.java b/src/main/java/com/booleanuk/api/products/Product.java new file mode 100644 index 0000000..c4f7a5e --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/Product.java @@ -0,0 +1,45 @@ +package com.booleanuk.api.products; + +public class Product { + private static int nextId = 1; + private final int id; + private String name; + private String category; + private int price; + + public Product(String name, String category, int price){ + this.id = nextId; + nextId+=1; + this.name = name; + this.category = category; + this.price = price; + } + + public int getId(){ + return this.id; + } + + public String getName(){ + return this.name; + } + + public void setName(String newName){ + this.name = newName; + } + + public String getCategory(){ + return this.category; + } + + public void setCategory(String newCategory){ + this.category = newCategory; + } + + public int getPrice(){ + return this.price; + } + + public void setPrice(int newPrice){ + this.price = newPrice; + } +} diff --git a/src/main/java/com/booleanuk/api/products/ProductController.java b/src/main/java/com/booleanuk/api/products/ProductController.java new file mode 100644 index 0000000..0116784 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductController.java @@ -0,0 +1,45 @@ +package com.booleanuk.api.products; + + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository productRepo = new ProductRepository(); + + @GetMapping + public List getProducts(@RequestParam (required = false) String category){ + if (category != null){ + return this.productRepo.getByCategory(category); + } + return this.productRepo.getAll(); + } + + @GetMapping("/{id}") + public Product getOneProduct(@PathVariable(name = "id") int id){ + return this.productRepo.getOne(id); + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product createOneProduct(@RequestBody Product product){ + return this.productRepo.createProduct(product); + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product updateOneProduct(@PathVariable(name = "id") int id, @RequestBody Product product){ + return this.productRepo.updateProduct(id, product); + } + + @DeleteMapping("/{id}") + public Product deleteOneProduct(@PathVariable(name = "id") int id){ + return this.productRepo.deleteProduct(id); + } + + +} diff --git a/src/main/java/com/booleanuk/api/products/ProductRepository.java b/src/main/java/com/booleanuk/api/products/ProductRepository.java new file mode 100644 index 0000000..f42bbb0 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -0,0 +1,79 @@ +package com.booleanuk.api.products; + + +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +import java.util.ArrayList; + +public class ProductRepository { + + private ArrayList products = new ArrayList<>(){{ + add(new Product("How to build APIs", "Book", 1500)); + add(new Product("Elden Ring", "Game", 90)); + }}; + + public Product createProduct(Product product){ + for (Product p : products){ + if (p.getName().equals(product.getName())){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provided name already exists!"); + } + } + this.products.add(product); + return product; + } + + public ArrayList getAll(){ + return this.products; + } + + public ArrayList getByCategory(String category){ + ArrayList categoryList = new ArrayList<>(); + for (Product p : products){ + if (p.getCategory().toLowerCase().equals(category)){ + categoryList.add(p); + } + } + if (!categoryList.isEmpty()){ + return categoryList; + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found!"); + } + + public Product getOne(int id){ + for (Product p : products){ + if (p.getId() == id){ + return p; + } + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found!"); + } + + public Product updateProduct(int id, Product product){ + for (Product p : products){ + if (p.getId() == id){ + for (Product ps : products){ + if (ps.getName().equals(product.getName()) && ps != p){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists!"); + } + } + p.setName(product.getName()); + p.setCategory(product.getCategory()); + p.setPrice(product.getPrice()); + return p; + } + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found!"); + } + + public Product deleteProduct(int id){ + for (Product p : products){ + if (p.getId() == id){ + products.remove(p); + return p; + } + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found!"); + } + +}