Ejemplo n.º 1
0
        public async Task <ActionResult> UpdateFlightAsync(string id, SaveFlightDTO values)
        {
            var flight = await _unitOfWork.Flights.GetByAsync(id);

            // Check flight exists
            if (flight == null)
            {
                return(NotFound(new { Id = "Mã chuyến bay này không tồn tại." }));
            }

            // Check id of flight
            if (id != values.Id)
            {
                return(BadRequest(new { id = "Mã chuyến bay không hợp lệ." }));
            }

            // Create SaveFlightDTO
            SaveFlightDTO saveFlightDTO = new SaveFlightDTO {
                Id          = values.Id,
                StartTime   = values.StartTime,
                FlightTime  = values.FlightTime,
                AirportFrom = values.AirportFrom,
                AirportTo   = values.AirportTo,
                SeatsCount  = values.SeatsCount,
                AirlineId   = values.AirlineId,
                Status      = values.Status
            };

            // Mapping: SaveAirport
            _mapper.Map <SaveFlightDTO, Flight>(saveFlightDTO, flight);

            // Mapping: SaveFlightTicketCategory
            foreach (var val in values.FlightTicketCategories)
            {
                var flightTicketCategoryAsync = await _unitOfWork.Flights
                                                .GetFlightTicketCategoriesByIdAsync(values.Id);

                var flightTicketCategory = flightTicketCategoryAsync
                                           .Where(ftc => ftc.TicketCategoryId == val.TicketCategoryId).SingleOrDefault();

                SaveFlightTicketCategoryDTO save = new SaveFlightTicketCategoryDTO {
                    FlightId         = values.Id,
                    TicketCategoryId = val.TicketCategoryId,
                    Price            = val.Price,
                };
                _mapper.Map <SaveFlightTicketCategoryDTO, FlightTicketCategory>(save, flightTicketCategory);
            }

            await _unitOfWork.CompleteAsync();

            return(Ok(new { success = true, message = "Sửa thành công" }));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> AddFlightAsync(SaveFlightDTO saveFlightDTO)
        {
            // Mapping: SaveFlightDTO
            var flight = _mapper.Map <SaveFlightDTO, Flight>(saveFlightDTO);

            var flightTempAsync = await _unitOfWork.Flights.FindAsync(f =>
                                                                      f.Id.ToLower().Equals(flight.Id.ToLower()));

            // Kiểm tra mã chuyến bay đã tồn tại hay chưa
            var flightTemp = flightTempAsync.SingleOrDefault();

            if (flightTemp != null)
            {
                return(BadRequest(new { Id = "Mã chuyến bay này đã tồn tại." }));
            }

            await _unitOfWork.Flights.AddAsync(flight);

            await _unitOfWork.CompleteAsync();

            return(Ok(new { success = true, message = "Thêm thành công." }));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> PostFlightAsync(SaveFlightDTO saveFlightDTO)
        {
            var res = await _service.AddFlightAsync(saveFlightDTO);

            return(res);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> PutFlightAsync(string id, SaveFlightDTO values)
        {
            var flight = await _service.UpdateFlightAsync(id, values);

            return(flight);
        }