public int Add(RailwayStationDto entity) { lock (Mutex) { return(AddNew(entity)); } }
public void Update(int key, RailwayStationDto entity) { lock (Mutex) { _logger.Debug($"Updating station {key}..."); var station = _unitOfWork.RailwayStations.Get(key); if (station == null) { _logger.Warn($"Requested station {key} does not exist!"); entity.Id = key; AddNew(entity); return; } station.Name = entity.Name; if (entity.Location != null) { station.Location = GetLocation(entity); } if (entity.RailwayPlatforms != null && entity.RailwayPlatforms.Count > 0) { station.RailwayPlatforms = GetPlatforms(entity); station.NumberOfPlatforms = station.RailwayPlatforms.Count; } _unitOfWork.SaveChanges(); _logger.Info($"Updated station {key}"); } }
private void AssertEqual(RailwayStation station, RailwayStationDto dto) { Assert.Equal(station.Id, dto.Id); Assert.Equal(station.Name, dto.Name); Assert.Equal(station.NumberOfPlatforms, dto.NumberOfPlatforms); AssertEqual(station.Location, dto.Location); }
private int AddNew(RailwayStationDto entity) { _logger.Debug("Adding new station..."); var station = _mapper.Map <RailwayStation>(entity); station.RailwayPlatforms = GetPlatforms(entity); station.Location = GetLocation(entity); var addedStation = _unitOfWork.RailwayStations.Add(station); _unitOfWork.SaveChanges(); _logger.Info($"New station {addedStation.Id} added!"); return(addedStation.Id); }
private ICollection <RailwayPlatform> GetPlatforms(RailwayStationDto entity) { int[] ids = entity.RailwayPlatforms.Select(rp => rp.Id).ToArray(); return(_unitOfWork.RailwayPlatforms.GetAll(rp => ids.Contains(rp.Id)).ToList()); }
private Location GetLocation(RailwayStationDto entity) { return(_unitOfWork.Locations.Get(entity.Location.Id)); }
public static async Task Initialize(IUnitOfWork unitOfWork) { try { using (var uow = unitOfWork) { if (!await uow.RailwayStationRepository.Get().AnyAsync()) { var railwaySt = new RailwayStationDto { Name = "Ленинградский" }; uow.RailwayStationRepository.Insert(railwaySt); railwaySt.Stations = new List <RailwayStStationStations> { new RailwayStStationStations { RailwayStationDto = railwaySt, StationDto = new StationDto { Name = "НЕИЗВЕСТНО", EcpCode = 0, } }, new RailwayStStationStations { RailwayStationDto = railwaySt, StationDto = new StationDto { Name = "Станция 1", Description = " Описание Станции 1", EcpCode = 1111, } }, new RailwayStStationStations { RailwayStationDto = railwaySt, StationDto = new StationDto { Name = "Станция 2", Description = " Описание Станции 2", EcpCode = 2222, } }, new RailwayStStationStations { RailwayStationDto = railwaySt, StationDto = new StationDto { Name = "Станция 3", Description = " Описание Станции 3", EcpCode = 3333, } }, new RailwayStStationStations { RailwayStationDto = railwaySt, StationDto = new StationDto { Name = "Станция 4", Description = " Описание Станции 4", EcpCode = 4444, } }, }; await uow.SaveAsync(); } if (!await uow.RegulatoryScheduleRepository.Get().AnyAsync()) { var railwatSt = await uow.RailwayStationRepository.Get().FirstAsync(); var unknownSt = await uow.StationRepository.Get().FirstOrDefaultAsync(st => st.Name == "НЕИЗВЕСТНО"); if (unknownSt == null) { return; } //------------------------------------ var stations = await uow.StationRepository.Get().Where(st => true).ToListAsync(); var routeStations = stations.Select(st => new StationsRouteDto { StationDto = st, IsLanding = true, ArrivalTime = DateTime.Now }).ToList(); var regSh = new RegulatoryScheduleDto { RailwayStId = railwatSt.Id, ArrivalTime = DateTime.MaxValue, DepartureTime = DateTime.Now, DaysFollowings = "Ежедневно", NumberOfTrain = "152", RouteName = "Москва-Питер", DestinationStationDto = unknownSt, DispatchStationDto = unknownSt, }; var routes = routeStations.Select(rs => new RegShStationsRouteRoutes { StationRouteDto = rs, RegulatoryScheduleDto = regSh }).ToList(); regSh.Route = routes; uow.RegulatoryScheduleRepository.Insert(regSh); await uow.SaveAsync(); } if (!await uow.StationRouteRepository.Get().AnyAsync()) { var station = await uow.StationRepository.Get().FirstOrDefaultAsync(st => st.Name == "Станция 1"); if (station != null) { var stRoute = new StationsRouteDto() { IsLanding = true, StationDtoId = station.Id, ArrivalTime = DateTime.Now }; uow.StationRouteRepository.Insert(stRoute); await uow.SaveAsync(); } } } } catch (Exception e) { Console.WriteLine(e); } }