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
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/bagels/product/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.booleanuk.api.bagels.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);
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/booleanuk/api/bagels/product/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.booleanuk.api.bagels.product;

public class Product {
private static int nextId = 1;
private int id;
private String name;
private String category;
private int price;

public Product(String name, String category, int price) {
this.id = nextId;
nextId++;
this.name = name;
this.category = category;
this.price = 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;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.booleanuk.api.bagels.product;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.ArrayList;

@RestController
@RequestMapping("products")
public class ProductController {
private ProductRepository theProducts;

public ProductController() {
this.theProducts = new ProductRepository();
}

@GetMapping
@ResponseBody
public ArrayList<Product> getAllProducts(@RequestParam(required = false) String category) {
ArrayList<Product> newList = this.theProducts.getAll(category);
if(newList.isEmpty() && category != null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found.");
}
return newList;
}

@GetMapping("{id}")
public Product getOneProduct(@PathVariable int id) {
Product theProduct = this.theProducts.getOne(id);
if(theProduct == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found.");
}
return theProduct;
}

@PostMapping
public Product createProduct(@RequestBody Product product) {
Product newProduct = this.theProducts.create(product);
if(newProduct == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists.");
}
return newProduct;
}

@PutMapping("{id}")
public Product updateProduct(@PathVariable int id, @RequestBody Product product) {

if(!this.theProducts.checkIfNameIsUnique(product)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists");
}

Product theProduct = this.theProducts.update(id, product);
if(theProduct == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found.");
}
return theProduct;
}

@DeleteMapping("{id}")
public Product deleteProduct(@PathVariable int id) {
Product theProduct = this.theProducts.delete(id);
if(theProduct == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return theProduct;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.booleanuk.api.bagels.product;

import java.util.ArrayList;

public class ProductRepository {
private ArrayList<Product> products;

public ProductRepository() {
this.products = new ArrayList<>();
}

public ArrayList<Product> getAll(String category) {
if(category == null) {
return this.products;
}
ArrayList<Product> newList = new ArrayList<>();
for(Product p : this.products) {
if(p.getCategory().equalsIgnoreCase(category)) {
newList.add(p);
}
}
return newList;
}

public Product getOne(int id) {
for(Product product : this.products) {
if(product.getId() == id) {
return product;
}
}
return null;
}

public Product create(Product product) {
for(Product p : this.products) {
if(p.getName().equalsIgnoreCase(product.getName())) {
return null;
}
}
this.products.add(product);
return product;
}

public Product update(int id, Product product) {

Product theProduct = this.getOne(id);
if(theProduct == null) {
return theProduct;
}
theProduct.setName(product.getName());
theProduct.setCategory(product.getCategory());
theProduct.setPrice(product.getPrice());
return theProduct;
}

public boolean checkIfNameIsUnique(Product product) {
for(Product p : this.products) {
if(p.getName().equalsIgnoreCase(product.getName())) {
return false;
}
}
return true;
}

public Product delete(int id) {
Product theProduct = this.getOne(id);
if(theProduct == null) {
return theProduct;
}
this.products.remove(theProduct);
return theProduct;
}
}