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);
}
}
65 changes: 65 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,65 @@
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.List;

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

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

@GetMapping
public List<Product> getAll(@RequestParam(name = "category", required = false, defaultValue = "") String category){
if(category.isEmpty())
return this.theProducts.findAll();
List<Product> products = this.theProducts.findAll(category);
if (products.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of that category");
}
return products;
}


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

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product post(@RequestBody Product product){
if (this.theProducts.addProduct(product))
return product;
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product update(@PathVariable (name = "id") int id, @RequestBody Product product) {
Product pr = theProducts.update(id, product);
if (pr == null)
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
return pr;
}

@DeleteMapping("/{id}")
public Product delete(@PathVariable (name = "id") int id) {
Product pr = this.theProducts.delete(id);
if (pr == null)
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
return pr;
}
}
46 changes: 46 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,46 @@
package com.booleanuk.api.models;

public class Product {
private static int nextId;

private int id;
private String name;
private String category;
private int price;

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

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;
}

public int getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.booleanuk.api.repositories;

import com.booleanuk.api.models.Product;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

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

public class ProductRepository {
List<Product> products;

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

products.add(new Product("How to buil APIs", "Book", 1500));
}

public boolean addProduct(Product product){
for(Product pr: products){
if (pr.getName().equals(product.getName()))
return false;
}
products.add(product);
return true;
}

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

public List<Product> findAll(String category) {
List<Product> foundPr = new ArrayList<>();
for (Product pr: products){
if (pr.getCategory().equals(category))
foundPr.add(pr);
}
return foundPr;
}

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

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

public Product update(int id, Product product ){
for (Product pr: products){
if (pr.getName().equals(product.getName()))
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
Product product1 = find(id);
if (product1 != null){
product1.setName(product.getName());
product1.setCategory(product.getCategory());
product1.setPrice(product.getPrice());
return product1;
}
return null;
}
}