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

This file was deleted.

15 changes: 0 additions & 15 deletions src/main/java/com/booleanuk/api/bagels/BagelController.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/main/java/com/booleanuk/api/bagels/BagelRepository.java

This file was deleted.

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

public class Product {
private static int nextId = 1;
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 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;
}
}
81 changes: 81 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,81 @@
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> listByCategory = this.theProducts.getAllProductsByCategory(category);
if(listByCategory.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There are no products that have the entered category!");
}
return listByCategory;
}


@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product createProduct(@RequestBody Product product){
Product productToCreate = this.theProducts.isNameAlreadyContainedInList(product);
if(productToCreate == null){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with the same name already exists in the list!");
}

return this.theProducts.create(product);
}


@GetMapping("{id}")
public Product getProductById(@PathVariable int id){
Product productToGet = this.theProducts.getProductById(id);
if(productToGet == null){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There is no product with the entered ID!");
}
return productToGet;
}


@PutMapping("{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product updateProductById(@PathVariable int id, @RequestBody Product product){
Product aProduct = this.theProducts.getProductById(id);
if(aProduct == null){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There is no product with the entered ID!");
}

aProduct = this.theProducts.isNameAlreadyContainedInList(id, product);
if(aProduct == null){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with the same name already exists in the list!");
}

return this.theProducts.updateProductById(id, product);
}


@DeleteMapping("{id}")
public Product deleteProductById(@PathVariable int id){
Product productToDelete = this.theProducts.getProductById(id);
if(productToDelete == null){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "There is no product with the entered ID!");
}

return this.theProducts.deleteProductById(id);
}
}
84 changes: 84 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,84 @@
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<>(){{
add(new Product("Harry Potter and the goblet of fire", "Book", 10));
add(new Product("Antilop", "Chair", 59));
add(new Product("Hobbit", "Book", 15));
}};
}

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

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

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

public Product updateProductById(int id, Product productToUpdate){
for(Product aProduct : this.products){
if(aProduct.getId() == id){
aProduct.setName(productToUpdate.getName());
aProduct.setCategory(productToUpdate.getCategory());
aProduct.setPrice(productToUpdate.getPrice());

return aProduct;
}
}
return null;
}

public Product deleteProductById(int id){
for(Product aProduct : this.products){
if(aProduct.getId() == id){
this.products.remove(aProduct);
return aProduct;
}
}
return null;
}

public Product isNameAlreadyContainedInList(Product productToCheck){
for(Product aProduct : this.products){
if(aProduct.getName().equals(productToCheck.getName())){
productToCheck = null;
return productToCheck;
}
}
return productToCheck;
}

public Product isNameAlreadyContainedInList(int id, Product productToCheck){
for(Product aProduct : this.products){
if(aProduct.getName().equals(productToCheck.getName()) && aProduct.getId() != id){
productToCheck = null;
return productToCheck;
}
}
return productToCheck;
}

public List<Product> getAllProductsByCategory(String category){
return this.products.stream()
.filter(s -> s.getCategory().equals(category)).toList();
}
}