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);
}
}
45 changes: 45 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,45 @@
package com.booleanuk.api.products;

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

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

public int getId() {
return 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;
}
}
72 changes: 72 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,72 @@
package com.booleanuk.api.products;

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

import java.nio.channels.ScatteringByteChannel;
import java.util.List;
import java.util.NoSuchElementException;

@RestController
@RequestMapping("products")

public class ProductController {
private ProductRepository theProducts;

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

@GetMapping
public List<Product> getAll(@RequestParam(required = false) String category) {
if (this.theProducts.getAll(category) == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found!");
}
return this.theProducts.getAll(category);
}

@PostMapping
public Product create(@RequestBody Product product) {
Product toReturn = this.theProducts.createProduct(product);
if (toReturn == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Already exists!");
}
return toReturn;
}

//try-catch not needed at all but I just wanted to try it
@GetMapping("/{id}")
public Product getOne(@PathVariable(name = "id") int id) {
try {
return this.theProducts.getOneProduct(id);
} catch (NoSuchElementException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
}
}

@PutMapping("/{id}")
public Product update(@PathVariable(name = "id") int id, @RequestBody Product product) {
try {
Product toReturn = this.theProducts.updateProduct(id, product);
if (toReturn == null) {
throw new IllegalArgumentException("Item already exists");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this IF statement and the corresponding exception is correct...

}
return toReturn;
} catch (NoSuchElementException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
} catch (IllegalArgumentException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
}


@DeleteMapping("/{id}")
public Product delete(@PathVariable(name = "id") int id) {
try {
return this.theProducts.deleteProduct(id);
} catch (NoSuchElementException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
}
}
}
81 changes: 81 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,81 @@
package com.booleanuk.api.products;

import jdk.jshell.spi.ExecutionControl;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.server.ResponseStatusException;

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

public class ProductRepository {
private List<Product> products;

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

public Product createProduct(Product product) {
for (Product sameNameProduct : this.products) {
if (product.getName().equals(sameNameProduct.getName())) {
return null;
}
}
this.products.add(product);
return product;
}

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


public Product getOneProduct(int id) throws NoSuchElementException {
return this.products.stream()
.filter(product -> product.getId() == id)
.findFirst()
.orElseThrow(NoSuchElementException::new);
}

public Product updateProduct(int id, Product product) {
try {
Product productToUpdate = this.getOneProduct(id);
for (Product sameNameProduct : this.products) {
if (product.getName().equals(sameNameProduct.getName())) {
return null;
}
}
productToUpdate.setName(product.getName());
productToUpdate.setCategory(product.getCategory());
productToUpdate.setPrice(product.getPrice());
return productToUpdate;
} catch (NoSuchElementException e) {
throw e;
}
}


public Product deleteProduct(int id) {
try {
Product productToDelete = this.getOneProduct(id);
this.products.remove(productToDelete);
return productToDelete;
} catch (NoSuchElementException e) {
throw e;
}
}

}