public IHttpActionResult HardDeleteFlight(int?id)
        {
            if (!User.IsInRole(AvailableRoles.DatabaseManager))
            {
                return(BadRequest("You have not access to delete the flight.\nPlease contact your Database Manager."));
            }

            //The HardDeleteFlight Action will remove every record in database that relates with the flight
            if (!id.HasValue)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var flightDb = flightRepository.GetSingleFlight(id ?? (int)InvalidPropertyValues.undefinedValue);

            if (flightDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var flightTickets = ticketRepository.GetTicketsOfFlight(flightDb.FlightId);

            ClearOrders(flightTickets);
            ticketRepository.RemoveMultipleTicket(flightTickets);

            flightRepository.Remove(flightDb);

            unitOfWork.Complete();

            var flightService = new FlightHubService();

            flightService.FlightDeleted(id ?? (int)InvalidPropertyValues.undefinedValue);

            return(Ok());
        }
        private void InformViewsForReleaseSeat(Flight flight, IEnumerable <long> releasedSeat)
        {
            var flightService = new FlightHubService();

            flightService.FlightSeatsOpened(releasedSeat.ToArray());
            flightService.FligthStatusChanged(flight.FlightId, (int)FlightStatusEnum.OnScedule);
        }
        public IHttpActionResult ReleaseSeat([FromBody] long[] seatIds)
        {
            if (seatIds == null || seatIds.Length < 1)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var seatsToRelease = flightSeatRepository.GetFlightSeatsById(seatIds);

            if (seatsToRelease == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var flightId      = seatsToRelease[0].FlightId;
            var flightTickets = ticketRepository.GetTicketsOfFlight(flightId);

            var closedSeats = flightTickets.Where(t => seatIds.Contains(t.SeatId));

            if (closedSeats.Count() > 0)
            {
                return(BadRequest("The Seat cannot be released because a ticket is already purchased!"));
            }

            seatsToRelease.ForEach(fs => fs.ReleaseSeat());

            unitOfWork.Complete();

            var flightService = new FlightHubService();

            flightService.FlightSeatsOpened(seatIds);

            return(Ok(seatIds));
        }
        public IHttpActionResult DeleteFlight(int?id)
        {
            if (!id.HasValue)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var flightDb = flightRepository.GetSingleFlight(id ?? (int)InvalidPropertyValues.undefinedValue);

            if (flightDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var flightTickets = ticketRepository.GetTicketsOfFlight(flightDb.FlightId);

            if (flightTickets.Any())
            {
                return(BadRequest("The Flight has purchased Tickets!\nTo delete it contact your Database Manager."));
            }

            flightRepository.Remove(flightDb);

            unitOfWork.Complete();

            var flightService = new FlightHubService();

            flightService.FlightDeleted(id ?? (int)InvalidPropertyValues.undefinedValue);

            return(Ok());
        }
        public ActionResult Create()
        {
            int refreshInterval = Convert.ToInt32(TempData["RefreshInterval"]);

            ViewBag.RefreshInterval = refreshInterval + 1;

            if (Session["flight"] == null || Session["flightSeats"] == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }

            Flight            flight      = Session["flight"] as Flight;
            List <FlightSeat> flightSeats = Session["flightSeats"] as List <FlightSeat>;

            //For holding the seats when user refreshes the view of the form
            foreach (var flightSeat in flightSeats)
            {
                if (flightSeat.IsAvailable)
                {
                    flightSeat.HoldSeat();
                    flightSeatRepository.ModifySeats(flightSeat);
                }
            }
            unitOfWork.Complete();

            var flightSeatIds = flightSeats.Select(fs => fs.FlightSeatId).ToArray();
            var flightService = new FlightHubService();

            flightService.FlightSeatsClosed(flightSeatIds);

            var ticketViewModels = new List <TicketFormViewModel>();

            flightSeats.ForEach(fs => ticketViewModels.Add(new TicketFormViewModel(flight.FlightId, fs)));

            var ticketContainer = InitializeTicketContainer(ticketViewModels);
            var orderViewModel  = new OrderTicketsFormViewModel(null, null, ticketContainer.GetTotalPrice());
            var viewModel       = new OrderTicketFormContainerViewModel(orderViewModel, ticketContainer, flight);

            return(View(viewModel));
        }
        public ActionResult ReleaseOrder()
        {
            var tempFlight = Session["flight"] as Flight;
            List <FlightSeat> flightSeats = Session["flightSeats"] as List <FlightSeat>;

            flightSeats.ForEach(s => s.ReleaseSeat());
            foreach (var flightSeat in flightSeats)
            {
                context.Entry(flightSeat).State = EntityState.Modified;
            }

            context.SaveChanges();

            var flightSeatIds = flightSeats.Select(fs => fs.FlightSeatId).ToArray();
            var flightService = new FlightHubService();

            flightService.FlightSeatsOpened(flightSeatIds);

            ClearOrderSession();

            return(RedirectToAction("ChooseSeats", "Flight", new { id = tempFlight.FlightId }));
        }
        private void InformViewsForCreateOrEdit(Flight flightToDB, int viewModelsId)
        {
            //Get the flight from database with all its relations needed for a flightDto
            var flightToViews = flightRepository.GetSingleFlight(flightToDB.FlightId);

            var flightService  = new FlightHubService();
            var flightStatuses = flightStatusRepository.GetFlightStatuses();

            var flightDto = Mapper.Map <Flight, FlightDto>(flightToDB);

            flightDto.FlightStatuses = flightStatuses.Select(s => Mapper.Map <FlightStatus, FlightStatusDto>(s)).ToList();
            flightDto.ConvertDate();

            if (viewModelsId == 0)
            {
                flightService.FlightCreated(flightDto);
            }
            else
            {
                flightService.FlightUpdated(flightDto);
            }
        }
        public IHttpActionResult HoldSeats([FromBody] long[] seatIds)
        {
            if (seatIds == null || seatIds.Length < 1)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var seatsToRelease = flightSeatRepository.GetFlightSeatsById(seatIds);

            if (seatsToRelease == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            seatsToRelease.ForEach(fs => fs.HoldSeat());

            unitOfWork.Complete();

            var flightService = new FlightHubService();

            flightService.FlightSeatsClosed(seatIds);

            return(Ok(seatIds));
        }
        public IHttpActionResult ChangeStatus(int?id, int?statusId)
        {
            if (!id.HasValue || !statusId.HasValue)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var flightDb = flightRepository.GetSingleFlight(id ?? (int)InvalidPropertyValues.undefinedValue);

            if (flightDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            flightDb.ChangeFlightStatus(statusId ?? (int)InvalidPropertyValues.undefinedValue);

            unitOfWork.Complete();

            var flightService = new FlightHubService();

            flightService.FligthStatusChanged(id ?? (int)InvalidPropertyValues.undefinedValue, statusId ?? (int)InvalidPropertyValues.undefinedValue);

            return(Ok());
        }