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

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

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

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getCategory() {
return category;
}

public int getPrice() {
return price;
}

public void setName(String name) {
this.name = name;
}

public void setCategory(String category) {
this.category = category;
}

public void setPrice(int price) {
this.price = price;
}
}
77 changes: 77 additions & 0 deletions src/main/java/com/booleanuk/api/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.booleanuk.api;

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

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController
@RequestMapping("products")
public class ProductController {
private ProductRepository repo = new ProductRepository();

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

@GetMapping
public List<Product> getProducts(@RequestParam(required = false) String category) {
List<Product> result = new ArrayList<>();
if (category != null) {
result = repo.getCategory(category);
if (result == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"No products of the provided category were found.");
}
} else {
result = repo.getAll();
}
return result;
}

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

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product updateProduct(@PathVariable int id, @RequestBody Product updated) {
Product product = repo.update(id, updated);
if (product == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found.");
}
return product;
}

@DeleteMapping("/{id}")
public Product deleteProduct(@PathVariable int id) {
Product product = repo.delete(id);
if (product == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found.");
}
return product;
}

}
82 changes: 82 additions & 0 deletions src/main/java/com/booleanuk/api/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.booleanuk.api;

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

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

public class ProductRepository {
private List<Product> products = new ArrayList<>() {
{
add(new Product("How to build APIs", "Book", 1500));
add(new Product("Grey bat", "Sports", 3000));
}
};

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

List<Product> getCategory(String category) {
List<Product> filtered = new ArrayList<>();
for (Product p : products) {
if (p.getCategory().toLowerCase().equals(category.toLowerCase())) {
filtered.add(p);
}
}
// return null if no items with this category
if (filtered.size() == 0) {
return null;
}
return filtered;
}

Product getProduct(int id) {
for (Product p : products) {
if (p.getId() == id) {
return p;
}
}
return null;
}

Product create(Product product) {
// check if product with same name exists
for (Product p : products) {
if (p.getName().equals(product.getName())) {
return null;
}
}

products.add(product);
return product;
}

Product update(int id, Product updated) {
Product product = getProduct(id);
if (product != null) {

Choose a reason for hiding this comment

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

Nested code is quite hard to read, consider splitting out the conditional checks into their own private methods

// check if product with updated name already exists
for (Product p : products) {
if (p.getName().equals(updated.getName())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Product with provided name already exists.");
}
}
product.setName(updated.getName());
product.setCategory(updated.getCategory());
product.setPrice(updated.getPrice());
}
return product;
}

Product delete(int id) {
for (Product p : products) {
if (p.getId() == id) {
products.remove(p);
return p;
}
}
return null;
}
}