Пример #1
0
        public async Task <IActionResult> GetPassengersForBooking(int bookingId)
        {
            if (!int.TryParse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty, out var userId))
            {
                return(Unauthorized());
            }

            var result = await _passengerRepository.GetPassengersForBooking(bookingId, userId);

            return(result != null && result.Any() ? Ok(result) : BadRequest());
        }
        public async Task <IActionResult> Edit(BookingForEditDto bookingForEdit)
        {
            if (!int.TryParse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty, out var userId))
            {
                return(Unauthorized());
            }

            var booking = await _bookingRepository.GetById(bookingForEdit.Id);

            if (booking == null)
            {
                return(BadRequest("There was an error while processing Your request."));
            }

            if (booking.UserId != userId)
            {
                return(Unauthorized());
            }

            var passengersToUpdate = _mapper.Map <List <Passenger> >(bookingForEdit.Passengers);

            var passengersForBooking = await _passengerRepository.GetPassengersForBooking(bookingForEdit.Id, userId);

            var passengersId = new List <int>();

            foreach (var passenger in passengersForBooking)
            {
                passengersId.Add(passenger.Id);
            }
            foreach (var passenger in passengersToUpdate)
            {
                if (!passengersId.Contains(passenger.Id))
                {
                    return(Unauthorized());
                }
            }
            var updated = await _passengerRepository.UpdatePassengers(passengersToUpdate);

            return(updated != null?Ok() : BadRequest("There was an error while processing Your request."));
        }