public ActionResult Add(AddFileViewModel model) { if (!ModelState.IsValid) { return(View(model)); } if (model.file != null) { if (model.file.ContentType != "application/vnd.ms-excel") { TempData["Erors"] = "Неправильное расширение файла, загрузите файл с расширением xls"; return(View()); } else { try { var fileName = this.HttpContext.Request.MapPath("~/Content/shedule.xls"); model.file.SaveAs(fileName); IEnumerable <busStop> answer = SheduleCreator.Create(fileName); repository.DeleteAll(); repository.AddStops(answer); TempData["Success"] = "Расписание добавлено"; } catch { TempData["Erors"] = "Ошибка при обработке файла, проверьте правильность файла"; } return(View()); } } else { TempData["Erors"] = "Выберите файл"; return(View()); } }
public async Task <bool> AddStopsToDatabase() { try { string response = await _getData.GetJsonString(AppSettings.StopsListJson); if (string.IsNullOrEmpty(response)) { return(false); } JArray linesArray = JArray.Parse(response); LastId = await _stopsRepository.GetLastId(); if (LastId != 0) { await _stopsRepository.DeleteAll(); } foreach (var stop in linesArray) { await _stopsRepository.Create(new StopRepo { Id = LastId + 1, StopName = _maps.MapUnicodeCharsToPolishChars(stop.Value <string>("nazwa")), StopNumbers = stop.Value <string>("numery"), VehiclesList = _maps.MapUnicodeCharsToPolishChars(stop.Value <string>("kierunki")) }); LastId++; } return(true); } catch (Exception ex) { return(false); } }