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
7 changes: 7 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}

rootProject.name = 'jave.api.mvc.in.memory'
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/product/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.booleanuk.api.product;

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.booleanuk.api.product.controllers;

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

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

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

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

@GetMapping
public List<Product> getAll(@RequestParam(defaultValue="") String category){
if(!category.isEmpty()){
List<Product> filteredProd = this.productRepository.getAllByCategory(category);
if(filteredProd.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products with that category found");
}
return filteredProd;
}

List<Product> allProds = this.productRepository.getAll();
if(allProds.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products found");
}
return allProds;
}

@GetMapping("{id}")
public Product getById(@PathVariable int id){
Product prod = this.productRepository.getById(id);
nullCheck(prod, HttpStatus.NOT_FOUND, "Product not found.");
return prod;
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product addProduct(@RequestBody Product product){
if(productRepository.checkNameConflict(product)){
throw new ResponseStatusException(HttpStatus.CONFLICT, "Product with provided name already exists");
}
return this.productRepository.add(product);
}

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

if(productRepository.checkNameConflict(product)){
throw new ResponseStatusException(HttpStatus.CONFLICT, "Product with provided name already exists");
}

Product foundProd = this.productRepository.getById(id);
nullCheck(foundProd, HttpStatus.NOT_FOUND, "Product not found.");
foundProd.setName(product.getName());
foundProd.setCategory(product.getCategory());
foundProd.setPrice(product.getPrice());

return foundProd;
}

@DeleteMapping("{id}")
public Product deleteProduct(@PathVariable int id){
Product removed = this.productRepository.removeProduct(id);
nullCheck(removed, HttpStatus.NOT_FOUND, "Product not found.");
return removed;
}

private void nullCheck(Product product, HttpStatus status, String message){
if(product == null){
throw new ResponseStatusException(status, message);
}
}

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

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

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

public String getName() {
return name;
}

public String getCategory() {
return category;
}

public String getPrice() {
return price;
}

public int getId() {
return id;
}

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

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

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

import com.booleanuk.api.product.models.Product;

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

public class ProductRepository {
private List<Product> products;

public ProductRepository(List<Product> products) {
this.products = products;
}

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

public boolean checkNameConflict(Product product){
for(Product curr : this.products){
if(curr.getName().equals(product.getName())){
return true;
}
}
return false;
}

public Product add(Product product){ //Void for now, would return db return if connected to db
this.products.add(product);
return product;
}

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

public Product removeProduct(int id){
Product removeProd = this.getById(id);
this.products.remove(removeProd);
return removeProd; //Return null if not found
}

public List<Product> getAllByCategory(String cat){
List<Product> found = new ArrayList<>();
for(Product curr : this.products){
if(cat.equals(curr.getCategory())){
found.add(curr);
}
}
return found;

}
}