-
Notifications
You must be signed in to change notification settings - Fork 100
Linda Do #96
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
lindadqd
wants to merge
2
commits into
boolean-uk:main
Choose a base branch
from
lindadqd: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
Linda Do #96
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,12 @@ | ||
| package com.booleanuk.api.products; | ||
|
|
||
| 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); | ||
| } | ||
| } |
5 changes: 4 additions & 1 deletion
5
...booleanuk/api/bagels/BagelController.java → .../products/controller/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
75 changes: 75 additions & 0 deletions
75
src/main/java/com/booleanuk/api/products/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,75 @@ | ||
| package com.booleanuk.api.products.controller; | ||
|
|
||
| import com.booleanuk.api.products.model.Product; | ||
| import com.booleanuk.api.products.model.ProductRepository; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| @RestController | ||
| @RequestMapping("products") | ||
| public class ProductController { | ||
| private ProductRepository productRepository; | ||
|
|
||
| public ProductController() { | ||
| this.productRepository = new ProductRepository(); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ArrayList<Product> getAll() { | ||
| ArrayList<Product> products = productRepository.getAll(); | ||
| if (products.isEmpty()) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No products of the provided category were found"); | ||
| } | ||
| return products; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<String> createProduct(@RequestBody Product newProduct){ | ||
| Product product = productRepository.addProduct(newProduct); | ||
| if (product == null){ | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "This product is already in the list"); | ||
| } | ||
| return new ResponseEntity<String>(HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public Product getOne(@PathVariable int id) { | ||
| Product product = productRepository.getOne(id); | ||
| if (product == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find this product"); | ||
| } | ||
| return product; | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ResponseEntity<String> update(@PathVariable int id, @RequestBody Product product ) { | ||
| Product productCheck = productRepository.getOne(id); | ||
| if (productCheck == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find this product"); | ||
| } | ||
|
|
||
| boolean updatedProduct = productRepository.checkName(product, id); | ||
|
|
||
| if (!updatedProduct){ | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "This product is already in the list"); | ||
| } | ||
|
|
||
| productRepository.updateProduct(id, product); | ||
| return new ResponseEntity<String>(HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public Product delete(@PathVariable int id){ | ||
| Product product = productRepository.deleteProduct(id); | ||
| if (product == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find this product"); | ||
| } | ||
| return product; | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
.../java/com/booleanuk/api/bagels/Bagel.java → ...m/booleanuk/api/products/model/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
2 changes: 1 addition & 1 deletion
2
...booleanuk/api/bagels/BagelRepository.java → ...k/api/products/model/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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/booleanuk/api/products/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,52 @@ | ||
| package com.booleanuk.api.products.model; | ||
|
|
||
| public class Product { | ||
| public 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 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; | ||
| } | ||
|
|
||
|
|
||
| } |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/booleanuk/api/products/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,69 @@ | ||
| package com.booleanuk.api.products.model; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class ProductRepository { | ||
| private ArrayList<Product> products; | ||
|
|
||
| public ProductRepository() { | ||
| this.products = new ArrayList<>(); | ||
| this.products.add(new Product("product1", "category1", 100)); | ||
| this.products.add(new Product("product2", "category2", 40)); | ||
| this.products.add(new Product("product3", "category3", 90)); | ||
|
|
||
| } | ||
|
|
||
| public ArrayList<Product> getAll() { | ||
| return this.products; | ||
| } | ||
|
|
||
| public Product getOne(int id) { | ||
| for (Product product : this.products) { | ||
| if (product.getId() == id) { | ||
| return product; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public Product addProduct(Product newProduct) { | ||
| for (Product product : this.products) { | ||
| if (product.getName().equals(newProduct.getName())) { | ||
| return null; | ||
| } | ||
| } | ||
| products.add(newProduct); | ||
| return newProduct; | ||
| } | ||
|
|
||
| public boolean checkName(Product product, int id){ | ||
| for (Product productInList : this.products) { | ||
| if (productInList.getName().equals(product.getName())) { | ||
| return false; | ||
| } | ||
| } return true; | ||
| } | ||
|
|
||
| public Product deleteProduct(int id){ | ||
| for (Product product : this.products) { | ||
| if (product.getId() == id) { | ||
| products.remove(product); | ||
| return product; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public void updateProduct(int id, Product product) { | ||
| for (Product productInList : this.products) { | ||
| if (productInList.getId() == id) { | ||
| productInList.setName(product.getName()); | ||
| productInList.setCategory(product.getCategory()); | ||
| productInList.setPrice(product.getPrice()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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'd put all this business logic in either the model or repository