-
Notifications
You must be signed in to change notification settings - Fork 100
Marcus Ikdal #81
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
Marcus0410
wants to merge
2
commits into
boolean-uk:main
Choose a base branch
from
Marcus0410: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
Marcus Ikdal #81
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,44 @@ | ||
| package com.booleanuk.api; | ||
|
|
||
| public class Product { | ||
| private static int nextId = 1; | ||
| private final int id; | ||
| private String name; | ||
| private String category; | ||
| private int price; | ||
|
|
||
| public Product(String name, String category, int price) { | ||
| this.id = nextId++; | ||
| this.name = name; | ||
| this.category = category; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getCategory() { | ||
| return category; | ||
| } | ||
|
|
||
| public int getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void setCategory(String category) { | ||
| this.category = category; | ||
| } | ||
|
|
||
| public void setPrice(int price) { | ||
| this.price = price; | ||
| } | ||
| } |
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,77 @@ | ||
| package com.booleanuk.api; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| @RestController | ||
| @RequestMapping("products") | ||
| public class ProductController { | ||
| private ProductRepository repo = new ProductRepository(); | ||
|
|
||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public Product createProduct(@RequestBody Product product) { | ||
| Product created = repo.create(product); | ||
| if (created == null) { | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Product with provided name already exists."); | ||
| } | ||
| return created; | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<Product> getProducts(@RequestParam(required = false) String category) { | ||
| List<Product> result = new ArrayList<>(); | ||
| if (category != null) { | ||
| result = repo.getCategory(category); | ||
| if (result == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, | ||
| "No products of the provided category were found."); | ||
| } | ||
| } else { | ||
| result = repo.getAll(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public Product getSpecificProduct(@PathVariable int id) { | ||
| Product product = repo.getProduct(id); | ||
| if (product == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); | ||
| } | ||
| return product; | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public Product updateProduct(@PathVariable int id, @RequestBody Product updated) { | ||
| Product product = repo.update(id, updated); | ||
| if (product == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); | ||
| } | ||
| return product; | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public Product deleteProduct(@PathVariable int id) { | ||
| Product product = repo.delete(id); | ||
| if (product == null) { | ||
| throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found."); | ||
| } | ||
| return product; | ||
| } | ||
|
|
||
| } |
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,82 @@ | ||
| package com.booleanuk.api; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| public class ProductRepository { | ||
| private List<Product> products = new ArrayList<>() { | ||
| { | ||
| add(new Product("How to build APIs", "Book", 1500)); | ||
| add(new Product("Grey bat", "Sports", 3000)); | ||
| } | ||
| }; | ||
|
|
||
| List<Product> getAll() { | ||
| return products; | ||
| } | ||
|
|
||
| List<Product> getCategory(String category) { | ||
| List<Product> filtered = new ArrayList<>(); | ||
| for (Product p : products) { | ||
| if (p.getCategory().toLowerCase().equals(category.toLowerCase())) { | ||
| filtered.add(p); | ||
| } | ||
| } | ||
| // return null if no items with this category | ||
| if (filtered.size() == 0) { | ||
| return null; | ||
| } | ||
| return filtered; | ||
| } | ||
|
|
||
| Product getProduct(int id) { | ||
| for (Product p : products) { | ||
| if (p.getId() == id) { | ||
| return p; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| Product create(Product product) { | ||
| // check if product with same name exists | ||
| for (Product p : products) { | ||
| if (p.getName().equals(product.getName())) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| products.add(product); | ||
| return product; | ||
| } | ||
|
|
||
| Product update(int id, Product updated) { | ||
| Product product = getProduct(id); | ||
| if (product != null) { | ||
| // check if product with updated name already exists | ||
| for (Product p : products) { | ||
| if (p.getName().equals(updated.getName())) { | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, | ||
| "Product with provided name already exists."); | ||
| } | ||
| } | ||
| product.setName(updated.getName()); | ||
| product.setCategory(updated.getCategory()); | ||
| product.setPrice(updated.getPrice()); | ||
| } | ||
| return product; | ||
| } | ||
|
|
||
| Product delete(int id) { | ||
| for (Product p : products) { | ||
| if (p.getId() == id) { | ||
| products.remove(p); | ||
| return p; | ||
| } | ||
| } | ||
| 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.
Nested code is quite hard to read, consider splitting out the conditional checks into their own private methods