diff --git a/src/main/java/com/booleanuk/api/CorsConfig.java b/src/main/java/com/booleanuk/api/CorsConfig.java new file mode 100644 index 0000000..b6dde7d --- /dev/null +++ b/src/main/java/com/booleanuk/api/CorsConfig.java @@ -0,0 +1,23 @@ +package com.booleanuk.api; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class CorsConfig { + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") // allow all paths + .allowedOrigins("*") // http://localhost:3000 + .allowedMethods("GET", "POST", "PUT", "DELETE") + .allowedHeaders("*"); + } + }; + } +} 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/products/Product.java b/src/main/java/com/booleanuk/api/products/Product.java new file mode 100644 index 0000000..3c90bb8 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/Product.java @@ -0,0 +1,49 @@ +package com.booleanuk.api.products; + +public class Product { + private int id; + private String name; + private String category; + private int price; + + public Product() { + } + + public Product(String name, String category, int price) { + this.name = name; + this.category = category; + this.price = price; + } + + 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/products/ProductController.java b/src/main/java/com/booleanuk/api/products/ProductController.java new file mode 100644 index 0000000..b707589 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductController.java @@ -0,0 +1,68 @@ +package com.booleanuk.api.products; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.NoSuchElementException; + +@RestController +@RequestMapping("/products") +public class ProductController { + ProductRepository repository; + + public ProductController(ProductRepository repository) { + this.repository = repository; + } + + @PostMapping + public ResponseEntity createProduct(@RequestBody Product product) { + try { + Product p = this.repository.createProduct(product); + return ResponseEntity.status(HttpStatus.CREATED).body(p); + } catch (Exception e) { + return ResponseEntity.badRequest().body("Error creating product: " + e.getMessage()); + } + } + + @GetMapping + public ResponseEntity getAllProducts(@RequestParam String category ) { + try { + return ResponseEntity.ok(this.repository.getAllProducts(category)); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error: " + e.getMessage()); + } + } + + @GetMapping("/{id}") + public ResponseEntity getSingleProduct(@PathVariable int id) { + try { + return ResponseEntity.ok(this.repository.getSingleProduct(id)); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error.. " + e.getMessage()); + } + } + + @PutMapping("/{id}") + public ResponseEntity updateProduct(@PathVariable int id, @RequestBody Product body) { + try { + return ResponseEntity.status(HttpStatus.CREATED).body(this.repository.updateProduct(id, body)); + } catch (IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Error: " + e.getMessage()); + } catch (NoSuchElementException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error: " + e.getMessage()); + } + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteProduct(@PathVariable int id) { + try { + this.repository.deleteProduct(id); + return ResponseEntity.status(HttpStatus.OK).body("Product removed."); + } catch (NoSuchElementException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error: " + e.getMessage()); + } + } + +} 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..bee6b77 --- /dev/null +++ b/src/main/java/com/booleanuk/api/products/ProductRepository.java @@ -0,0 +1,68 @@ +package com.booleanuk.api.products; + +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; + +@Repository +public class ProductRepository { + private int idCounter = 1; + private List productList = new ArrayList<>(); + + public ProductRepository() {} + + public Product createProduct(Product product) throws Exception { + for (Product p : productList) { + if (p.getName().equalsIgnoreCase(product.getName())) { + throw new Exception("Product with provided name already exists."); + } + } + + product.setId(idCounter++); + this.productList.add(product); + return product; + } + + public List getAllProducts(String category) throws Exception { + List list = productList.stream() + .filter(product -> product.getCategory().equalsIgnoreCase(category)) + .toList(); + if (!list.isEmpty()) { + return list; + } else { + throw new Exception("No products of the provided category were found."); + } + } + + public Product getSingleProduct(int id) { + return productList.stream() + .filter(product -> product.getId() == id) + .findFirst().orElseThrow(() -> new NoSuchElementException("Product not found with id: " + id)); + } + + public Product updateProduct(int id, Product body) { + Product product = productList.stream() + .filter(p -> p.getId() == id) + .findFirst().orElseThrow(() -> new NoSuchElementException("Product not found with id: " + id)); + + for (Product p : productList) { + if (p.getName().equalsIgnoreCase(body.getName())) + throw new IllegalArgumentException("Product with provided name already exists."); + } + + product.setName(body.getName()); + product.setCategory(body.getCategory()); + product.setPrice(body.getPrice()); + return product; + } + + public void deleteProduct(int id) { + Product product = productList.stream() + .filter(p -> p.getId() == id) + .findFirst().orElseThrow(() -> new NoSuchElementException("Product not found with id: " + id)); + + productList.remove(product); + } +}