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/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 static int nextId = 0;
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 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;
}
}
70 changes: 70 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,70 @@
package com.booleanuk.api.products;

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 theProducts;

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

@GetMapping
public List<Product> getAllProducts(@RequestParam(name="category", required = false) String category) {
if (category == null) {
return this.theProducts.getAll();
}

List<Product> pCategory = this.theProducts.getProductsInCategory(category);
if (pCategory == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found.");
}
return pCategory;
}

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

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

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product updateAproduct(@PathVariable int id, @RequestBody Product product) {
if (theProducts.isNameInList(product)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists");
}
Product pToUpdate = this.theProducts.update(id, product);
if (pToUpdate == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found");
}
return product;
}

@DeleteMapping("/{id}")
public Product deleteProduct(@PathVariable int id) {
Product product = this.theProducts.delete(id);
if (product == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found");
}
return product;
}
}
75 changes: 75 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,75 @@
package com.booleanuk.api.products;

import java.util.ArrayList;
import java.util.List;

public class ProductRepository {
private List<Product> products;

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

this.products.add(new Product("Bagel", "Food", 10));
this.products.add(new Product("Tea", "Food", 12));
this.products.add(new Product("Harry Potter", "Book", 100));
}

public List<Product> getProductsInCategory(String category) {
List<Product> pCategory = new ArrayList<>();
for (Product product : this.products) {
if (product.getCategory().toLowerCase().equals(category)) {
pCategory.add(product);
}
}
if (!pCategory.isEmpty()) {
return pCategory;
}
return null;
}

public List<Product> getAll() {
return this.products;
}

public Product create(Product product) {
this.products.add(product);
return product;
}

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

public Product update(int id, Product product) {
Product pToUpdate = getOne(id);
if (pToUpdate != null) {
this.products.get(id).setName(product.getName());
this.products.get(id).setCategory(product.getCategory());
this.products.get(id).setPrice(product.getPrice());
return this.products.get(id);
}
return null;
}

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

public boolean isNameInList(Product product) {
for (Product pToFind : this.products) {
if (pToFind.getName().equals(product.getName())) {
return true;
}
}
return false;
}
}