From 39e8c28bd0bab50ebc31369c5a217f63a3cbdf8f Mon Sep 17 00:00:00 2001 From: Amin <69254513+AminGh05@users.noreply.github.com> Date: Wed, 9 Jul 2025 18:32:29 +0330 Subject: [PATCH] Update Session09 Backend.md updated up to endpoint of reservation --- .../Session09/Session09 Backend.md | 92 +++++++++++++++---- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/ProjectOrientedSessions/Session09/Session09 Backend.md b/ProjectOrientedSessions/Session09/Session09 Backend.md index fe866de..0e8d93d 100644 --- a/ProjectOrientedSessions/Session09/Session09 Backend.md +++ b/ProjectOrientedSessions/Session09/Session09 Backend.md @@ -21,9 +21,9 @@ SerialNumber = Guid.NewGuid().ToString("N") - [ ] Check out new ERD: [Here](https://github.com/TheOrderOfPhoenix/ASP.NET/blob/main/ProjectOrientedSessions/docs/AlibabaERD-Version02.pdf) - [ ] Add migrations for the above database changes. -## 🧑‍💼 Service Layer +## 🧑‍💼 Path to Service Layer -- [ ] Create DTOs `CreateTravellerTicketDto` and `CreateTicketOrderDto`: +- [ ] Create DTOs `CreateTravellerTicketDto` and `CreateTicketOrderDto` and the **mappings**: ```csharp public class CreateTravellerTicketDto { @@ -61,6 +61,9 @@ public class CreateTicketOrderDto public long TransportationId { get; set; } public List MyProperty { get; set; } } +``` +```csharp + ``` Note: If you have the **Coupon** feature in your project, then add a `CouponCode` property in `CreateTicketOrderDto`. @@ -261,24 +264,81 @@ public async Task> CreateTicketOrderAsync(long accountId, CreateTic ## 🎯 Controller Layer -- [ ] Create `TicketOrderController` with the following endpoints: - - [ ] `POST /CreateTicketOrder` - - [ ] `GET /DownloadPdf` +- [ ] Create `TicketOrderController` with the endpoint `POST /CreateTicketOrder` +```csharp +[ApiController] +[Route("api/[controller]")] +[Authorize(Roles = "User")] +public class TicketOrderController : ControllerBase +{ + private readonly IUserContext _userContext; + private readonly ITicketOrderService _ticketOrderService; + + public TicketOrderController(IUserContext userContext, + ITicketOrderService ticketOrderService) + { + _userContext = userContext; + _ticketOrderService = ticketOrderService; + } + + [HttpPost("create-order")] + public async Task CreateTicketOrder([FromBody] CreateTicketOrderDto dto) + { + long accountId = _userContext.GetUserId(); + // check for account-id to be valid + if (accountId <= 0) + { + return Unauthorized(); + } + + var result = await _ticketOrderService.CreateTicketOrderAsync(accountId, dto); + if (result.IsSuccess) + { + return Ok(result.Data); + } + + return result.Status switch + { + ResultStatus.Unauthorized => Unauthorized(result.ErrorMessage), + ResultStatus.NotFound => NotFound(result.ErrorMessage), + ResultStatus.ValidationError => BadRequest(result.ErrorMessage), + _ => StatusCode(500, result.ErrorMessage) + }; + } +} +``` -## 🔁 DTOs & Mappings - -- [ ] Add `CreateTicketOrderDto`, `CreateTravelerTicketDto`. -- [ ] Add `TicketOrderSummaryDto`, `TravelerTicketDto`. -- [ ] Add mappings in `MappingProfile`. - ## 🗃️ Repository Layer -- [ ] Create `ITicketOrderRepository`, `TicketOrderRepository`. -- [ ] Implement: - - [ ] `FindAndLoadAllDetails` - - [ ] `GetAllByBuyerId` +Implement there methods in `TicketOrderRepository` and its interface +- [ ] `FindAndLoadAllDetails` +```csharp +public Task FindAndLoadAllDetailsAsync(long id) +{ + var ticketOrder = DbSet + .Include(to => to.Transportation).ThenInclude(t => t.FromLocation).ThenInclude(fl => fl.City) + .Include(to => to.Transportation).ThenInclude(t => t.ToLocation).ThenInclude(tl => tl.City) + .Include(to => to.Tickets).ThenInclude(t => t.Traveler) + .Where(to => to.Id == id).FirstOrDefaultAsync(); ---- + return ticketOrder; +} +``` + +- [ ] `GetAllByBuyerId` +```csharp +public async Task> GetAllByBuyerId(long buyerId) +{ + var ticketOrders = await DbSet + .Include(to => to.Transaction) + .Include(to => to.Transportation).ThenInclude(t => t.FromLocation) + .Include(to => to.Transportation).ThenInclude(t => t.ToLocation) + .Include(to => to.Transportation).ThenInclude(t => t.Company) + .Include(to => to.Transportation).ThenInclude(t => t.Vehicle) + .Where(to => to.BuyerId == buyerId).ToListAsync(); + return ticketOrders; +} +``` # Transportation and Seat Selection