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/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);
}
}
28 changes: 28 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,28 @@
package com.booleanuk.api.products;

public class Product {
private static int nextId = 1;

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

public Product(String name, String category, double price) {
this.id = 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 double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
44 changes: 44 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,44 @@
package com.booleanuk.api.products;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

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

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

@GetMapping
public ArrayList<Product> getAll() {
return this.productRepository.getAll();
}

@PostMapping("/auto")
public ResponseEntity<Product> createAutoProduct() {
Product autoProduct = new Product("Gaming Chair", "Furniture", 199.99);
Product savedProduct = productRepository.addProduct(autoProduct);
return ResponseEntity.status(HttpStatus.CREATED).body(savedProduct);
}

@DeleteMapping("/{id}")
public ResponseEntity<String> deleteProduct(@PathVariable int id) {
boolean deleted = productRepository.delete(id);
if (deleted) {
return ResponseEntity.status(HttpStatus.OK)
.body("Product with ID " + id + " was deleted successfully.");
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("Product with ID " + id + " not found.");
}
}



}
58 changes: 58 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,58 @@
package com.booleanuk.api.products;

import java.util.ArrayList;

public class ProductRepository {
private ArrayList<Product> products;

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

this.products.add(new Product("Laptop", "Electronics", 899.99));
this.products.add(new Product("T-shirt", "Clothing", 19.99));
this.products.add(new Product("Coffee Mug", "Kitchen", 5.49));
}

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

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

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

public Product update(int id, Product updatedProduct) {
Product existingProduct = getOne(id);
if (existingProduct == null) {
return null;
}
existingProduct.setName(updatedProduct.getName());
existingProduct.setCategory(updatedProduct.getCategory());
existingProduct.setPrice(updatedProduct.getPrice());
return existingProduct;
}

public boolean delete(int id) {
Product existingProduct = getOne(id);
if (existingProduct == null) {
return false;
}
this.products.remove(existingProduct);
return true;
}

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