Esempio n. 1
0
        public async Task <IActionResult> AddShow(ShowDetailsDto bookingDetails)
        {
            if (await _repo.GetMultiplexe(bookingDetails.MultiplexId) == null)
            {
                return(NotFound("Multiplex not found"));
            }

            var movieToRepo = _mapper.Map <Movie>(bookingDetails);

            _repo.Add(movieToRepo);

            if (await _repo.SaveAll())
            {
                var showToRepo = _mapper.Map <Show>(bookingDetails);
                showToRepo.MovieId = movieToRepo.Id;
                _repo.Add(showToRepo);

                var showToReturn = _mapper.Map <ShowDetailsReturnDto>(showToRepo);

                if (await _repo.SaveAll())
                {
                    return(CreatedAtRoute("Getshow", new { controller = "Movies", multiplxId = showToRepo.MultiplexId }, showToRepo));
                }
            }
            throw new Exception("Error in saving Movie show details");
        }
        public async Task <IActionResult> BookMovie(UserBookingDto bookingDetails)
        {
            bookingDetails.UserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (await _repo.GetShow(bookingDetails.ShowId) == null || await _repo.GetMultiplexe(bookingDetails.MultiplexId) == null)
            {
                return(NotFound("Selected Show not found"));
            }

            var availableSeats = _mapper.Map <IEnumerable <SeatDto> >(await _repo.GetSeats(bookingDetails.MultiplexId, bookingDetails.ShowId));

            if (!bookingDetails.Seat.All(x => availableSeats.Any(y => x.SeatId == y.SeatId)))
            {
                return(NotFound("Selected seats are not available"));
            }

            var bookingToRepo = _mapper.Map <Booking>(bookingDetails);

            _repo.Add(bookingToRepo);

            if (await _repo.SaveAll())
            {
                var bookedSeatsToRepo = _mapper.Map <List <BookedSeat> >(bookingDetails.Seat);
                foreach (var bookedSeatToRepo in bookedSeatsToRepo)
                {
                    bookedSeatToRepo.BookingId = bookingToRepo.Id;
                    _repo.Add(bookedSeatToRepo);
                }

                var bookingReturnDto = _mapper.Map <UserBookingReturnDto>(bookingToRepo);

                if (await _repo.SaveAll())
                {
                    return(CreatedAtRoute("GetBookingById", new { controller = "Bookings", id = bookingToRepo.Id }, bookingReturnDto));
                }
            }

            throw new Exception("Booking cannot be completed");
        }