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/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.booleanuk.api;

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

public Product(String name, String category, int price) {
this.name = name;
this.category = category;
this.price = price;
this.id = uniqueId;
uniqueId++;
}

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;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/booleanuk/api/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.booleanuk.api;

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

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

@GetMapping List<Product> getAll(@RequestParam(required = false) String category){
return this.productRepository.findAll(category);
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product create(@RequestBody Product product){
this.productRepository.create(product.getName(), product.getCategory(), product.getPrice());
return product;
}

@GetMapping("/{id}")
public Product getSpecificProduct(@PathVariable int id){
return this.productRepository.findSpecificProduct(id);
}

@DeleteMapping("/{id}")
public Product deleteProduct(@PathVariable int id){
return this.productRepository.delete(id);
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product updateProduct(@PathVariable int id, @RequestBody Product product){
Product newProduct = this.productRepository.update(id, product);

if(newProduct == null){
for (int i = 0; i < productRepository.getProducts().size(); i++){

}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "The product with the ID: " + id + " could not be found in repository");
}
return newProduct;
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/booleanuk/api/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.booleanuk.api;

import com.sun.net.httpserver.HttpsServer;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

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

public class ProductRepository {

private List<Product> products = new ArrayList<>();

public ProductRepository(){
products.add(new Product("Corn flakes", "Food", 20));
}

public void create(String name, String category, int price){
for(Product p: products){
if(p.getName().equals(name)){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product already exists in storage");
}
}
Product product = new Product(name,category,price);
this.products.add(product);
}

public List<Product> findAll(String category) {
//If no parameter, just return the entire list
if(category == null){
return products;
}
List<Product> categoryList = new ArrayList<>();
for(Product p: products){
if(p.getCategory().equalsIgnoreCase(category)){
categoryList.add(p);
}
}
if(categoryList.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Couldn't find any products in the given category");
}
return categoryList;
}

public Product update(int id, Product newProduct){
for(Product p: products){
if(p.getId() == id){
p.setName(newProduct.getName());
p.setCategory(newProduct.getCategory());
p.setPrice(newProduct.getPrice());
return newProduct;
}
}
return null;
}

public Product delete(int id){
for(Product p: products){
if(p.getId() == id){
products.remove(p);
return p;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "The product with the ID: " + id + " does not exist");
}

public Product findSpecificProduct(int id){
for(Product p: products){
if(p.getId() == id){
return p;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Couldn't find the product with ID: " + id);
}

public List<Product> getProducts() {
return products;
}
}