// create a pending order for confirmation
        //public OrderDto Order(List<SeatSelectionDto> seats, int scheduleId, string customer)
        //{
        //    //get schedule info
        //    var schedule = _unitOfWork.ScheduleRepository.GetById(scheduleId);

        //    //create seats
        //    var strSeats = seats.Split(',');
        //    var seatList = (from seat in strSeats
        //        let seatInfo = _unitOfWork.SeatRepository.Get(
        //        filter: s => s.SeatFormatId == schedule.BusType.SeatFormatId && s.SeatNumber == seat).FirstOrDefault()
        //        where seatInfo != null
        //        select new SeatDto
        //        {
        //            SeatNumber = seat,
        //            SeatClass = seatInfo.SeatClass
        //        }).ToList();

        //    //return the result
        //    return new OrderDto
        //    {
        //        ScheduleId = scheduleId,
        //        BusInfo = schedule.Company.Name + " - " + schedule.BusType.Name,
        //        LocationDetail = schedule.JourneyFrom.Name + " to " + schedule.JourneyTo.Name,
        //        Departure = schedule.DepartureTime,
        //        Seats = seatList,
        //        UserId = customer
        //    };
        //}
        public CartDto AddToCart(CartDto cart, SelectedSeatsDto seats)
        {
            // check if the order already exists or we'd have to create a new one
            var            oldOrder = (ICollection <TicketOrderDto>)cart.Orders.Where(o => o.OrderType == OrderType.BusTicket);
            TicketOrderDto oldTicketOrder;

            if (oldOrder.Count >= 0)
            {
                oldTicketOrder = oldOrder.FirstOrDefault(o => o.ScheduleId == seats.ScheduleId);
                if (oldTicketOrder != null)
                {
                    cart.Orders.Remove(oldTicketOrder);
                    oldTicketOrder.Seats.ToList().AddRange(seats.Seats);
                    cart.Orders.Add(oldTicketOrder);
                }
            }
            else
            {
                var order = new TicketOrderDto
                {
                    OrderType    = OrderType.BusTicket,
                    ScheduleId   = seats.ScheduleId,
                    LocationInfo = seats.LocationInfo,
                    Seats        = seats.Seats
                };

                cart.Orders.Add(order);
            }

            return(cart);
        }
Beispiel #2
0
        public async Task <bool> OrderTicketsForEvent(string userId, TicketOrderDto dto)
        {
            var currentEvent = await eventsRepository.All().FirstOrDefaultAsync(x => x.Id == dto.EventId);

            if (currentEvent.TotalTickets >= dto.TicketsCount)
            {
                currentEvent.TotalTickets -= dto.TicketsCount;
                var order = new Order
                {
                    AppUserId   = userId,
                    EventId     = dto.EventId,
                    OrderedOn   = DateTime.UtcNow,
                    TicketCount = dto.TicketsCount,
                };
                await ordersRepository.AddAssync(order);

                await ordersRepository.SaveChangesAsync();

                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Order(TicketOrderDto dto)
        {
            var userId           = userManager.GetUserId(this.User);
            var successfullOrder = await ordersService.OrderTicketsForEvent(userId, dto);

            if (successfullOrder && dto.TicketsCount > 0)
            {
                TempData["Purchase"] = $"Successfully purchased {dto.TicketsCount} tickets for event {dto.EventName}";
            }
            else
            {
                int ticketsAvailable = ordersService.CheckTicketsInStock(dto.EventId);
                TempData["FailedPurchase"] = $"Not Enough Tickets available! Maximum allowed : {ticketsAvailable}";
            }
            return(RedirectToAction("All", "Events", new { area = "Event" }));
        }