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
2 changes: 1 addition & 1 deletion gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

import com.booleanuk.api.model.Product;
import com.booleanuk.api.model.ProductRepo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("products")
public class ProductController {
private final ProductRepo authorDB;

public ProductController(){
this.authorDB = new ProductRepo();
}

@GetMapping("")
public List<Product> getAll() {
return this.authorDB.getAll();
}

@GetMapping("{/category}")
public List<Product> getByCategory(@RequestParam String category) {
return this.authorDB.getByCategory(category);
}

@GetMapping("/{id}")
public Product getForID(@PathVariable (name="id") int id){
return this.authorDB.getForID(id);
}

@PutMapping("/{id}")
public Product updateForID(@PathVariable (name="id") int id, @RequestBody Product product){
return this.authorDB.update(id, product);
}

@DeleteMapping("/{id}")
public HttpStatus deleteForID(@PathVariable (name="id") int id){
this.authorDB.delete(id);
return HttpStatus.OK;
}

@PostMapping()
public Product create(@RequestBody Product product){
return this.authorDB.create(product);
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/booleanuk/api/model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.booleanuk.api.model;

public class Product {
private static int nextID = 0;

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 void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public void update(Product product, int id){
this.category = product.getCategory();
this.name = product.getName();
this.id = id;
this.price = product.getPrice();
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/booleanuk/api/model/ProductRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.booleanuk.api.model;

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

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

public class ProductRepo {
private ArrayList<Product> products;

public ProductRepo(){
this.products = new ArrayList<>();
this.products.add(new Product("Norway's greatest hits", "CD", 2));
this.products.add(new Product("Nina Jirachi - Fk My Computer", "Vinyl", 999));
}

public List<Product> getAll(){
return products;
}

public List<Product> getByCategory(String category){
return products.stream().filter(product -> Objects.equals(category, product.getCategory())).toList();
}

public Product getForID(int id){
Product product = null;
try {
product = this.products.get(id);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
return product;
}

public Product update(int id, Product product){
Product productToUpdate = null;
try {
productToUpdate = this.products.get(id);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}

productToUpdate.update(product, id);
return productToUpdate;
}

public void delete(int id){
try {
this.products.remove(id);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
}
}

public Product create(Product product){
this.products.add(product);
return this.getForID(product.getId());
}
}