public async Task <AppResponse> CriarAluno(CriarAlunoRequest request) { var usuario = new Usuario(request.Nome, request.UserName, request.Email); if (!usuario.TaValido()) { return(new AppResponse(false, ERRO_CRIAR_ALUNO, usuario.ObterErros())); } IdentityResult result = await _userManager.CreateAsync(usuario, request.Senha); if (!result.Succeeded) { return(new AppResponse(ERRO_CRIAR_ALUNO, false, IdentityHelper.ObterErros(result))); } else { IdentityResult resultRole = await _userManager.AddToRoleAsync(usuario, ROLE_ALUNO); if (!resultRole.Succeeded) { return(new AppResponse(ERRO_CRIAR_ALUNO, false, IdentityHelper.ObterErros(result))); } usuario.Aluno = new Aluno { UsuarioId = usuario.Id, Matricula = request.Matricula }; await _uow.CommitAsync(); await VincularDisciplinasEmAluno(new VincularAlunoDisciplinasRequest(request.Disciplinas, usuario.Id)); } return(new AppResponse(true, MSG_CRIAR_ALUNO, new AlunoComDisciplinaDTO(usuario.Aluno))); }
public async Task <IActionResult> Post([FromBody] CriarAlunoRequest request) { AppResponse resposta = await _usuarioServico.CriarAluno(request); if (resposta.Sucesso) { return(Ok(resposta)); } return(BadRequest(resposta)); }
public async Task <AppResponse> ImportarAlunos(ImportarAlunos importarAlunos) { var erros = new List <string>(); if (importarAlunos.Arquivo.Length == 0) { return(new AppResponse(false, "Arquivo não informado ou corrompido.")); } else { // copiando arquivo... var filePath = Path.GetTempFileName(); string destino = Path.Combine(_hostingEnvironment.WebRootPath, "Importacoes", Guid.NewGuid() + ".xlsx"); using (var stream = new FileStream(destino, FileMode.Create)) await importarAlunos.Arquivo.CopyToAsync(stream); Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR"); IWorkbook workbook = new XSSFWorkbook(); ISheet sheet; using (var stream = new FileStream(destino, FileMode.Open)) { stream.Position = 0; XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream); sheet = xssWorkbook.GetSheetAt(0); } int linha = 0; var usuarios = new List <CriarAlunoRequest>(); foreach (IRow row in sheet) { if (linha == 0) { linha++; continue; } string nome = row.GetCell(0).ToString(); string login = row.GetCell(1).ToString(); string email = row.GetCell(2).ToString(); string matricula = row.GetCell(3).ToString(); var usuario = new CriarAlunoRequest(nome, login, email, "123456", new List <int>()) { Matricula = matricula }; usuarios.Add(usuario); linha++; } foreach (var criarAlunoRequest in usuarios) { AppResponse resposta = await CriarAluno(criarAlunoRequest); if (!resposta.Sucesso) { erros.Add(string.Join(",", resposta.Erros)); } } } if (erros.Count == 0) { return(new AppResponse(true, "Importação realizada com sucesso.")); } else { return(new AppResponse(true, $"Importação realizada, porem ocorreram alguns erros:\n {string.Join("\n",erros)}")); } }