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

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

public ProductCreateDto() {

}
public ProductCreateDto(String name, int id, int price, String category) {
setName(name);
setCategory(category);
setPrice(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;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.booleanuk.api.product.controller;

import com.booleanuk.api.product.controller.Dto.ProductCreateDto;
import com.booleanuk.api.product.model.pojo.Product;
import com.booleanuk.api.product.model.repository.ProductRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

private ProductRepository productRepository;

public ProductController() {
productRepository = new ProductRepository();

}

@GetMapping
public List<Product> getProducts(@RequestParam(required = false) String category) {
return productRepository.getProducts(category);
}

@GetMapping("/{id}")
public Product getProductById(@PathVariable int id) {
return productRepository.getOne(id);
}


@PostMapping()
public ResponseEntity<Product> createProduct(@RequestBody ProductCreateDto productCreateDto) {
Product product = new Product(productCreateDto);

productRepository.createProduct(product);

return ResponseEntity.status(HttpStatus.CREATED).body(product);
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product update(@PathVariable int id, @RequestBody ProductCreateDto body){
return productRepository.updateProduct(id, body);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public Product delete(@PathVariable int id) {
return productRepository.deleteProduct(id);
}


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

import com.booleanuk.api.product.controller.Dto.ProductCreateDto;

public class Product {
private static int nextId = 1;

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

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

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

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,85 @@
package com.booleanuk.api.product.model.repository;

import com.booleanuk.api.product.controller.Dto.ProductCreateDto;
import com.booleanuk.api.product.model.pojo.Product;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

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

public class ProductRepository {
private List<Product> products;

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

this.products.add(new Product("Spoon", "Kitchen", 100));
this.products.add(new Product("Laptop", "Electronics", 10000));
this.products.add(new Product("Book", "Education", 150));

}

public List<Product> getProducts(String category) {
if (category == null || category.isBlank()) {
return this.products;
}
List<Product> result = new ArrayList<>();
for (Product p : products){
if (p.getCategory().equalsIgnoreCase(category)) {
result.add(p);
}
}

if (result.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found.");
}

return result;
}

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 void createProduct(Product product) {
for (Product p : this.products) {
if (p.getName().equalsIgnoreCase((product.getName()))) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists");
}

}
products.add(product);
}

public Product updateProduct(int id, ProductCreateDto body) {
for (Product p : this.products) {
if (p.getId() == id) {
p.setName(body.getName());
p.setPrice(body.getPrice());
p.setCategory(body.getCategory());
return p;
}
}

throw new ResponseStatusException(HttpStatus.NOT_FOUND, "product not found");
}

public Product deleteProduct(int id) {
for (Product p : this.products){
if (p.getId() == id) {
this.products.remove(p);
return p;
}
}

throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found");
}
}