This is the final project for the Code: KY May 2024 cohort, a complete e-commerce site for a holiday-themed dessert store serving a local community center.
This project is an ASP.NET Core 9 Razor Pages web application using:
- Database: SQLite 3
- ORM: Entity Framework Core
- Testing Framework: XUnit
The site integrates with two external APIs:
- Stripe: For payment processing.
- weatherapi.com: To display local weather on the homepage.
- ASP.NET Core 9
- Razor Pages
- Entity Framework Core
- SQLite
- XUnit
- Stripe API
- WeatherAPI
The main project directories are:
- HolidayDessertStore: Contains the main web application.
- HolidayDessertStore.API: Contains the API project.
- HolidayDessertStore.Tests: Contains the unit tests.
Key files and directories within HolidayDessertStore
:
appsettings.json
: Configuration settings for the application, including Stripe API keys.Data/
: Contains theApplicationDbContext.cs
for database context.Models/
: Contains the data models for the application.Pages/
: Contains the Razor Pages for the web application.Services/
: Contains services for interacting with external APIs and business logic.wwwroot/
: Contains static files like CSS, JavaScript, and images.
Key files and directories within HolidayDessertStore.API
:
appsettings.json
: Configuration settings for the API.Controllers/
: Contains API controllers.Data/
: Contains theApplicationDbContext.cs
for the API database context.Models/
: Contains the data models for the API.
-
Clone the repository:
git clone https://github.com/brett03112/Code-You-Final-Project
-
Navigate to the web project directory:
cd HolidayDessertStore
-
Install .NET 9 SDK: Download and install the .NET 9 SDK from https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/sdk-9.0.101-windows-x64-installer
Ensure the external API keys are configured correctly:
-
Open
HolidayDessertStore/Services/WeatherService.cs
. -
Locate the
WeatherService
constructor. -
Enter your weatherapi.com API key in the
_apiKey = "";
line.public WeatherService(IHttpClientFactory httpClientFactory, ILogger<WeatherService> logger) { _httpClientFactory = httpClientFactory; _logger = logger; _apiKey = "YOUR_WEATHER_API_KEY"; // Replace with your API key }
-
Open
HolidayDessertStore/appsettings.json
. -
Enter your Stripe.com API keys:
{ "StripeSettings": { "SecretKey": "YOUR_STRIPE_SECRET_KEY", "PublishableKey": "YOUR_STRIPE_PUBLISHABLE_KEY" }, // ... other settings }
dotnet run
- APIAdmin User: (Login-api-requests.http)
- Username:
admin@example.com
- Password:
admin00
You must be logged in under the "admin" user role above. Go to: https://localhost:7056/swagger
- Username:
Unit tests are implemented in the HolidayDessertStore.Tests
project using an in-memory database via the Microsoft.EntityFrameworkCore.InMemory
provider (Version 8.0.0).
To run the unit tests:
-
Navigate to the test project directory:
cd HolidayDessertStore.Tests
-
Run the tests:
dotnet test
- public async Task AddToCart_WithValidQuantity_ShouldAddItemAndUpdateStock()
- which tests adding items to the cart and updating the available items.
- public async Task AddToCart_ExceedingAvailableQuantity_ShouldThrowException()
- which tests that calling the "ShoppingCartService.AddToCartAsync" with a quantity that exceeds that available stock will throw an exception.
- public async Task GetCartTotal_WithMultipleItems_ShouldCalculateCorrectTotal()
- Make sure that the correct total is returned with multiple items in the cart.
- public async Task UpdateCartItemQuantity_ShouldUpdateQuantityAndStock()
- Ensures that the service can correctly update the quantity of an item in a cart and update the available stock accordingly.
The HolidayDessertStore.Tests
project includes the following unit tests for the shopping cart service:
ShoppingCartServiceTests.AddToCart_WithValidQuantity_ShouldAddItemAndUpdateStock
: Tests adding a valid quantity of an item to the cart and verifies that the item is added and the stock is updated.ShoppingCartServiceTests.AddToCart_ExceedingAvailableQuantity_ShouldThrowException
: Tests that attempting to add more items to a cart than is available should throw an exception.ShoppingCartServiceTests.GetCartTotal_WithMultipleItems_ShouldCalculateCorrectTotal
: Tests that theShoppingCartService.GetCartTotalAsync
method can accurately calculate the total cost of all items in a cart, given that the cart contains multiple items.ShoppingCartServiceTests.UpdateCartItemQuantity_ShouldUpdateQuantityAndStock
: Tests that updating a cart item quantity should update the quantity and stock accordingly.ShoppingCartServiceTests.RemoveFromCart_ShouldRestoreStock
: Tests that removing a cart item will restore the quantity of the associated dessert to the quantity available before the item was added.
Example HTTP requests can be found in the following files:
GET-api-requests.http
POST-api-requests.http
PUT-api-requests.http
DELETE-api-requests.http
Login-api-requests.http
Deployment steps are outlined in UserInstructions.md
.
The project is deployed to Azure App Service and can be accessed at: https://holidaydessertstore-ehcdecg3a5arbtd2.canadacentral-01.azurewebsites.net/
The initial admin user is created using the script found in InitialAdminUser.sql
.