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

public class Product {
private static int nextId = 1;
private final int id;
private String name;
private String category;
private int price;

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

public int getId(){
return this.id;
}

public String getName(){
return this.name;
}

public void setName(String newName){
this.name = newName;
}

public String getCategory(){
return this.category;
}

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

public int getPrice(){
return this.price;
}

public void setPrice(int newPrice){
this.price = newPrice;
}
}
45 changes: 45 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,45 @@
package com.booleanuk.api.products;


import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("products")
public class ProductController {
private ProductRepository productRepo = new ProductRepository();

@GetMapping
public List<Product> getProducts(@RequestParam (required = false) String category){
if (category != null){
return this.productRepo.getByCategory(category);
}
return this.productRepo.getAll();
}

@GetMapping("/{id}")
public Product getOneProduct(@PathVariable(name = "id") int id){
return this.productRepo.getOne(id);
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product createOneProduct(@RequestBody Product product){
return this.productRepo.createProduct(product);
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product updateOneProduct(@PathVariable(name = "id") int id, @RequestBody Product product){
return this.productRepo.updateProduct(id, product);
}

@DeleteMapping("/{id}")
public Product deleteOneProduct(@PathVariable(name = "id") int id){
return this.productRepo.deleteProduct(id);
}


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


import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.ArrayList;

public class ProductRepository {

private ArrayList<Product> products = new ArrayList<>(){{
add(new Product("How to build APIs", "Book", 1500));
add(new Product("Elden Ring", "Game", 90));
}};

public Product createProduct(Product product){
for (Product p : products){
if (p.getName().equals(product.getName())){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provided name already exists!");
}
}
this.products.add(product);
return product;
}

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

public ArrayList<Product> getByCategory(String category){
ArrayList<Product> categoryList = new ArrayList<>();
for (Product p : products){
if (p.getCategory().toLowerCase().equals(category)){
categoryList.add(p);
}
}
if (!categoryList.isEmpty()){
return categoryList;
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found!");
}

public Product getOne(int id){
for (Product p : products){
if (p.getId() == id){
return p;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found!");
}

public Product updateProduct(int id, Product product){
for (Product p : products){
if (p.getId() == id){
for (Product ps : products){
if (ps.getName().equals(product.getName()) && ps != p){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists!");
}
}
p.setName(product.getName());
p.setCategory(product.getCategory());
p.setPrice(product.getPrice());
return p;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found!");
}

public Product deleteProduct(int id){
for (Product p : products){
if (p.getId() == id){
products.remove(p);
return p;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found!");
}

}