Esempio n. 1
0
        public IComandResult Post([FromBody] SalvarTurmaCommands command)
        {
            var user = this.User.Identity.Name;

            //var user = Guid.Parse("781503e9-272b-4251-8fdf-77aca5f2d57a");
            command.SetarUsuarioId(user);
            var result = (ComandResult)_turmaHandler.Handle(command);

            this.Commit(result.Success);
            return(result);
        }
        public IComandResult Turma(IFormFile upload)
        {
            if (upload != null && upload.Length > 0)
            {
                // ExcelDataReader works with the binary Excel file, so it needs a FileStream
                // to get started. This is how we avoid dependencies on ACE or Interop:
                Stream stream = upload.OpenReadStream();

                // We return the interface, so that
                IExcelDataReader reader = null;


                if (upload.FileName.EndsWith(".xls"))
                {
                    reader = ExcelDataReader.ExcelReaderFactory.CreateBinaryReader(stream);
                }
                else if (upload.FileName.EndsWith(".xlsx"))
                {
                    reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }
                else
                {
                    return(new ComandResult(false, "Arquivo não suportado!!", new { }));
                }

                var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                {
                    ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                    {
                        UseHeaderRow = true
                    }
                });

                // reader.IsFirstRowAsColumnNames = true;

                //DataSet result = reader.AsDataSet();
                reader.Close();

                var tblAuthors = result.Tables[0];



                foreach (DataRow drCurrent in tblAuthors.Rows)
                {
                    var salvar = new SalvarTurmaCommands();

                    salvar.Ensino            = drCurrent["ENSINO"].ToString();
                    salvar.Periodo           = drCurrent["PERIODO"].ToString();
                    salvar.NomeCoordenador   = drCurrent["COORDENADOR"].ToString();
                    salvar.NomeDiretor       = drCurrent["DIRETOR"].ToString();
                    salvar.QtdAulas1Bimestre = Convert.ToInt32(drCurrent["QTDAULAS1BIMESTRE"].ToString());
                    salvar.QtdAulas2Bimestre = Convert.ToInt32(drCurrent["QTDAULAS2BIMESTRE"].ToString());
                    salvar.QtdAulas3Bimestre = Convert.ToInt32(drCurrent["QTDAULAS3BIMESTRE"].ToString());
                    salvar.QtdAulas4Bimestre = Convert.ToInt32(drCurrent["QTDAULAS4BIMESTRE"].ToString());

                    var serieId        = _serieRepositorio.Existe(drCurrent["SERIE"].ToString());
                    var anoId          = _anoRepositorio.Existe(drCurrent["ANO"].ToString());
                    var funcionarioId  = _funcionarioRepositorio.Existe(drCurrent["PROFESSOR"].ToString());
                    var departamentoId = _departamentoRepositorio.Existe(drCurrent["DEPARTAMENTO"].ToString());
                    var escolaId       = _escolaRepositorio.Existe(drCurrent["ESCOLA"].ToString());

                    if (serieId == null)
                    {
                        return(new ComandResult(false, "SerieId não encontrado!!" + drCurrent["SERIE"].ToString(), new { }));
                    }
                    if (anoId == null)
                    {
                        return(new ComandResult(false, "anoId não encontrado!!" + drCurrent["ANO"].ToString(), new { }));
                    }
                    if (funcionarioId == null)
                    {
                        return(new ComandResult(false, "funcionarioId não encontrado!!" + drCurrent["PROFESSOR"].ToString(), new { }));
                    }
                    if (departamentoId == null)
                    {
                        return(new ComandResult(false, "departamentoId não encontrado!!" + drCurrent["DEPARTAMENTO"].ToString(), new { }));
                    }
                    if (escolaId == null)
                    {
                        return(new ComandResult(false, "escolaId não encontrado!!" + drCurrent["ESCOLA"].ToString(), new { }));
                    }

                    salvar.SerieId        = serieId.Id.ToString();
                    salvar.AnoId          = anoId.Id.ToString();
                    salvar.FuncionarioId  = funcionarioId.Id.ToString();
                    salvar.EscolaId       = escolaId.Id.ToString();
                    salvar.DepartamentoId = departamentoId.Id.ToString();
                    var user = Guid.Parse(this.User.Identity.Name);

                    if (user == null)
                    {
                        return(new ComandResult(false, "Usuario não encontrado!!", new { }));
                    }
                    salvar.SetarUsuarioId(user.ToString());
                    //var user = Guid.Parse("781503e9-272b-4251-8fdf-77aca5f2d57a");


                    _turmaHandler.Handle(salvar);
                }

                this.Commit(true);

                return(new ComandResult(true, "Adicionado com sucesso", new { }));
            }
            else
            {
                return(new ComandResult(false, "Arquivo não encontrado!!", new { }));
            }
        }