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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly 'org.projectlombok:lombok:1.18.34'
annotationProcessor 'org.projectlombok:lombok'
}

tasks.named('test') {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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);
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/booleanuk/api/Products/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.booleanuk.api.Products;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Data
@AllArgsConstructor

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

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

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("products")
public class ProductController {
ProductRepository repository;
public ProductController(ProductRepository repository) {
this.repository = repository;
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<Product> getAll(@RequestParam(value = "category", required = false) String category) {
if (category != null) {
List<Product> products = this.repository.getAllByCategory(category);
if (products.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found.");
}
return products;
}
return this.repository.getAll();
}


@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public Product getById(@PathVariable("id") int id) {
Product product = this.repository.find(id);
return product;
}


@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Product create(@RequestBody Product product) {
return this.repository.create(product.getName(), product.getCategory(), product.getPrice());
}



@PutMapping("/{id}")
@ResponseStatus(HttpStatus.CREATED)
public Product putProduct(@PathVariable("id") int id, @RequestBody Product updatedProduct ) {
if(this.repository.find(updatedProduct.getName()).isPresent()){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists.");
}else{
Product product = this.repository.find(id);
if(product != null){
product.setName(updatedProduct.getName());
product.setCategory(updatedProduct.getCategory());
product.setPrice(updatedProduct.getPrice());
return product;
}else{
return null;
}
}

}

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



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





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

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

@Repository
public class ProductRepository {
private int idCounter = 1;
private List<Product> data = new ArrayList<>();
public Product create(String name, String category, int price) {
Product product = new Product(this.idCounter++, name, category , price);
this.data.add(product);
return product;
}
public List<Product> getAll() {
return this.data;
}
public List<Product> getAllByCategory(String category) {
List<Product> productsMatching = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
if(data.get(i).getCategory().equals(category)){
productsMatching.add(data.get(i));
}
}
return productsMatching;
}

public Product find(int id) {
return this.data.stream()
.filter(product -> product.getId() == id)
.findFirst()
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."));
}
public Optional<Product> find(String name) {
return this.data.stream()
.filter(product -> product.getName().equals(name))
.findFirst();
}

public Product delete(int id) {
for (int i = 0; i < this.data.size(); i++) {
if(this.data.get(i).getId() == id){
Product deletedProduct = this.data.get(i);
this.data.remove(i);
return deletedProduct;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found.");
}
}