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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.booleanuk.api.bagels;
package com.booleanuk.api.bagel;

public class Bagel {
private int id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.booleanuk.api.bagels;
package com.booleanuk.api.bagel;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.booleanuk.api.bagels;
package com.booleanuk.api.bagel;

import java.util.ArrayList;
import java.util.List;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/product/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.booleanuk.api.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.booleanuk.api.product.controller;

import com.booleanuk.api.product.model.Product;
import com.booleanuk.api.product.model.ProductRepository;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.awt.color.ProfileDataException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

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

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

@GetMapping
@RequestMapping(method = RequestMethod.GET)
public List<Product> getAll(@RequestParam("category") Optional<String> category) {
if(category.isPresent()) {
if (productRepository.getAll() != null) {
List<Product> productsCategory = new ArrayList<>();
for (int i = 0; i < productRepository.getAll().size(); i++) {
if (productRepository.getAll().get(i).getCategory().equals(category.get())) {
productsCategory.add(productRepository.getAll().get(i));
}
}
if(productsCategory.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,"No products of the provided category were found");
}
return productsCategory;
}

Choose a reason for hiding this comment

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

This code can be simplified a lot further by using streams and filtering

throw new ResponseStatusException(HttpStatus.NOT_FOUND,"No products of the provided category were found");
}
if (productRepository.getAll() != null) {
return productRepository.getAll();
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found");
}

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

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

@PutMapping("/{id}")
public Product update(@PathVariable int id, @RequestBody Product product) {
if (productRepository.update(id,product) != null) {
return product;
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found.");
}

}

@DeleteMapping("/{id}")
public Product delete(@PathVariable int id) {
if (productRepository.delete(id) != null) {
return productRepository.getOne(id);
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND,"Product not found.");
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/booleanuk/api/product/model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.booleanuk.api.product.model;

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) {
nextId++;
this.id = 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.booleanuk.api.product.model;

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;

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

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

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

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

public Product create(Product product) {
for (Product value : products) {
if (value.getName().equals(product.getName())) {
return null;
}
}
products.add(product);
return product;
}

public Product update(int id, Product product) {
Product updatedProduct = this.getOne(id);
if (updatedProduct != null) {
if (updatedProduct.getName().equals(product.getName())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists.");
}
updatedProduct.setName(product.getName());
updatedProduct.setCategory(product.getCategory());
updatedProduct.setPrice(product.getPrice());
}
return updatedProduct;
}

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