Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/com/booleanuk/api/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -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("*");
}
};
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/Main.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/booleanuk/api/products/Product.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/booleanuk/api/products/ProductController.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

}
68 changes: 68 additions & 0 deletions src/main/java/com/booleanuk/api/products/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> getAllProducts(String category) throws Exception {
List<Product> 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);
}
}