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

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

import java.util.ArrayList;

@RestController
@RequestMapping("products")
public class ProductController {

private ProductRepository productRepository;

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

@GetMapping
public ArrayList<Product> getAll() {
return productRepository.findAll();
}

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

return product;
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product create(@RequestBody Product product) {
if (productRepository.getProducts().contains(product)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Not cool.");
}
productRepository.create(product);
return product;
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product update(@PathVariable (name = "id") int id, @RequestBody Product product) {
if (id < productRepository.getProducts().size()) {
productRepository.getProducts().get(id).setType(product.getType());
productRepository.getProducts().get(id).setCategory(product.getCategory());
productRepository.getProducts().get(id).setPrice(product.getPrice());
return productRepository.getProducts().get(id);
}
return null;
}

@DeleteMapping("/{id}")
public Product delete(@PathVariable (name = "id") int id) {
if (id < productRepository.getProducts().size()) {
Product remove = productRepository.find(id);
productRepository.updateId(id);
return productRepository.getProducts().remove(remove.getId());
}
return null;
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/booleanuk/api/models/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.booleanuk.api.models;

public class Product {

private static int nextId = 0;
private int id;
private String type;
private String category;
private int price;

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

public static int getNextId() {
return nextId;
}

public static void setNextId(int nextId) {
Product.nextId = nextId;
}

public int getId() {

return id;
}

public String getType() {

return type;
}

public int getPrice() {

return price;
}

public void setType(String type) {
this.type = type;
}

public void setId(int id) {
this.id = id;
}

public String getCategory() {
return category;
}

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

public void setPrice(int price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.booleanuk.api.repositories;

import com.booleanuk.api.bagels.Bagel;
import com.booleanuk.api.models.Product;

import java.util.ArrayList;

public class ProductRepository {

private ArrayList<Product> products;
private int id;

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

//this.products.add(new Product(this.id++, "Bagel", "Food", 20));
//this.products.add(new Product(this.id ++, "Coffee", "Food", 15));
}

public void create(Product product) {
this.products.add(product);
}

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

public void setProducts(ArrayList<Product> products) {
this.products = products;
}

public ArrayList<Product> findAll() {
return this.products;
}

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

public void updateId(int id) {
for (int i = id; i < products.size() - 1; i ++) {
products.get(i+1).setId(i);
}
}
}