コード例 #1
0
ファイル: CrawlerApp.cs プロジェクト: bruno-sales/Ikuzo-api
        public ValidationResult SyncModals()
        {
            var validation = new ValidationResult();

            try
            {
                //Get data from external resource
                var rioBusLines = _riobusRepository.GetAllLines().ToList();

                //Analyse Objects
                foreach (var rioBusLine in rioBusLines)
                {
                    var modalsToCreate = new List <Modal>();

                    //Get line from database
                    var line = _lineService.Get(rioBusLine.Line);

                    if (line != null)
                    {
                        //Get buses from external resource
                        var rioBusBuses = _riobusRepository.GetBusesInfoFromLine(line.LineId).ToList();

                        if (rioBusBuses.Any()) //Having buses
                        {
                            foreach (var rioBusBus in rioBusBuses)
                            {
                                //Check if adding in the right line
                                if (!string.Equals(line.LineId.ToLower(), rioBusBus.Line.ToLower()))
                                {
                                    continue;
                                }

                                //if not already added
                                if (_modalService.Get(rioBusBus.Order) == null)
                                {
                                    modalsToCreate.Add(new Modal(rioBusBus.Order, line.LineId)); //Add to Save
                                }
                            }
                        }
                    }

                    if (modalsToCreate.Any() == false || line == null)
                    {
                        continue;
                    }

                    //Remove previous bus info
                    validation.AddError(_modalService.RemoveModalsFromLine(line.LineId));

                    //Create
                    _modalService.CreateModals(modalsToCreate);
                }
            }
            catch (Exception e)
            {
                validation.AddError(new ValidationError(e.Message));
            }

            return(validation);
        }