public void UpdateFlight(FlightDTO dto) { var flight = _db.Flights .Include(x => x.Airplane) .Include(x => x.City) .FirstOrDefault(x => x.Id == dto.Id); var city = _db.Cities.FirstOrDefault(x => x.Id == dto.CityId); var currentCityName = ConfigurationManager.AppSettings["CurrentCityName"]; DateTime.TryParseExact(dto.Time, "H:mm", null, System.Globalization.DateTimeStyles.None, out var time); flight.FlightNumber = dto.FlightNumber; flight.Time = time; flight.Tittle = flight.IsArrival ? $"{city.Name} - {currentCityName}" : $"{currentCityName} - {city.Name}"; flight.CityId = dto.CityId; flight.AirplaneId = dto.AirplaneId; flight.SoldTicketsAmount = dto.SoldTicketsAmount; _db.Update(flight); _db.SaveChanges(); }
public virtual async Task <TEntity> UpdateAsync(TEntity entity) { if (entity == null) { throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null"); } try { AirportContext.Update(entity); await AirportContext.SaveChangesAsync(); return(entity); } catch (DbUpdateException dbEx) { throw new DbUpdateException($"Issue while saving to DB: {dbEx.Message}"); } catch (Exception ex) { throw new Exception($"{nameof(entity)} could not be updated: {ex.Message}"); } }