Train Booking Project with .NET 8 | ASP NET Core Web API , C# | Train Ticket booking Website
Train Booking System using .NET Core Web API and Angular: A Complete Guide
In this comprehensive guide, we will walk you
through building a train booking system using .NET
Core Web API and Angular. This web-based application allows users to
book train tickets, select seats, make payments, and manage bookings. We
will also cover essential concepts like user authentication, train schedule
management, and payment integration with Stripe.
1.
Introduction to the Train Booking System
The Train Booking System is designed to
make the process of booking train tickets seamless and efficient. Built with .NET
Core Web API for the backend and Angular for the frontend, this
system allows users to search trains, select seats, and make
secure payments. Admins can easily manage train schedules, bookings, and
payments. This guide is perfect for developers looking to build a robust and
scalable system with modern technologies.
2.
Prerequisites for Building the System
Before diving into the development, ensure
that you have the following tools and knowledge:
- Basic
knowledge of C# and Angular: You'll be working with both the backend
(C#/.NET Core) and frontend (Angular).
- Familiarity
with Entity Framework Core: This helps with database management and
migrations.
- Tools
Required:
- Visual
Studio for .NET development
- Node.js
and Angular CLI for frontend development
- SQL
Server for the database
- Stripe
API for payment processing
- Postman for
API testing
3. System
Features for Users and Admins
User
Features
- Authentication:
Users can sign up, log in, and log out securely using ASP.NET Core
Identity.
- Train
Schedules: Users can view available train
schedules, including train routes and timings.
- Seat
Selection and Booking: Users can choose available seats and confirm bookings after selecting a train.
- Payment
Processing: Integrated payment options (e.g., Stripe
or PayPal) allow users to pay for their tickets securely.
- Booking
Management: Users can view their bookings, cancel
tickets, and request refunds if necessary.
Admin
Features
- Manage
Train Schedules: Admins can add, update, and remove
train schedules from the system.
- Manage
Bookings: Admins can view all user bookings and
take necessary actions if required.
- Payment
Management: Admins can track payments and ensure
successful transactions.
4.
Architectural Overview
The system is built using a Client-Server
model:
- Client
(Frontend): Angular is used to build the dynamic
user interface. It communicates with the backend API using HTTP requests.
- Server
(Backend): .NET Core Web API handles user
authentication, train schedule management, seat booking, and payment
processing.
- Database: SQL
Server is used to store all the data, such as user information, bookings,
and payment records.
- Payment
Gateway: Integrated with Stripe or PayPal
for secure and reliable payment processing.
5. Setting
Up the Project
To start building the Train Booking System, follow these steps:
dotnet new
webapi -n TrainBookingSystem
cd TrainBookingSystem
dotnet add
package Microsoft.EntityFrameworkCore.SqlServer
This will create a new .NET Core Web API
project and install the necessary dependencies, including Entity Framework
Core for database management.
6. Backend
Development: User Authentication and Database Models
User
Authentication
We begin by setting up ASP.NET Core
Identity for user authentication. Here's a basic implementation:
public class
ApplicationUser : IdentityUser { }
public class
AppDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Train> Trains { get; set;
}
public DbSet<Booking> Bookings { get;
set; }
public DbSet<Payment> Payments { get;
set; }
public AppDbContext(DbContextOptions<AppDbContext>
options) : base(options) { }
}
This code sets up the ApplicationUser class for
authentication and a DbContext class that manages Trains, Bookings,
and Payments.
Train
Schedule Management API
Admins can manage the train schedules via the
API. Here's a basic example of how to add a new train:
[HttpPost]
public async
Task<IActionResult> AddTrain([FromBody] Train train)
{
_context.Trains.Add(train);
await _context.SaveChangesAsync();
return Ok(new { Message = "Train added
successfully!" });
}
This API allows admins to add new trains to
the system, including route, timings, and available seats.
7. Frontend
Development: Train List and Booking Interface
The Angular frontend communicates with the
backend to display train schedules and handle bookings. Below is an example of
how the Train List page can be structured in Angular:
<table>
<tr>
<th>Train Name</th>
<th>Route</th>
<th>Action</th>
</tr>
<tr *ngFor="let train of trains">
<td>{{ train.name }}</td>
<td>{{ train.route }}</td>
<td><a [routerLink]="['/booking',
train.id]">Book Now</a></td>
</tr>
</table>
This table displays a list of available
trains. Users can click the "Book Now" link to select a train
and proceed with booking.
8. Payment
Integration: Stripe Payment Gateway
The payment processing is done using Stripe.
Here's how you can integrate it into your backend:
[HttpPost("pay")]
public async
Task<IActionResult> ProcessPayment([FromBody] PaymentRequest request)
{
var options = new ChargeCreateOptions
{
Amount = request.Amount * 100,
Currency = "usd",
Source = request.Token
};
var service = new ChargeService();
Charge charge = service.Create(options);
return Ok(new { Message = "Payment
successful!" });
}
The payment API handles the transaction by
creating a charge using the Stripe API and returning a success message
to the user.
9. Testing
and Debugging the System
Testing is a critical step to ensure the
functionality and security of your Train Booking System. You can use the
following tools:
- Unit
Testing: Use xUnit or NUnit to
test your backend APIs and logic.
- API
Testing: Test your API endpoints using Postman.
- Debugging: Use
the Visual Studio Debugger to debug your application and ensure
everything runs smoothly.
10.
Deployment and Hosting
Once your Train Booking System is
ready, you can deploy it to a cloud platform like Azure. Here's how you
can deploy it:
- Use Azure
App Service to host your application.
- Set up
Continuous Integration and Continuous Deployment (CI/CD) pipelines
with GitHub Actions or Azure DevOps to automate the
deployment process.
11.
Conclusion
🎉
Congratulations! You’ve successfully built a Train Booking System using .NET
Core Web API and Angular. This system includes:
- User
authentication with ASP.NET Core Identity
- Train
schedule management with Entity Framework Core
- Seat
selection and booking functionalities
- Stripe
payment integration for secure payments
- Deployment on Azure
with CI/CD integration
By following this guide, you’ve learned how to
develop and deploy a full-featured train booking application.
Join the conversation