This repository contains the Hotel Management API, a robust and scalable solution for managing hotel operations. The project is structured using a layered architecture, promoting modularity, maintainability, and testability.
- Swagger API Documentation: Access the API documentation here.
- Next.js Frontend: Access the frontend hosted in Azure here.
- Project Structure
- Base Layer
- App Layer
- WebApp Layer
- X-Road Protocol Implementation
- Testing
- Getting Started
- Seeded Users
- Contributing
- License
- Docker Guide
The API has been enhanced with X-Road protocol support, which includes the following features:
-
Request Header Validation: The
X-Road-Client
header is checked for presence in incoming requests. If missing, aBadRequest
response is returned. This is implemented in theXRoadHeaderFilter
class.- Relevant Code:
XRoadHeaderFilter.cs
(startLine: 10, endLine: 18)
- Relevant Code:
-
Response Headers: The API adds several X-Road specific headers to the response:
X-Road-Service
: Indicates the service being accessed, configured via theXRoadServiceAttribute
.X-Road-Id
: A unique identifier for the request.X-Road-Request-Hash
: A hash of the request for integrity verification.- Relevant Code:
XRoadHeaderFilter.cs
(startLine: 20, endLine: 35)
-
Centralized Error Handling: Custom exceptions like
BadRequestException
andNotFoundException
are used to handle errors gracefully. TheXRoadExceptionFilter
ensures that errors are returned with a consistent structure, including anX-Road-Error
header.- Relevant Code:
XRoadExceptionFilter.cs
(startLine: 10, endLine: 43),BadRequestException.cs
(startLine: 1, endLine: 6),NotFoundException.cs
(startLine: 1, endLine: 6)
- Relevant Code:
-
Endpoint-Specific Configuration: The
XRoadServiceAttribute
allows for endpoint-specific configuration, specifying the service details for each API endpoint.- Relevant Code:
XRoadServiceAttribute.cs
(startLine: 1, endLine: 12)
- Relevant Code:
-
API Controllers: The logic for handling X-Road headers and errors is integrated into the API controllers, ensuring that all endpoints comply with the X-Road protocol.
- Relevant Code:
BookingsController.cs
,HotelsController.cs
,RoomsController.cs
,AccountController.cs
,ClientsController.cs
- Relevant Code:
The application comes with pre-configured users for testing purposes:
-
Admin User
- UserName:
[email protected]
- Password:
Foo.Bar1
- UserName:
-
Guest User
- UserName:
[email protected]
- Password:
Guest.Pass1
- UserName:
When registering a new user through the application, the user will be created with regular user privileges.
The project is organized into several key layers:
This layer contains reusable components and infrastructure shared across the application.
- Base.Contracts: Defines core interfaces like
IBaseEntityRepository<TEntity, TKey>
for generic repository operations andIBaseUnitOfWork
for the Unit of Work pattern. - Base.DAL: Implements base data access logic with
BaseEntityRepository<TKey, TDomainEntity, TDalEntity, TDbContext>
andBaseUnitOfWork<TDbContext>
. - Base.BLL: Implements base business logic services with
BaseEntityService<TBllEntity, TDalEntity, TRepository, TKey>
. - Base.Domain: Contains base domain entity classes such as
EntityId<TKey>
andAuditableEntity<TKey>
. - Base.Helpers: Contains helper classes for common tasks, including JWT token handling.
- Base.Resources: Contains resource files for localization.
This layer contains the specific business logic and data access for the hotel management application.
- App.Contracts.DAL: Defines application-specific interfaces extending the base interfaces.
- App.DAL.EF: Implements the application's data access layer using Entity Framework Core.
- App.BLL: Implements the application's business logic services.
- App.Domain: Contains the application's domain models.
- App.DTO: Contains Data Transfer Objects for DAL, BLL, and Public API.
This layer contains the ASP.NET Core Web API implementation.
- Controllers: Contains MVC controllers for testing purposes.
- ApiControllers: Contains API controllers for handling API requests.
- Program.cs: The application's entry point, responsible for setting up dependency injection, database configuration, and middleware.
- App.Test: Contains integration tests for the API controllers.
To get started with the project, follow these steps:
- Clone the repository.
- Set up the database and configure the connection string.
- Run the application using your preferred IDE or command line.
The application can be configured to use different database settings as specified in the appsettings.json
file:
-
UseInMemoryDatabase: This option allows the application to use an in-memory database for testing purposes. Set
"UseInMemoryDatabase": true
under the"Testing"
section to enable this feature. -
Database Type: You can choose whether the application runs on an in-memory database or a real database by setting
"UseInMemory": true
or"UseInMemory": false
under the"Database"
section. -
Connection Strings: The application uses PostgreSQL databases by default. The connection strings are defined under the
"ConnectionStrings"
section:DefaultConnection
: Used for the main application database.TestConnection
: Used for testing purposes.
-
Data Initialization Options: The
"DataInit"
section provides options for database initialization:DropDatabase
: If set totrue
, the database will be dropped and recreated.MigrateDatabase
: If set totrue
, the database will be migrated to the latest version.SeedIdentity
: If set totrue
, identity data (such as users) will be seeded.õSeedData
: If set totrue
, additional data will be seeded into the database.
You can use the provided docker-compose.yaml
file to quickly set up a PostgreSQL database for the application. Ensure you have Docker and Docker Compose installed, then run the following command in the root directory of the project:
docker-compose up -d
This command will start a PostgreSQL container with the necessary configuration. The database will be accessible on port 5446. To stop the database, use:
docker-compose down
For details on building and pushing Docker images, refer to the Dockerfile and DOCKER_GUIDE.md.
Contributions are welcome! Please fork the repository and submit a pull request for any enhancements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.
Install/update tooling
dotnet tool update -g dotnet-ef
dotnet tool update -g dotnet-aspnet-codegenerator
Run from solution folder
dotnet ef migrations --project App.DAL.EF --startup-project WebApp add First-Db
dotnet ef database --project App.DAL.EF --startup-project WebApp update
dotnet ef database --project App.DAL.EF --startup-project WebApp drop
These controllers are generated for quick testing of the data model.
Install from nuget:
- Microsoft.VisualStudio.Web.CodeGeneration.Design
- Microsoft.EntityFrameworkCore.SqlServer
Run from WebApp folder!
dotnet aspnet-codegenerator controller -name HotelsController -actions -m App.Domain.Hotel -dc AppDbContext -outDir Controllers --useDefaultLayout --useAsyncActions --referenceScriptLibraries -f
dotnet aspnet-codegenerator controller -name RoomsController -actions -m App.Domain.Room -dc AppDbContext -outDir Controllers --useDefaultLayout --useAsyncActions --referenceScriptLibraries -f
dotnet aspnet-codegenerator controller -name BookingsController -actions -m App.Domain.Booking -dc AppDbContext -outDir Controllers --useDefaultLayout --useAsyncActions --referenceScriptLibraries -f
dotnet aspnet-codegenerator controller -name HotelsController -m App.Domain.Hotel -dc AppDbContext -outDir ApiControllers -api --useAsyncActions -f
dotnet aspnet-codegenerator controller -name RoomsController -m App.Domain.Room -dc AppDbContext -outDir ApiControllers -api --useAsyncActions -f
dotnet aspnet-codegenerator controller -name BookingsController -m App.Domain.Booking -dc AppDbContext -outDir ApiControllers -api --useAsyncActions -f
dotnet aspnet-codegenerator identity -dc AppDbContext --userClass App.Domain.Identity.AppUser -f