-
Notifications
You must be signed in to change notification settings - Fork 100
Thomas Kristiansen #79
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
Thomaskri0801
wants to merge
3
commits into
boolean-uk:main
Choose a base branch
from
Thomaskri0801: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
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
2 changes: 1 addition & 1 deletion
2
.../java/com/booleanuk/api/bagels/Bagel.java → ...n/java/com/booleanuk/api/bagel/Bagel.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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package com.booleanuk.api.bagels; | ||
| package com.booleanuk.api.bagel; | ||
|
|
||
| public class Bagel { | ||
| private int id; | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
...booleanuk/api/bagels/BagelController.java → .../booleanuk/api/bagel/BagelController.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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package com.booleanuk.api.bagels; | ||
| package com.booleanuk.api.bagel; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
2 changes: 1 addition & 1 deletion
2
...booleanuk/api/bagels/BagelRepository.java → .../booleanuk/api/bagel/BagelRepository.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
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.product; | ||
|
|
||
| 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); | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/booleanuk/api/product/controller/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,80 @@ | ||
| package com.booleanuk.api.product.controller; | ||
|
|
||
| import com.booleanuk.api.product.model.Product; | ||
| import com.booleanuk.api.product.model.ProductRepository; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| import java.awt.color.ProfileDataException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/products") | ||
| public class ProductController { | ||
| private ProductRepository productRepository; | ||
|
|
||
| public ProductController() { | ||
| productRepository = new ProductRepository(); | ||
| } | ||
|
|
||
| @GetMapping | ||
| @RequestMapping(method = RequestMethod.GET) | ||
| public List<Product> getAll(@RequestParam("category") Optional<String> category) { | ||
| if(category.isPresent()) { | ||
| if (productRepository.getAll() != null) { | ||
| List<Product> productsCategory = new ArrayList<>(); | ||
| for (int i = 0; i < productRepository.getAll().size(); i++) { | ||
| if (productRepository.getAll().get(i).getCategory().equals(category.get())) { | ||
| productsCategory.add(productRepository.getAll().get(i)); | ||
| } | ||
| } | ||
| if(productsCategory.isEmpty()) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND,"No products of the provided category were found"); | ||
| } | ||
| return productsCategory; | ||
| } | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND,"No products of the provided category were found"); | ||
| } | ||
| if (productRepository.getAll() != null) { | ||
| return productRepository.getAll(); | ||
| } | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found"); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public Product getOne(@PathVariable int id) { | ||
| if(productRepository.getOne(id) != null) { | ||
| return productRepository.getOne(id); | ||
| } | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); | ||
| } | ||
|
|
||
| @PostMapping | ||
| public Product create(@RequestBody Product product) { | ||
| if (productRepository.create(product) != null) { | ||
| return product; | ||
| } | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists"); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public Product update(@PathVariable int id, @RequestBody Product product) { | ||
| if (productRepository.update(id,product) != null) { | ||
| return product; | ||
| } else { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public Product delete(@PathVariable int id) { | ||
| if (productRepository.delete(id) != null) { | ||
| return productRepository.getOne(id); | ||
| } | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND,"Product not found."); | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
src/main/java/com/booleanuk/api/product/model/Product.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,51 @@ | ||
| package com.booleanuk.api.product.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) { | ||
| nextId++; | ||
| this.id = 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 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; | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/booleanuk/api/product/model/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,68 @@ | ||
| package com.booleanuk.api.product.model; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class ProductRepository { | ||
|
|
||
| private List<Product> products; | ||
|
|
||
| public ProductRepository() { | ||
| this.products = new ArrayList<>(); | ||
| } | ||
|
|
||
| public List<Product> getAll() { | ||
| if (products.isEmpty()) { | ||
| return null; | ||
| } | ||
| return this.products; | ||
| } | ||
|
|
||
| public List<Product> getProducts() { | ||
| return products; | ||
| } | ||
|
|
||
| public Product getOne(int id) { | ||
| for (Product product : products) { | ||
| if (product.getId() == id) { | ||
| return product; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public Product create(Product product) { | ||
| for (Product value : products) { | ||
| if (value.getName().equals(product.getName())) { | ||
| return null; | ||
| } | ||
| } | ||
| products.add(product); | ||
| return product; | ||
| } | ||
|
|
||
| public Product update(int id, Product product) { | ||
| Product updatedProduct = this.getOne(id); | ||
| if (updatedProduct != null) { | ||
| if (updatedProduct.getName().equals(product.getName())) { | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); | ||
| } | ||
| updatedProduct.setName(product.getName()); | ||
| updatedProduct.setCategory(product.getCategory()); | ||
| updatedProduct.setPrice(product.getPrice()); | ||
| } | ||
| return updatedProduct; | ||
| } | ||
|
|
||
| public Product delete(int id) { | ||
| Product deletedProduct = this.getOne(id); | ||
| if (deletedProduct != null) { | ||
| products.remove(deletedProduct); | ||
| return deletedProduct; | ||
| } | ||
| return null; | ||
| } | ||
| } |
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.
This code can be simplified a lot further by using streams and filtering