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/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.booleanuk;

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
@@ -1,4 +1,7 @@
package com.booleanuk.api.bagels;
package com.booleanuk.api.bagels.controller;

import com.booleanuk.api.bagels.model.pojo.Bagel;
import com.booleanuk.api.bagels.model.repository.BagelRepository;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.booleanuk.api.bagels;
package com.booleanuk.api.bagels.model.pojo;

public class Bagel {
private int id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.booleanuk.api.bagels;
package com.booleanuk.api.bagels.model.repository;

import com.booleanuk.api.bagels.model.pojo.Bagel;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.booleanuk.api.products.controller;

import com.booleanuk.api.products.controller.dto.ProductDto;
import com.booleanuk.api.products.model.pojo.Product;
import com.booleanuk.api.products.model.repository.ProductRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

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

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

// Get all
@GetMapping
public ResponseEntity<List<Product>> getAll() {
return ResponseEntity.ok(this.productRepository.findAll());
}

// Get by category
@GetMapping(params = "category")
public ResponseEntity<?> getByCategory(@RequestParam String category) {
try {
return ResponseEntity.ok(this.productRepository.findAllByCategory(category));
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(e.getMessage());
}
}

// Get by id
@GetMapping("/{id}")
public ResponseEntity<?> getById(@PathVariable int id) {
try {
return ResponseEntity.ok(productRepository.findById(id));
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(e.getMessage());
}
}

// Create
@PostMapping
public ResponseEntity<?> createProduct(@RequestBody ProductDto productCreateDto) {
try {
return ResponseEntity.status(HttpStatus.CREATED).body(productRepository.create(new Product(productCreateDto)));
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(e.getMessage());
}
}

// Update by id
@PutMapping("/{id}")
public ResponseEntity<?> updateProductId(@PathVariable int id, @RequestBody ProductDto body) {
try {
return ResponseEntity.status(HttpStatus.CREATED).body(this.productRepository.updateById(id, body));
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(e.getMessage());
}
}

// Delete by id
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteProduct(@PathVariable int id) {
try {
return ResponseEntity.ok(productRepository.deleteById(id));
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(e.getMessage());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.booleanuk.api.products.controller.dto;

public class ProductDto {
private String name;
private String category;
private int price;

public ProductDto(String name, String category, int price) {
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;
}
}
66 changes: 66 additions & 0 deletions src/main/java/com/booleanuk/api/products/model/pojo/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.booleanuk.api.products.model.pojo;

import com.booleanuk.api.products.controller.dto.ProductDto;

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

public Product() {

}

public Product(String name, String category, int price) {
setId(nextId++);
setName(name);
setCategory(category);
setPrice(price);
}

public Product(ProductDto productCreateDto) {
this(productCreateDto.getName(), productCreateDto.getCategory(), productCreateDto.getPrice());
}

public static int getNextId() {
return nextId;
}

public static void setNextId(int nextId) {
Product.nextId = nextId;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.booleanuk.api.products.model.repository;

import com.booleanuk.api.products.controller.dto.ProductDto;
import com.booleanuk.api.products.model.pojo.Product;

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

public class ProductRepository {
private List<Product> products = new ArrayList<>();

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

// Find all by category
public List<Product> findAllByCategory(String category) throws Exception {
List<Product> filtered = findAll().stream()
.filter(p -> category.equalsIgnoreCase(p.getCategory()))
.toList();

if (filtered.isEmpty()) {
throw new Exception("No products in the provided category were found");
}

return filtered;
}

// Find by id
public Product findById(int id) throws Exception {
return products.stream()
.filter(p -> p.getId() == id)
.findFirst()
.orElseThrow(() -> new Exception("Not found"));
}

// Create
public Product create(Product product) throws Exception {
List<Product> filtered = products
.stream()
.filter(p -> p.getName().equals(product.getName()))
.toList();

if (filtered.isEmpty()) {
Product newProduct = new Product(product.getName(), product.getCategory(), product.getPrice());
products.add(newProduct);
return product;
}

throw new Exception("Product with provided name already exists");
}

// Update by id
public Product updateById(int id, ProductDto body) throws Exception {
// Will throw if product with id not found
Product currentProduct = findById(id);

// Check if name already in other product
List<Product> filtered = products.stream()
.filter(p -> p.getName().equals(body.getName()))
.toList();
if (!filtered.isEmpty()) {
throw new Exception("Product with provided name already exists");
}
currentProduct.setName(body.getName());
currentProduct.setCategory(body.getCategory());
currentProduct.setPrice(body.getPrice());
return currentProduct;
}

// Delete by id
public Product deleteById(int id) throws Exception {
Product productToDelete = findById(id);
products = products.stream().filter(p -> p.getId() != id).toList();
return productToDelete;
}

}