Пример #1
0
        public async Task <HttpResponseMessage> UpdateStationAsync(IEnumerable <StationJsonModel> stations)
        {
            if (stations == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            try
            {
                bool isUpdated = false;

                List <Station> stationsNew = new List <Station>();

                foreach (var station in stations)
                {
                    var value = await _context.Stations.SingleOrDefaultAsync(r => r.Code == station.Code);

                    if (value != null) // update
                    {
                        if (station.DateUpdate > value.DateUpdate)
                        {
                            continue;
                        }

                        value.Name                  = station.Name;
                        value.RailwayId             = station.RailwayID;
                        value.CountryId             = station.CountryID;
                        value.DateCreate            = station.DateCreate;
                        value.DateUpdate            = station.DateUpdate;
                        value.Code                  = station.Code;
                        value.CodeOSGD              = station.CodeOSGD;
                        value.Name12Char            = station.Name12Char;
                        value.FreightSign           = station.FreightSign;
                        value.RailwayDepartmentID   = station.RailwayDepartmentID;
                        value.CountryId             = station.CountryID;
                        _context.Entry(value).State = EntityState.Modified;
                        isUpdated = true;
                    }
                    else // new
                    {
                        stationsNew.Add(new Station
                        {
                            Id                  = station.ID,
                            Name                = station.Name,
                            RailwayId           = station.RailwayID,
                            CountryId           = station.CountryID,
                            DateCreate          = station.DateCreate,
                            DateUpdate          = station.DateUpdate,
                            Code                = station.Code,
                            CodeOSGD            = station.CodeOSGD,
                            Name12Char          = station.Name12Char,
                            FreightSign         = station.FreightSign,
                            RailwayDepartmentID = station.RailwayDepartmentID
                        });
                    }
                }

                if (isUpdated)
                {
                    await _context.SaveChangesAsync();
                }

                if (stationsNew.Count > 0)
                {
                    await _context.BulkInsertAsync(stationsNew);

                    await _context.BulkSaveChangesAsync(bulk => bulk.BatchSize = 100);
                }
            }
            catch (Exception ex)
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }