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

public class Product {
private static int nextID = 1;

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

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


public int getId() {
return id;
}

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

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

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

@GetMapping
public List<Product> getAllProducts(@RequestParam(name = "category", required = false)String category ) {
List<Product> products;
if(category != null){
products = this.theProducts.getAll(category);
}
else{
products = this.theProducts.getAll();
}


if (products.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
return products;
}


@GetMapping("/{id}")
public Product getOneProduct(@PathVariable(name = "id") int id) {

if (this.theProducts.getOne(id) == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
return this.theProducts.getOne(id);
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product updateProduct(@PathVariable(name = "id") int id, @RequestBody Product newProduct){

if(this.theProducts.chechNameAvailable(newProduct.getName(), id)){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Name already exists");
}
if (this.theProducts.updateProduct(id, newProduct) == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
return newProduct;
}

@DeleteMapping("/{id}")
public Product removeProduct(@PathVariable(name = "id") int id){

Product product = this.theProducts.removeProduct(id);
if (product == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
return product;
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product createProduct(@RequestBody Product product){

if(this.theProducts.chechNameAvailable(product.getName())){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Name already exists");
}
if (this.theProducts.createProduct(product) == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
return product;
}
}
84 changes: 84 additions & 0 deletions src/main/java/com/booleanuk/api/Product/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.booleanuk.api.Product;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class ProductRepository {

private ArrayList<Product> products;

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

this.products.add(new Product("Volvo c30 R-design", "Car", 100000));
this.products.add(new Product("Nintendo", "Game console", 4000));
}

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

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

public Product updateProduct(int id, Product updatedProduct){
for(Product product : products){
if(product.getId() == id){
product.setName(updatedProduct.getName());
product.setCategory(updatedProduct.getCategory());
product.setPrice(updatedProduct.getPrice());
return product;
}
}
return null;
}

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

public Product createProduct(Product product){
products.add(product);
return product;
}

public Boolean chechNameAvailable(String name){
for (Product product : products){
if(product.getName().equals(name)){
return true;
}
}
return false;
}

public Boolean chechNameAvailable(String name, int id){
for (Product product : products){
if(product.getName().equals(name) && product.getId() != id){
return true;
}
}
return false;
}

public List<Product> getAll(String category) {
List<Product> temp = new ArrayList<>();
temp = this.products.stream()
.filter(product -> product.getCategory().equals(category))
.toList();
return temp;
}
}
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.