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
26 changes: 17 additions & 9 deletions src/main/java/com/booleanuk/api/bagels/BagelController.java
Original file line number Diff line number Diff line change
@@ -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<Bagel> getAll() {
return this.repository.findAll();
}
//@RestController
//@RequestMapping("bagel")
public class BagelController {
// BagelRepository repository;
//
// public BagelController(BagelRepository repository) {
// this.repository = repository;
// }
//
//
//// @GetMapping
// public List<Bagel> getAll() {
// return this.repository.findAll();
// }
}
36 changes: 23 additions & 13 deletions src/main/java/com/booleanuk/api/bagels/BagelRepository.java
Original file line number Diff line number Diff line change
@@ -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<Bagel> data = new ArrayList<>();

public void create(String type, int price) {
Bagel bagel = new Bagel(this.idCounter++, type, price);
this.data.add(bagel);
}

public List<Bagel> 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<Bagel> findAll() {
// return this.data;
// }
//
// @GetMapping("/{id}")
// public Bagel find(int id) {
// return this.data.stream()
// .filter(bagel -> bagel.getId() == id)
// .findFirst()
// .orElseThrow();
// }
}
14 changes: 14 additions & 0 deletions src/main/java/com/booleanuk/api/bagels/Main.java
Original file line number Diff line number Diff line change
@@ -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);


}
}
57 changes: 57 additions & 0 deletions src/main/java/com/booleanuk/api/bagels/Product.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
84 changes: 84 additions & 0 deletions src/main/java/com/booleanuk/api/bagels/ProductController.java
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> getAllSorted() {

return this.repository.findAllSorted();
}


// @GetMapping("/{id}")
// public Bagel find(int id) {

}
114 changes: 114 additions & 0 deletions src/main/java/com/booleanuk/api/bagels/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> findAll() {
// Product product = new Product("Namee", 13, "12",this.idCounter++);
// this.products.add(product);
return this.products;
}

public List<Product> findAllSorted(){
List<Product> sortedList = new ArrayList<>();
List<String> 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;

}
}