diff --git a/gradlew.bat b/gradlew.bat index f127cfd..864b6cc 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,5 +1,5 @@ @rem -@rem Copyright 2015 the original author or authors. +@rem Copyright 2015 the original product or authors. @rem @rem Licensed under the Apache License, Version 2.0 (the "License"); @rem you may not use this file except in compliance with the License. diff --git a/src/main/java/com/booleanuk/api/Main.java b/src/main/java/com/booleanuk/api/Main.java new file mode 100644 index 0000000..4e437d6 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -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); + } +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/api/controller/ProductController.java b/src/main/java/com/booleanuk/api/controller/ProductController.java new file mode 100644 index 0000000..e15d34c --- /dev/null +++ b/src/main/java/com/booleanuk/api/controller/ProductController.java @@ -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 getAll() { + return this.authorDB.getAll(); + } + + @GetMapping("{/category}") + public List 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); + } +} diff --git a/src/main/java/com/booleanuk/api/model/Product.java b/src/main/java/com/booleanuk/api/model/Product.java new file mode 100644 index 0000000..be503d9 --- /dev/null +++ b/src/main/java/com/booleanuk/api/model/Product.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/api/model/ProductRepo.java b/src/main/java/com/booleanuk/api/model/ProductRepo.java new file mode 100644 index 0000000..def2424 --- /dev/null +++ b/src/main/java/com/booleanuk/api/model/ProductRepo.java @@ -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 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 getAll(){ + return products; + } + + public List 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()); + } +}