Ejemplo n.º 1
0
        public List <Train> GetAllTrains()
        {
            var trainsDTO = _reader.ReadFile <TrainCSV>(Path.Combine(_dataFolder, "trains.txt"));

            return(trainsDTO.Select(td => new Train()
            {
                Carriages = new List <Carriage>(),
                StartLocation = td.StartLocation,
                EndLocation = td.EndLocation,
                Number = int.Parse(td.Number),
                Id = int.Parse(td.Number)
            }).ToList());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> StoreWeatherInfo(HttpPostedFileBase file)
        {
            try
            {
                //OpenWeatherMapService openWeatherMapService = new OpenWeatherMapService();
                //WeatherInfoGenerator weatherInfoGenerator = new WeatherInfoGenerator();

                if (ModelState.IsValid)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        if (file.FileName.EndsWith(".csv"))
                        {
                            using (var reader = new StreamReader(file.InputStream))
                            {
                                var cityIds = await _csvReader.ReadFile(reader);

                                foreach (var city in cityIds)
                                {
                                    string cityWeatherInfo = await _openWeatherMapService.GetWeatherInfoByCity(city);

                                    _weatherInfoGenerator.SaveWeatherInfoForCity(cityWeatherInfo, city);
                                }
                            }
                        }
                        else
                        {
                            ViewBag.Message = "Only CSV files accepted";
                            return(View("Index"));
                        }
                        ViewBag.Message = "Weather information saved successfully in Output folder of the solution";
                        return(View("Index"));
                    }
                    else
                    {
                        ViewBag.Message = "Please upload file in CSV format only";
                        return(View("Index"));
                    }
                    return(View("Index"));
                }
                else
                {
                    ViewBag.Message = "File upload failed!!";
                    return(View("Index"));
                }
            }

            catch (Exception e)
            {
                ViewBag.Message = "File upload failed!!";
                return(View("Index"));
            }
        }
Ejemplo n.º 3
0
        public List <Train> GetAllTrains()
        {
            //var trainsDTO = _reader.ReadFile<TrainCSV>(Path.Combine(_config.CsvDataPath, "TrainsDB.csv"));
            //var carriagesDTO = _reader.ReadFile<TrainCSV>(Path.Combine(_config.CsvDataPath, "carriages.csv"));
            //var placesDTO = _reader.ReadFile<TrainCSV>(Path.Combine(_config.CsvDataPath, "places.csv"));
            //var i = 0;
            //trainsDTO.Select(td => new Train()
            //{

            //});
            var Trains = new List <Train>();

            foreach (var trainDTO in _reader.ReadFile <TrainCSV>(Path.Combine(_config.CsvDataPath, "TrainsDB.csv")))
            {
                var train = new Train()
                {
                    AgencyId      = int.Parse(trainDTO.Id),
                    StartLocation = trainDTO.StartLocation,
                    EndLocation   = trainDTO.EndLocation,
                    Number        = int.Parse(trainDTO.Number),
                    Id            = int.Parse(trainDTO.Number),
                    Carriages     = new List <Carriage>(),
                };
                foreach (var carriagesDTO in _reader.ReadFile <CarriageCSV>(Path.Combine(_config.CsvDataPath, "carriages.csv")))
                {
                    if (train.Number == int.Parse(carriagesDTO.TrainId))
                    {
                        var carr = new Carriage()
                        {
                            Number       = int.Parse(carriagesDTO.CarriageId),
                            TrainId      = int.Parse(carriagesDTO.TrainId),
                            Train        = train,
                            DefaultPrice = decimal.Parse(carriagesDTO.Price),
                            Id           = int.Parse(carriagesDTO.CarriageId),
                            Type         = (CarriageType)Enum.Parse(typeof(CarriageType), carriagesDTO.Type),
                            Places       = new List <Place>()
                        };
                        var places = int.Parse(carriagesDTO.Places);
                        for (int pl = 1; pl <= places; pl++)
                        {
                            var newPlace = new Place()
                            {
                                Carriage        = carr,
                                CarriageId      = carr.Id,
                                Number          = pl,
                                Id              = carr.Id * pl,
                                PriceMultiplier = 1
                            };
                            carr.Places.Add(newPlace);
                        }
                        train.Carriages.Add(carr);
                    }
                    else
                    {
                        continue;
                    }
                }


                Trains.Add(train);
            }
            return(Trains);
            //return trainsDTO.Select(td => new Train()
            //{
            //    AgencyId = int.Parse(td.Id),
            //    Carriages = new List<Carriage>()
            //    {

            //         new Carriage() {

            //              Places = new List<Place>(),
            //              Type = CarriageType.SecondClassSleeping,
            //              DefaultPrice = 100m,
            //              Number = 1,
            //              places = 20,
            //        },new Carriage() {
            //              Places = new List<Place>(),
            //              Type = CarriageType.Sedentary,
            //              DefaultPrice = 40m,
            //              Number = 2,
            //              places = 100,
            //        },new Carriage() {
            //              Places = new List<Place>(),
            //              Type = CarriageType.FirstClassSleeping,
            //              DefaultPrice = 120m,
            //              Number = 3,
            //              places = 10,
            //        }
            //    },

            //    StartLocation = td.StartLocation,
            //    EndLocation = td.EndLocation,
            //    Number = int.Parse(td.Number),
            //    Id = int.Parse(td.Number),



            //}).ToList();
        }