-
Notifications
You must be signed in to change notification settings - Fork 100
Magnus Krumbacher #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Magnus-droid
wants to merge
2
commits into
boolean-uk:main
Choose a base branch
from
Magnus-droid:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.booleanuk.api.products; | ||
|
|
||
| public class Product { | ||
| private int id; | ||
| private String name; | ||
| private String category; | ||
| private int price; | ||
| private static int nextId = 1; | ||
|
|
||
| public Product(String name, String category, int price) { | ||
| this.name = name; | ||
| this.category = category; | ||
| this.price = price; | ||
| this.id = nextId; | ||
| nextId++; | ||
| } | ||
|
|
||
| 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 int getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public void setPrice(int price) { | ||
| this.price = price; | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/booleanuk/api/products/ProductController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.booleanuk.api.products; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| import java.nio.channels.ScatteringByteChannel; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| @RestController | ||
| @RequestMapping("products") | ||
|
|
||
| public class ProductController { | ||
| private ProductRepository theProducts; | ||
|
|
||
| public ProductController() { | ||
| this.theProducts = new ProductRepository(); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<Product> getAll(@RequestParam(required = false) String category) { | ||
| if (this.theProducts.getAll(category) == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found!"); | ||
| } | ||
| return this.theProducts.getAll(category); | ||
| } | ||
|
|
||
| @PostMapping | ||
| public Product create(@RequestBody Product product) { | ||
| Product toReturn = this.theProducts.createProduct(product); | ||
| if (toReturn == null) { | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Already exists!"); | ||
| } | ||
| return toReturn; | ||
| } | ||
|
|
||
| //try-catch not needed at all but I just wanted to try it | ||
| @GetMapping("/{id}") | ||
| public Product getOne(@PathVariable(name = "id") int id) { | ||
| try { | ||
| return this.theProducts.getOneProduct(id); | ||
| } catch (NoSuchElementException e) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public Product update(@PathVariable(name = "id") int id, @RequestBody Product product) { | ||
| try { | ||
| Product toReturn = this.theProducts.updateProduct(id, product); | ||
| if (toReturn == null) { | ||
| throw new IllegalArgumentException("Item already exists"); | ||
| } | ||
| return toReturn; | ||
| } catch (NoSuchElementException e) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @DeleteMapping("/{id}") | ||
| public Product delete(@PathVariable(name = "id") int id) { | ||
| try { | ||
| return this.theProducts.deleteProduct(id); | ||
| } catch (NoSuchElementException e) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
src/main/java/com/booleanuk/api/products/ProductRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.booleanuk.api.products; | ||
|
|
||
| import jdk.jshell.spi.ExecutionControl; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class ProductRepository { | ||
| private List<Product> products; | ||
|
|
||
| public ProductRepository() { | ||
| this.products = new ArrayList<>(); | ||
| } | ||
|
|
||
| public Product createProduct(Product product) { | ||
| for (Product sameNameProduct : this.products) { | ||
| if (product.getName().equals(sameNameProduct.getName())) { | ||
| return null; | ||
| } | ||
| } | ||
| this.products.add(product); | ||
| return product; | ||
| } | ||
|
|
||
| public List<Product> getAll(String category){ | ||
| if (category == null) { | ||
| return this.products; | ||
| } | ||
| List<Product> matchingProducts = new ArrayList<>(); | ||
| for (Product product : this.products) { | ||
| if (product.getCategory().equals(category)) { | ||
| matchingProducts.add(product); | ||
| } | ||
| } | ||
| if (matchingProducts.isEmpty()) { | ||
| return null; | ||
| } | ||
| return matchingProducts; | ||
| } | ||
|
|
||
|
|
||
| public Product getOneProduct(int id) throws NoSuchElementException { | ||
| return this.products.stream() | ||
| .filter(product -> product.getId() == id) | ||
| .findFirst() | ||
| .orElseThrow(NoSuchElementException::new); | ||
| } | ||
|
|
||
| public Product updateProduct(int id, Product product) { | ||
| try { | ||
| Product productToUpdate = this.getOneProduct(id); | ||
| for (Product sameNameProduct : this.products) { | ||
| if (product.getName().equals(sameNameProduct.getName())) { | ||
| return null; | ||
| } | ||
| } | ||
| productToUpdate.setName(product.getName()); | ||
| productToUpdate.setCategory(product.getCategory()); | ||
| productToUpdate.setPrice(product.getPrice()); | ||
| return productToUpdate; | ||
| } catch (NoSuchElementException e) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public Product deleteProduct(int id) { | ||
| try { | ||
| Product productToDelete = this.getOneProduct(id); | ||
| this.products.remove(productToDelete); | ||
| return productToDelete; | ||
| } catch (NoSuchElementException e) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this IF statement and the corresponding exception is correct...