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

public class Product {
private int id;
private String name;
private String category;
private Integer price;

public Product(int id, String name, String category, Integer price){
this.id = id;
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 Integer getPrice() { return price; }

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

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.booleanuk.api.Product.ProductRepository;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;

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

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

@GetMapping
public List<Product> getAll() {
List<Product> products = this.repository.findAll();
if (products.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found");
}
return products;
}


@PostMapping
public Product addProduct(@RequestBody Product product){
List<Product> items = this.getAll();

boolean exists = items.stream()
.anyMatch(item -> item.getName().equals(product.getName()));

if (exists) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists.");
}

return this.repository.create(product.getName(), product.getCategory(), product.getPrice());
}

@GetMapping("{id}")
public Product findId(@PathVariable int id) {
Product product = this.repository.find(id);

if (product == null){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found");
}

return product;
}

@PutMapping("{id}")
public Product updateProduct(@PathVariable int id, @RequestBody Product product){

Product existing = this.repository.find(id);

if (existing == null){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found");
}

boolean nameExists = this.repository.findAll().stream()
.anyMatch(p -> !(p.getId() == id) && p.getName().equals(product.getName()));

if (nameExists) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists.");
}

return this.repository.updateProduct(id, product);
}

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

if (delete == null){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found");
}

return delete;
}

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

import java.util.*;

public class ProductRepository {
private int idCounter = 1;
private List<Product> data = new ArrayList<>();

public ProductRepository(){
this.data.add(new Product(0, "Eple", "Frukt", 10));
}


public Product create(String name, String category, int price) {
Product product = new Product(this.idCounter++, name, category, price);
this.data.add(product);
return product;
}

public List<Product> findAll() { return this.data; }

public Product find (int id) {
return this.data.stream()
.filter(product -> product.getId() == id)
.findFirst()
.orElse(null);
}

public Product deleteProduct(int id){
Product product = this.find(id);
if (product == null) return null;
this.data.remove(product);
return product;
}

public Product updateProduct(int id, Product product){
Product product1 = this.find(id);
product1.setName(product.getName());
product1.setCategory(product.getCategory());
product1.setPrice(product.getPrice());
return product1;
}
}