From f1bd92c05b83a672a09483250bd9902cfee0a513 Mon Sep 17 00:00:00 2001 From: Jostein Ruen Date: Mon, 2 Sep 2024 08:27:44 +0200 Subject: [PATCH] finished core and ext --- src/main/java/com/booleanuk/api/Main.java | 11 +++ src/main/java/com/booleanuk/api/Product.java | 49 ++++++++++++ .../com/booleanuk/api/ProductController.java | 52 ++++++++++++ .../com/booleanuk/api/ProductRepository.java | 79 +++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 src/main/java/com/booleanuk/api/Main.java create mode 100644 src/main/java/com/booleanuk/api/Product.java create mode 100644 src/main/java/com/booleanuk/api/ProductController.java create mode 100644 src/main/java/com/booleanuk/api/ProductRepository.java 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.java b/src/main/java/com/booleanuk/api/Product.java new file mode 100644 index 0000000..46d12a8 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Product.java @@ -0,0 +1,49 @@ +package com.booleanuk.api; + +public class Product { + private static int uniqueId = 1; + private int id; + private String name; + private String category; + private int price; + + public Product(String name, String category, int price) { + this.name = name; + this.category = category; + this.price = price; + this.id = uniqueId; + uniqueId++; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = 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; + } +} diff --git a/src/main/java/com/booleanuk/api/ProductController.java b/src/main/java/com/booleanuk/api/ProductController.java new file mode 100644 index 0000000..327232e --- /dev/null +++ b/src/main/java/com/booleanuk/api/ProductController.java @@ -0,0 +1,52 @@ +package com.booleanuk.api; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +@RestController +@RequestMapping("products") +public class ProductController { + private ProductRepository productRepository; + + public ProductController(){ + this.productRepository = new ProductRepository(); + } + + @GetMapping List getAll(@RequestParam(required = false) String category){ + return this.productRepository.findAll(category); + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product create(@RequestBody Product product){ + this.productRepository.create(product.getName(), product.getCategory(), product.getPrice()); + return product; + } + + @GetMapping("/{id}") + public Product getSpecificProduct(@PathVariable int id){ + return this.productRepository.findSpecificProduct(id); + } + + @DeleteMapping("/{id}") + public Product deleteProduct(@PathVariable int id){ + return this.productRepository.delete(id); + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product updateProduct(@PathVariable int id, @RequestBody Product product){ + Product newProduct = this.productRepository.update(id, product); + + if(newProduct == null){ + for (int i = 0; i < productRepository.getProducts().size(); i++){ + + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "The product with the ID: " + id + " could not be found in repository"); + } + return newProduct; + } +} diff --git a/src/main/java/com/booleanuk/api/ProductRepository.java b/src/main/java/com/booleanuk/api/ProductRepository.java new file mode 100644 index 0000000..6143f82 --- /dev/null +++ b/src/main/java/com/booleanuk/api/ProductRepository.java @@ -0,0 +1,79 @@ +package com.booleanuk.api; + +import com.sun.net.httpserver.HttpsServer; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +import java.util.ArrayList; +import java.util.List; + +public class ProductRepository { + + private List products = new ArrayList<>(); + + public ProductRepository(){ + products.add(new Product("Corn flakes", "Food", 20)); + } + + public void create(String name, String category, int price){ + for(Product p: products){ + if(p.getName().equals(name)){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product already exists in storage"); + } + } + Product product = new Product(name,category,price); + this.products.add(product); + } + + public List findAll(String category) { + //If no parameter, just return the entire list + if(category == null){ + return products; + } + List categoryList = new ArrayList<>(); + for(Product p: products){ + if(p.getCategory().equalsIgnoreCase(category)){ + categoryList.add(p); + } + } + if(categoryList.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Couldn't find any products in the given category"); + } + return categoryList; + } + + public Product update(int id, Product newProduct){ + for(Product p: products){ + if(p.getId() == id){ + p.setName(newProduct.getName()); + p.setCategory(newProduct.getCategory()); + p.setPrice(newProduct.getPrice()); + return newProduct; + } + } + return null; + } + + public Product delete(int id){ + for(Product p: products){ + if(p.getId() == id){ + products.remove(p); + return p; + } + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "The product with the ID: " + id + " does not exist"); + } + + public Product findSpecificProduct(int id){ + for(Product p: products){ + if(p.getId() == id){ + return p; + } + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Couldn't find the product with ID: " + id); + } + + public List getProducts() { + return products; + } +}