This section provides an overview of the database schema created for the WebShop project. It includes details about each table, its columns, data types, and best practices used in the database design.
-
Description: Stores information about product categories.
-
Fields:
CategoryId
(UNIQUEIDENTIFIER, PRIMARY KEY, DEFAULT NEWID()): Unique identifier for each category.CategoryName
(NVARCHAR(100), NOT NULL): Name of the category.Description
(NVARCHAR(255)): Description of the category.
-
Indexes:
IDX_CategoryId
onCategoryId
to optimize lookups.
-
Description: Stores information about products available in the shop.
-
Fields:
ProductId
(UNIQUEIDENTIFIER, PRIMARY KEY, DEFAULT NEWID()): Unique identifier for each product.ProductName
(NVARCHAR(100), NOT NULL): Name of the product.ProductCode
(NVARCHAR(50), NOT NULL): Unique code for the product.Description
(NVARCHAR(255)): Description of the product.Price
(DECIMAL(18, 2), NOT NULL): Price of the product.StockQuantity
(INT, NOT NULL): Quantity of the product in stock.ImageBase64
(NVARCHAR(MAX)): Base64-encoded image of the product.
-
Indexes:
IDX_ProductId
onProductId
to improve performance.
-
Description: Manages the many-to-many relationship between products and categories.
-
Fields:
ProductId
(UNIQUEIDENTIFIER, FOREIGN KEY): ReferencesProductId
in theProducts
table.CategoryId
(UNIQUEIDENTIFIER, FOREIGN KEY): ReferencesCategoryId
in theCategories
table.
-
Primary Key: Composite key (
ProductId
,CategoryId
).
- Description: Stores customer information.
- Fields:
CustomerId
(UNIQUEIDENTIFIER, PRIMARY KEY, DEFAULT NEWID()): Unique identifier for each customer.FirstName
(NVARCHAR(100), NOT NULL): Customer's first name.LastName
(NVARCHAR(100), NOT NULL): Customer's last name.Email
(NVARCHAR(255), NOT NULL): Customer's email address.
-
Description: Stores order information for customers.
-
Fields:
OrderId
(UNIQUEIDENTIFIER, PRIMARY KEY, DEFAULT NEWID()): Unique identifier for each order.CustomerId
(UNIQUEIDENTIFIER, NOT NULL, FOREIGN KEY): ReferencesCustomerId
in theCustomers
table.OrderDate
(DATETIME, NOT NULL): Date when the order was placed.
-
Indexes:
IDX_CustomerId
onCustomerId
to improve performance.
- Description: Manages the many-to-many relationship between orders and products.
- Fields:
OrderProductId
(UNIQUEIDENTIFIER, PRIMARY KEY, DEFAULT NEWID()): Unique identifier for each order-product relation.OrderId
(UNIQUEIDENTIFIER, FOREIGN KEY): ReferencesOrderId
in theOrders
table.ProductId
(UNIQUEIDENTIFIER, FOREIGN KEY): ReferencesProductId
in theProducts
table.Quantity
(INT, NOT NULL): Quantity of the product ordered.
- Normalization: The database schema is designed to be in Third Normal Form (3NF) to reduce redundancy and ensure data integrity.
- Use of GUIDs:
UNIQUEIDENTIFIER
(GUID) is used for primary keys to ensure globally unique values and facilitate distributed data generation. - Indexes: Indexes are created on primary keys and foreign keys to improve query performance.
- Referential Integrity: Foreign keys are used to maintain referential integrity between related tables.
- Data Types: Appropriate data types are used for each column to optimize storage and performance (e.g.,
DECIMAL
for prices,NVARCHAR
for text). - Base64 Image Storage: Product images are stored as base64 strings in
ImageBase64
for ease of retrieval and integration with front-end applications.
This schema is designed to provide a solid foundation for a web shop application, ensuring efficient data management and retrieval.
The WebShop API was developed to provide robust and efficient endpoints for managing products, categories, customers, and orders. Below are the key practices and architecture used in creating the API.
- Presentation Layer: Contains the controllers that handle HTTP requests and responses.
- Application Layer: Contains services that implement business logic.
- Core Layer: Contains domain entities and interfaces.
- Infrastructure Layer: Contains the implementation of repositories and database context.
- Utilized to manage dependencies and promote loose coupling. Services and repositories are injected into controllers and other services.
- Used to transfer data between layers. DTOs are used in service methods to encapsulate the data sent to and received from the API.
- All data access methods are implemented asynchronously using
async
andawait
to improve scalability and performance.
- Used as the ORM (Object-Relational Mapper) for database operations. It simplifies data access by allowing us to work with C# objects instead of SQL queries.
- Used for object-to-object mapping, simplifying the conversion between domain models and DTOs.
- Data annotations and FluentValidation are used to validate incoming data to ensure data integrity and consistency.
GET /api/Catalog/products
: Retrieves a paginated list of products.GET /api/Catalog/products/{id}
: Retrieves a product by its ID.
GET /api/orders
: Retrieves a list of orders.GET /api/orders/{id}
: Retrieves an order by its ID.POST /api/orders/{orderId}/products/{productId}/addtocart
: Adds a product to an existing or new order's cart for a specific customer.
POST /api/shoppingcart/update
: Updates the cart with new items or changes to existing items.POST /api/shoppingcart/checkout
: Processes the current cart and completes the order.
The frontend of the WebShop project has been developed using React with Redux to manage the application's global state. Below is a description of the component structure, services used to connect to the API, and key practices employed in frontend development.
The main components of the frontend are organized in the components
folder. The two main components are:
- Description: This component displays the user's shopping cart. It allows viewing the added products, updating the quantity of each product, and removing products. It also provides functionality to place the order.
- Main Functions:
- Load and display cart items from local storage.
- Update product quantities and remove products from the cart.
- Process the order and communicate with the backend to create a new order.
- Display success and error modals in case of issues during the ordering process.
- Description: This component displays a list of available products in the store. It allows users to view product details and add products to the cart.
- Main Functions:
- Retrieve and display the list of products from the API.
- Allow users to add products to the cart.
Services have been created to interact with the API and handle HTTP requests. These services are located in the services
folder and are designed to simplify communication with the backend.
- Description: This service handles requests related to orders. It includes methods to add products to the cart and process orders.
- Main Methods:
addToCart(orderId: string, orderData: OrderData)
: Sends order data to the backend to create or update an order.
- Description: This service handles requests related to products. It includes methods to get the list of products and details of a specific product.
- Main Methods:
getProducts()
: Retrieves a list of products.getProductById(productId: string)
: Retrieves details of a specific product.
- Componentization: Components are designed to be reusable and are kept separate in different files for better organization and maintainability.
- Abstracted Services: Services abstract the API communication logic, allowing components to focus solely on UI presentation and specific user interface logic.
- State Management with Redux: Redux is used to manage the global state of the shopping cart, ensuring consistent and predictable state updates across the application.
- Modularity and Scalability: The project structure is designed to be modular and scalable, making it easier to add new features and components as the project grows.
This approach ensures a smooth and efficient user experience while keeping the code organized and easy to maintain.
To get the WebShop application up and running, follow these steps:
-
Run the
CreateDB.sql
Script- This script creates the necessary database schema.
- Use SQL Server Management Studio or any other SQL client to execute the script against your SQL Server instance.
-
Run the
DataSeed.sql
Script- This script seeds the database with initial data.
- Execute it after running
CreateDB.sql
to populate the database with sample data.
-
Set Up Your Connection String
- Open the
appsettings.json
file in the API project. - Configure the connection string to point to your SQL Server database. Update the
ConnectionStrings
section with your database server details.
Example:
"SqlConnectionString": { "default": "Server=LUPEREZ;Database=WebShopDB;Trusted_Connection=True;TrustServerCertificate=True;"
- Open the
}
To get the WebShop application up and running, follow these steps:
- Build and Run the API Project
- Open the API project in Visual Studio or your preferred IDE.
- Build the project to compile the backend code.
- Run the project to start the backend API server.
- Ensure that the API is running on the configured port and is accessible.
-
Install Dependencies
- Navigate to the frontend project directory using a terminal or command prompt.
- Run the following command to install all required dependencies:
npm install
-
Start the Frontend Application
- Execute the following command to start the frontend development server:
npm start
- The application should open in your default web browser. If it doesn't, manually navigate to
http://localhost:3000
(or the port specified in yourpackage.json
).
- Execute the following command to start the frontend development server:
- Ensure that both the API and frontend applications are running simultaneously to interact with the complete application.
- If you encounter any issues, check the logs of both the backend API and frontend for error messages and troubleshoot accordingly.
Following these steps will set up and run the WebShop application on your local machine.