Exemplo n.º 1
0
        public ActionResult Index(DataImportModel dataImportModel)
        {
            if (!IsFileValid(dataImportModel))
            {
                ModelState.AddModelError("UploadFile", "Por favor seleccione un archivo de Excel");
            }

            var academicGrade = _academicGradeRepository.Filter(x => x.AcademicYear.Id == dataImportModel.Year &&
                                                                x.Grade.Id == dataImportModel.Grade && x.Section.Equals(dataImportModel.Section)).FirstOrDefault();

            if (academicGrade == null)
            {
                ModelState.AddModelError("Year", "No existe ese grado académico");
            }
            else if (academicGrade.Students.Any())
            {
                ModelState.AddModelError("Year", "Ya hay alumos en este grado, borrelos e ingreselos de nuevo.");
            }
            if (!ModelState.IsValid)
            {
                var user       = _userRepository.GetById(Convert.ToInt64(_sessionManagementService.GetUserLoggedId()));
                var isDirector = ViewBag.IsDirector = user.Role.Name.Equals("Director");
                ViewBag.GradeId = isDirector
                    ? new SelectList(_gradeRepository.Filter(x => x.EducationLevel.Director != null && x.EducationLevel.Director.Id == user.Id).ToList(), "Id", "Name", 0)
                    : new SelectList(_gradeRepository.GetAllGrade(), "Id", "Name", 0);
                ViewBag.Year    = new SelectList(_academicYearRepository.Filter(x => x.EnrollsOpen), "Id", "Year");
                ViewBag.Section = new List <SelectListItem>();
                return(View(dataImportModel));
            }
            var myDataSet = _dataImportService.GetDataSetFromExcelFile(dataImportModel.UploadFile);

            try
            {
                _dataImportService.Import(myDataSet, academicGrade);
            }
            catch (Exception ex)
            {
                _viewMessageLogic.SetNewMessage("Error!", ex.Message, ViewMessageType.ErrorMessage);
                return(RedirectToAction("Index"));
            }

            const string title   = "Importación de Datos Exitosa";
            var          content = string.Format("Se importaron datos para el año: {0}, grado: {1} y sección: {2}"
                                                 , academicGrade.AcademicYear.Year // 0
                                                 , academicGrade.Grade.Name        // 1
                                                 , dataImportModel.Section         // 2
                                                 );

            _viewMessageLogic.SetNewMessage(title, content, ViewMessageType.SuccessMessage);
            return(RedirectToAction("Index"));
        }