Esempio n. 1
0
        public async Task <ResultTypes> UpdateAirplaneSeatsAsync(int airplaneId, AirplaneSeat[] seats)
        {
            AirplaneEntity updatingAirplane = await _airplaneRepository.GetByIdAsync(airplaneId);

            if (updatingAirplane == null)
            {
                return(ResultTypes.NotFound);
            }

            // checks if all seats from 'seats' array have unique position
            bool unique = seats
                          .GroupBy(x => new { x.AirplaneId, x.Floor, x.Section, x.Zone, x.Row, x.Number })
                          .All(g => g.Count() == 1);

            if (!unique)
            {
                return(ResultTypes.Duplicate);
            }

            bool existingSeatTypes = true;

            foreach (AirplaneSeat seat in seats)
            {
                AirplaneSeatTypeEntity seatType =
                    await _airplaneRepository.GetSeatTypeById(seat.TypeId);

                if (seatType == null)
                {
                    existingSeatTypes = false;
                    break;
                }
            }

            if (!existingSeatTypes)
            {
                return(ResultTypes.NotFound);
            }

            await _airplaneRepository.DeleteAirplaneSeatsAsync(airplaneId);

            AirplaneSeatEntity[] seatsDal = seats.Select(_mapper.Map <AirplaneSeatEntity>).ToArray();

            await _airplaneRepository.UpdateAirplaneSeatsAsync(airplaneId, seatsDal);

            return(ResultTypes.Ok);
        }