Jamie Royal - Solution to User Stories#50
Open
JamieRoyal wants to merge 2 commits intoGreggs-Digital:mainfrom
Open
Jamie Royal - Solution to User Stories#50JamieRoyal wants to merge 2 commits intoGreggs-Digital:mainfrom
JamieRoyal wants to merge 2 commits intoGreggs-Digital:mainfrom
Conversation
This is my code that fulfils the requirements set by the User Stories, with some additional Unit Testing
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Greggs Products API — Implementation Overview
This document explains the enhancements and architectural changes made to the Greggs Products API to fulfill the specified user stories.
Summary of Changes
1. Replacing Static Product List with Data Access Layer
Original Implementation:
The ProductController returned a random selection from a static, hardcoded product array inside the controller.
Change Made:
Introduced a proper data access layer (IDataAccess and ProductAccess implementation) that simulates fetching products from a database. The controller now depends on this data access abstraction and calls its List method to retrieve the latest products.
Why:
This satisfies User Story 1:
"As a Greggs Fanatic, I want to get the latest menu of products rather than a random static list."
It decouples data retrieval from controller logic and allows future extension to real databases.
2. Implementing Currency Conversion
Original Implementation:
Prices were always returned in GBP without any currency conversion.
Change Made:
convertTo(defaulting to EUR) and converts all product prices accordingly before returning them.Error Handling:
If an unsupported currency is requested, the conversion fails gracefully — logging a warning and returning a BadRequest response indicating the unsupported currency.
Output DTO:
Introduced ProductDto to return product details, including original GBP price, converted price, and currency symbol.
Why:
This fulfills User Story 2:
"As a Greggs Entrepreneur, I want to get prices returned in Euros so I can expand our shop in Europe."
The implementation is extendable to support more currencies in the future.
3. Code Structure & Best Practices
Dependency Injection:
The data access layer is registered as a singleton service in Startup.cs and injected into the controller, promoting testability and separation of concerns.
Unit Testing:
Created unit tests covering:
The tests use mocks for the data access layer and logging, verifying both expected outputs and logging side effects.
Logging:
Currency conversion failures are logged with warnings to aid troubleshooting in production.
Swagger Integration:
Swagger is enabled to document and easily test the API endpoints.
How to Use
Get Products (default to EUR):
GET /product?pageStart=0&pageSize=5Returns a paged list of products priced in EUR by default.
Get Products in GBP:
GET /product?convertTo=GBPReturns products priced in GBP.
Unsupported Currency Example:
GET /product?convertTo=USDReturns HTTP 400 Bad Request with an error message.
Summary