The Product Filter API is designed to provide a robust and efficient way to filter products based on various query parameters. The solution incorporates several best practices to ensure clean, readable, and maintainable code while addressing performance, scalability, security, and user documentation.
-
Clean, Readable, Easy-to-Understand Code:
- The code is structured using clear naming conventions and follows the Single Responsibility Principle. Each method and class has a specific purpose, making it easier for developers to understand and maintain the codebase.
-
Performance, Scalability, and Security:
- In-Memory Caching: Implemented in-memory caching using
IMemoryCache
to store filtered product results. This reduces the number of external API calls, improving response times and overall performance. - Asynchronous Programming: Utilized asynchronous programming patterns (async/await) to handle I/O-bound operations efficiently, allowing the application to scale better under load.
- Input Validation: Ensured that all user inputs are validated to prevent common security vulnerabilities such as SQL injection and XSS attacks.
- In-Memory Caching: Implemented in-memory caching using
-
Unit Tests:
- Unit tests are implemented to verify the functionality of the API endpoints and the caching logic. This ensures that changes to the code do not introduce regressions and that the application behaves as expected.
-
Dependency Injection:
- The application uses dependency injection to manage service lifetimes and dependencies. This promotes loose coupling and enhances testability, allowing for easier unit testing and maintenance.
-
Appropriate Logging:
- Integrated logging using Serilog to capture important events and errors. The logging includes detailed information about the external API responses, which aids in debugging and monitoring the application’s behavior.
- Example of a logged response from the mocky.io API:
{ "products": [ { "id": 1, "name": "Product A", "price": 10.99, "sizes": ["small", "medium"] }, { "id": 2, "name": "Product B", "price": 15.49, "sizes": ["medium", "large"] } ] }
-
Security Implementation:
- JWT Authentication: Implemented JSON Web Token authentication to secure API endpoints
-
Service Layer Architecture:
- IProductService: Handles product-related business logic and external API communication
- IAuthenticationService: Manages user authentication and JWT token generation
- ICacheService: Abstracts caching operations for better maintainability
- IValidationService: Handles input validation and sanitization
-
Model Layer Structure:
- Domain Models:
Product
: Core product entityLoginModel
: User authentication detailsAPIResponse
: API response structureResultResponse
: Result response structure
- Domain Models:
-
Documentation for Users of the API:
- Comprehensive documentation is provided in the README file, detailing the API endpoints, request formats, and response structures. This documentation helps users understand how to interact with the API effectively.
- Example requests and responses are included to illustrate how to use the filtering capabilities of the API.
The API uses JWT Bearer token authentication. To access protected endpoints:
-
Default Test Credentials:
- Username:
admin
- Password:
password123
- Username:
-
Authentication Endpoint:
POST /api/auth/login Content-Type: application/json { "username": "admin", "password": "password123" }
-
Using the Token:
GET /products/filter Authorization: Bearer {your-jwt-token}
To get started with the Product Filter API, follow the installation and usage instructions provided in this README. Ensure that you have the necessary prerequisites installed, including the .NET 8 SDK and a suitable code editor.
By following these practices, the Product Filter API is designed to be a high-performance, scalable, and secure solution that is easy to understand and maintain.
The Product Filter API is a .NET 8 Web API that provides an HTTP endpoint to filter products based on various query parameters. The API retrieves product data from a mock external source and allows users to filter products by price, size, and highlight specific words in product descriptions.
-
GET /products/filter: Accepts optional query parameters to filter products:
minprice
: Minimum price of the products.maxprice
: Maximum price of the products.size
: Size of the products (e.g., small, medium, large).highlight
: Comma-separated words to highlight in product descriptions.
-
Returns a JSON response containing:
- All products if no parameters are provided.
- A filtered subset of products based on the provided parameters.
- A filter object with:
- Minimum and maximum price of all products.
- An array of distinct sizes available.
- An array of the ten most common words in product descriptions, excluding the five most common.
-
Highlights specified words in product descriptions using HTML
<em>
tags.
- .NET 8 SDK
- A code editor (e.g., Visual Studio, Visual Studio Code)
-
Clone the repository:
git clone <repository-url> cd ProductFilterApi
-
Restore the dependencies:
dotnet restore
-
Run the application:
dotnet run
-
Get all products:
GET /products/filter
-
Filter products by price:
GET /products/filter?minprice=5&maxprice=20
-
Filter products by size:
GET /products/filter?size=medium
-
Highlight words in descriptions:
GET /products/filter?highlight=green,blue
The API returns a JSON response structured as follows: