public IActionResult Index(int id = 1)
        {
            var traficLightByID = _repository.GetByIdAsync(id, CancellationToken.None).Result;

            if (traficLightByID is null)
            {
                traficLightByID = new TrafficLightEntity()
                {
                    Color = Colors.Red,
                    Date  = DateTime.Now
                };
                _repository.AddTrafficLightAsync(traficLightByID, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
                int lastId = _repository.GetMaxTraficLightsIdAsync(CancellationToken.None).Result;
                _currentTrafficLight.Id = lastId;
                TraficLightsWorker.TrafficLightsList.Add(_currentTrafficLight);
                _traficLightsWorker.CurrentTrafficLight = _currentTrafficLight;
                _traficLightsWorker.SetCurrentColorFromDBAsync(CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
                _traficLightsWorker.Activate();
                return(Redirect($"{lastId}"));
            }
            var traficLightWithCurrentId = TraficLightsWorker.TrafficLightsList.FirstOrDefault(t => t.Id == id);

            if (traficLightWithCurrentId != null)
            {
                _currentTrafficLight = traficLightWithCurrentId;
                _traficLightsWorker.CurrentTrafficLight = _currentTrafficLight;
                _traficLightsWorker.Activate();
                return(View(_currentTrafficLight));
            }
            else
            {
                TraficLightsWorker.TrafficLightsList.Add(_currentTrafficLight);
                _currentTrafficLight.Id = traficLightByID.Id;
                _traficLightsWorker.CurrentTrafficLight = _currentTrafficLight;
                _traficLightsWorker.SetCurrentColorFromDBAsync(CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
                _traficLightsWorker.Activate();
                return(View(_currentTrafficLight));
            }
        }
Exemplo n.º 2
0
        public async Task <ITrafficLight> Get(int id)
        {
            var traficLightById = await _repository.GetByIdAsync(id, CancellationToken.None);

            if (traficLightById == null)
            {
                traficLightById = new TrafficLightEntity()
                {
                    Color = Colors.Red,
                    Date  = DateTime.Now
                };

                //TODO replace code below with Interface Realization
                await _repository.AddTrafficLightAsync(traficLightById, CancellationToken.None);

                var trafficLightForService = new TrafficLight()
                {
                    Id = traficLightById.Id, Color = traficLightById.Color, Date = traficLightById.Date, IsSwitchingDown = default
                };
                _trafficLightsService.AddTrafficLight(trafficLightForService);

                return(trafficLightForService);
            }
            var activeTafficLigthFromService = _trafficLightsService._activeTrafficLights.FirstOrDefault(t => t.Id == id);

            if (activeTafficLigthFromService != null)
            {
                return(activeTafficLigthFromService);
            }
            else
            {
                var trafficLigthForService = new TrafficLight()
                {
                    Id = traficLightById.Id, Color = traficLightById.Color, Date = traficLightById.Date, IsSwitchingDown = default
                };
                _trafficLightsService.AddTrafficLight(trafficLigthForService);
                return(traficLightById);
            }
        }