diff --git a/src/main/java/com/booleanuk/api/product/Main.java b/src/main/java/com/booleanuk/api/product/Main.java new file mode 100644 index 0000000..bc18173 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/Main.java @@ -0,0 +1,11 @@ +package com.booleanuk.api.product; + +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/controller/Dto/ProductCreateDto.java b/src/main/java/com/booleanuk/api/product/controller/Dto/ProductCreateDto.java new file mode 100644 index 0000000..68b1c36 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/controller/Dto/ProductCreateDto.java @@ -0,0 +1,42 @@ +package com.booleanuk.api.product.controller.Dto; + +public class ProductCreateDto { + private String name; + private String category; + private int price; + + public ProductCreateDto() { + + } + public ProductCreateDto(String name, int id, int price, String category) { + setName(name); + setCategory(category); + setPrice(price); + } + + 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/product/controller/ProductController.java b/src/main/java/com/booleanuk/api/product/controller/ProductController.java new file mode 100644 index 0000000..8a00196 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/controller/ProductController.java @@ -0,0 +1,57 @@ +package com.booleanuk.api.product.controller; + +import com.booleanuk.api.product.controller.Dto.ProductCreateDto; +import com.booleanuk.api.product.model.pojo.Product; +import com.booleanuk.api.product.model.repository.ProductRepository; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/products") +public class ProductController { + + private ProductRepository productRepository; + + public ProductController() { + productRepository = new ProductRepository(); + + } + + @GetMapping + public List getProducts(@RequestParam(required = false) String category) { + return productRepository.getProducts(category); + } + + @GetMapping("/{id}") + public Product getProductById(@PathVariable int id) { + return productRepository.getOne(id); + } + + + @PostMapping() + public ResponseEntity createProduct(@RequestBody ProductCreateDto productCreateDto) { + Product product = new Product(productCreateDto); + + productRepository.createProduct(product); + + return ResponseEntity.status(HttpStatus.CREATED).body(product); + } + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product update(@PathVariable int id, @RequestBody ProductCreateDto body){ + return productRepository.updateProduct(id, body); + } + + @DeleteMapping("/{id}") + @ResponseStatus(HttpStatus.OK) + public Product delete(@PathVariable int id) { + return productRepository.deleteProduct(id); + } + + +} diff --git a/src/main/java/com/booleanuk/api/product/model/pojo/Product.java b/src/main/java/com/booleanuk/api/product/model/pojo/Product.java new file mode 100644 index 0000000..1f889d9 --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/model/pojo/Product.java @@ -0,0 +1,55 @@ +package com.booleanuk.api.product.model.pojo; + +import com.booleanuk.api.product.controller.Dto.ProductCreateDto; + +public class Product { + private static int nextId = 1; + + private String name; + private String category; + private int price; + private int id; + + public Product(String name, String category, int price) { + setId(nextId++); + setName(name); + setCategory(category); + setPrice(price); + } + + public Product(ProductCreateDto productCreateDto) { + this(productCreateDto.getName(), productCreateDto.getCategory(), productCreateDto.getPrice()); + } + + 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/product/model/repository/ProductRepository.java b/src/main/java/com/booleanuk/api/product/model/repository/ProductRepository.java new file mode 100644 index 0000000..14f9d6a --- /dev/null +++ b/src/main/java/com/booleanuk/api/product/model/repository/ProductRepository.java @@ -0,0 +1,85 @@ +package com.booleanuk.api.product.model.repository; + +import com.booleanuk.api.product.controller.Dto.ProductCreateDto; +import com.booleanuk.api.product.model.pojo.Product; +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; + + public ProductRepository() { + this.products = new ArrayList<>(); + + this.products.add(new Product("Spoon", "Kitchen", 100)); + this.products.add(new Product("Laptop", "Electronics", 10000)); + this.products.add(new Product("Book", "Education", 150)); + + } + + public List getProducts(String category) { + if (category == null || category.isBlank()) { + return this.products; + } + List result = new ArrayList<>(); + for (Product p : products){ + if (p.getCategory().equalsIgnoreCase(category)) { + result.add(p); + } + } + + if (result.isEmpty()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found."); + } + + return result; + } + + 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 void createProduct(Product product) { + for (Product p : this.products) { + if (p.getName().equalsIgnoreCase((product.getName()))) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists"); + } + + } + products.add(product); + } + + public Product updateProduct(int id, ProductCreateDto body) { + for (Product p : this.products) { + if (p.getId() == id) { + p.setName(body.getName()); + p.setPrice(body.getPrice()); + p.setCategory(body.getCategory()); + return p; + } + } + + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "product not found"); + } + + public Product deleteProduct(int id) { + for (Product p : this.products){ + if (p.getId() == id) { + this.products.remove(p); + return p; + } + } + + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } +}