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
25 changes: 0 additions & 25 deletions src/main/java/com/booleanuk/api/bagels/Bagel.java

This file was deleted.

15 changes: 0 additions & 15 deletions src/main/java/com/booleanuk/api/bagels/BagelController.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/main/java/com/booleanuk/api/bagels/BagelRepository.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/java/com/booleanuk/api/products/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.booleanuk.api.products;

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,42 @@
package com.booleanuk.api.products.controller;

import com.booleanuk.api.products.model.Product;
import com.booleanuk.api.products.model.ProductRepository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

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

@GetMapping
public List<Product> getAll(@RequestParam(required = false) String category) {
return this.repository.findAll(category);
}

@GetMapping("{id}")
public Product getById(@PathVariable int id) {
return repository.find(id);
}

@PostMapping
public Product createProduct(@RequestBody Product newProduct){
return repository.addProduct(newProduct);
}

@PutMapping("{id}")
public Product updateProduct(@PathVariable int id, @RequestBody String name){
return repository.updateProduct(id, name);
}

@DeleteMapping("{id}")
public boolean deleteProduct(@PathVariable int id){
return repository.deleteProduct(id);
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/booleanuk/api/products/model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.booleanuk.api.products.model;

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

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

public int getId() {
return id;
}

public String getCategory() {
return category;
}

public int getPrice() {
return price;
}

public String getName(){ return name; }

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

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,69 @@
package com.booleanuk.api.products.model;

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

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

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

public ProductRepository() {
Product product1 = new Product(this.idCounter++, "car", "utilities", 10000);
Product product2 = new Product(this.idCounter++, "house", "utilities", 200000);
Product product3 = new Product(this.idCounter++, "beer", "drink", 5);
this.data.add(product1);
this.data.add(product2);
this.data.add(product3);
}

public List<Product> findAll(String category) {
if (category != null && !category.isEmpty()){
List<Product> ls = this.data.stream().filter(n -> n.getCategory().equals(category)).toList();
if (ls.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found.");
}
return ls;
}
return this.data;
}

public Product find(int id) {
List<Product> product = this.data.stream()
.filter(prod -> prod.getId() == id)
.toList();
if (product.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found.");
}
return product.getFirst();
}

public Product addProduct(Product product){
if (data.stream().filter(it -> it.getName().equals(product.getName())).toList().isEmpty()){
this.data.add(product);
throw new ResponseStatusException(HttpStatus.CREATED, "Create a new product");
}
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists.");
}

public Product updateProduct(int id, String name){
Product product = find(id);
List<Product> existingProduct = data.stream().filter(it -> it.getName().equals(name)).toList();

if (!existingProduct.isEmpty()){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists.");
}
product.setName(name);
throw new ResponseStatusException(HttpStatus.CREATED, "Product successfully updated.");
}

public boolean deleteProduct(int id){
Product product = find(id);
this.data.remove(product);
throw new ResponseStatusException(HttpStatus.OK, "Product successfully deleted.");
}
}