From bb2b4dccd323671a26a4be5abbc96125ebbae132 Mon Sep 17 00:00:00 2001 From: Dave Ames Date: Tue, 16 Jul 2024 15:48:48 +0100 Subject: [PATCH] core complete and extenstions completed --- build.gradle | 17 ++- .../booleanuk/api/bagels/BagelController.java | 26 ++-- .../booleanuk/api/bagels/BagelRepository.java | 36 ++++-- .../java/com/booleanuk/api/bagels/Main.java | 14 +++ .../com/booleanuk/api/bagels/Product.java | 57 +++++++++ .../api/bagels/ProductController.java | 84 +++++++++++++ .../api/bagels/ProductRepository.java | 114 ++++++++++++++++++ 7 files changed, 321 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/booleanuk/api/bagels/Main.java create mode 100644 src/main/java/com/booleanuk/api/bagels/Product.java create mode 100644 src/main/java/com/booleanuk/api/bagels/ProductController.java create mode 100644 src/main/java/com/booleanuk/api/bagels/ProductRepository.java diff --git a/build.gradle b/build.gradle index 9e47655..d1f7789 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,17 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.0.4' - id 'io.spring.dependency-management' version '1.1.0' + id 'org.springframework.boot' version '3.3.1' + id 'io.spring.dependency-management' version '1.1.5' } -group = 'com.booleanuk.api' +group = 'com.example' version = '0.0.1-SNAPSHOT' -sourceCompatibility = '17' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} repositories { mavenCentral() @@ -14,9 +19,11 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' + developmentOnly 'org.springframework.boot:spring-boot-devtools' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } tasks.named('test') { useJUnitPlatform() -} +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/api/bagels/BagelController.java b/src/main/java/com/booleanuk/api/bagels/BagelController.java index cce2764..6ee7d35 100644 --- a/src/main/java/com/booleanuk/api/bagels/BagelController.java +++ b/src/main/java/com/booleanuk/api/bagels/BagelController.java @@ -1,15 +1,23 @@ package com.booleanuk.api.bagels; import java.util.List; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; -public class BagelController { - BagelRepository repository; - - public BagelController(BagelRepository repository) { - this.repository = repository; - } - public List getAll() { - return this.repository.findAll(); - } +//@RestController +//@RequestMapping("bagel") +public class BagelController { +// BagelRepository repository; +// +// public BagelController(BagelRepository repository) { +// this.repository = repository; +// } +// +// +//// @GetMapping +// public List getAll() { +// return this.repository.findAll(); +// } } diff --git a/src/main/java/com/booleanuk/api/bagels/BagelRepository.java b/src/main/java/com/booleanuk/api/bagels/BagelRepository.java index 320ddba..1f5f752 100644 --- a/src/main/java/com/booleanuk/api/bagels/BagelRepository.java +++ b/src/main/java/com/booleanuk/api/bagels/BagelRepository.java @@ -1,25 +1,35 @@ package com.booleanuk.api.bagels; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + import java.util.ArrayList; import java.util.List; + +//@RestController +//@RequestMapping("authors") public class BagelRepository { private int idCounter = 1; private List data = new ArrayList<>(); - public void create(String type, int price) { - Bagel bagel = new Bagel(this.idCounter++, type, price); - this.data.add(bagel); - } - public List findAll() { - return this.data; - } +// @PostMapping +// @ResponseStatus(HttpStatus.CREATED) +// public void create(@RequestBody String type,@RequestBody int price) { +// Bagel bagel = new Bagel(this.idCounter++, type, price); +// this.data.add(bagel); +// } - public Bagel find(int id) { - return this.data.stream() - .filter(bagel -> bagel.getId() == id) - .findFirst() - .orElseThrow(); - } +// public List findAll() { +// return this.data; +// } +// +// @GetMapping("/{id}") +// public Bagel find(int id) { +// return this.data.stream() +// .filter(bagel -> bagel.getId() == id) +// .findFirst() +// .orElseThrow(); +// } } diff --git a/src/main/java/com/booleanuk/api/bagels/Main.java b/src/main/java/com/booleanuk/api/bagels/Main.java new file mode 100644 index 0000000..02b0f45 --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/Main.java @@ -0,0 +1,14 @@ +package com.booleanuk.api.bagels; + + +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); + + + } +} diff --git a/src/main/java/com/booleanuk/api/bagels/Product.java b/src/main/java/com/booleanuk/api/bagels/Product.java new file mode 100644 index 0000000..adae86e --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/Product.java @@ -0,0 +1,57 @@ +package com.booleanuk.api.bagels; + +public class Product { + public static Integer nextID = 0; + private String name; + private Integer price; + private String category; + private int id; + + public Product(String name, Integer price, String category) { + this.name = name; + this.price = price; + this.category = category; + this.id = nextID++; + } + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public static Integer getNextID() { + return nextID; + } + + public static void setNextID(Integer nextID) { + Product.nextID = nextID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } +} diff --git a/src/main/java/com/booleanuk/api/bagels/ProductController.java b/src/main/java/com/booleanuk/api/bagels/ProductController.java new file mode 100644 index 0000000..a3f6624 --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/ProductController.java @@ -0,0 +1,84 @@ + +package com.booleanuk.api.bagels; + +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + + +@RestController +@RequestMapping("product") +public class ProductController { + ProductRepository repository; + + public ProductController() { + this.repository = new ProductRepository(); + } + + + @GetMapping + public List getAll() { + return this.repository.findAll(); + } + + @GetMapping("/{id}") + public Product findID(@PathVariable int id) { + Product product = this.repository.find(id); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + return product; + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product create(@RequestBody Product product){ + Product response = this.repository.create(product); + if (response != null){ + return response; + } + + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "It is a bad psot request"); + + + + } + + + @PutMapping("/{id}") + @ResponseStatus(HttpStatus.CREATED) + public Product updateProduct(@PathVariable int id,@RequestBody Product product ){ + + Product productToUpdate = this.repository.update(id, product); + if (productToUpdate != null) + return productToUpdate; + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "ID not found"); + + + } + + + @DeleteMapping("/{id}") + public Product delete(@PathVariable (name = "id") int id){ + Product prodDeleted = repository.find(id); + if (prodDeleted != null){ + this.repository.delete(id); + return prodDeleted; + } + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "ID not found"); + + } + + @GetMapping("sorted") + public List getAllSorted() { + + return this.repository.findAllSorted(); + } + + +// @GetMapping("/{id}") +// public Bagel find(int id) { + +} diff --git a/src/main/java/com/booleanuk/api/bagels/ProductRepository.java b/src/main/java/com/booleanuk/api/bagels/ProductRepository.java new file mode 100644 index 0000000..e598059 --- /dev/null +++ b/src/main/java/com/booleanuk/api/bagels/ProductRepository.java @@ -0,0 +1,114 @@ + + + +package com.booleanuk.api.bagels; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import java.util.*; + + +@RestController +@RequestMapping("authors") +public class ProductRepository { + private List products = new ArrayList<>(); + + + public ProductRepository() { + Product product = new Product("Firstname", 12, "category"); + Product product1 = new Product("Secondname", 123, "sfs"); + Product product2 = new Product("ThirdName", 12, "cddategory"); + Product product3 = new Product("Fourthname", 122, "casfstegory"); + Product product4 = new Product("Fifthname", 122, "dcasfstegory"); + Product product5 = new Product("sixtname", 122, "bcasfstegory"); + Product product6 = new Product("seventhname", 122, "casfstegory"); + Product product7 = new Product("eightname", 122, "acasfstegory"); + + products.add(product); + products.add(product1); + products.add(product2); + products.add(product3); + products.add(product4); + products.add(product5); + products.add(product6); + products.add(product7); + + + + } + + public void delete(int id){ + + + for (; id < products.size()-1; id++){ + products.set(id, products.get(id+1)); + } + + } + + public Product create(Product product) { + Product.nextID = products.size(); + product.setId(Product.nextID); + this.products.add(product); + return product; + } + + public List findAll() { +// Product product = new Product("Namee", 13, "12",this.idCounter++); +// this.products.add(product); + return this.products; + } + + public List findAllSorted(){ + List sortedList = new ArrayList<>(); + List newlist = new ArrayList<>(); + + for (Product product : products){ + newlist.add(product.getCategory()); + + } + Collections.sort(newlist); + for (String cat : newlist){ + sortedList.add(products.get(hasCat(cat))); + } + return sortedList; + } + + public int hasCat(String item){ + for (Product product : products){ + if (Objects.equals(product.getCategory(), item)){ + return product.getId(); + } + } + return -1; + } + + + + public Product update(int id, Product product){ + if (find(id) != null){ + Product prodToUpdate = find(id); + product.setId(id); + + products.set(id, product); + + return product; + } + return null; + + + + + } + + public Product find(int id) { + for (Product product : this.products) { + if (product.getId() == id) { + return product; + } + } + return null; + + } +}