Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] CriarProfessorRequest request)
        {
            AppResponse response = await _usuarioService.CriarProfessor(request);

            if (response.Sucesso)
            {
                return(Ok(response));
            }

            return(BadRequest(response));
        }
Exemplo n.º 2
0
        public async Task <AppResponse> CriarProfessor(CriarProfessorRequest request)
        {
            var usuario = new Usuario(request.Nome, request.UserName, request.Email);

            if (!usuario.TaValido())
            {
                return(new AppResponse(false, ERRO_BASE, usuario.ObterErros()));
            }

            IdentityResult result = await _userManager.CreateAsync(usuario, request.Senha);

            if (!result.Succeeded)
            {
                return(new AppResponse(false, ERRO_BASE, IdentityHelper.ObterErros(result)));
            }
            else
            {
                IdentityResult resultRole = await _userManager.AddToRoleAsync(usuario, ROLE_PROFESSOR);

                if (!resultRole.Succeeded)
                {
                    return(new AppResponse(false, ERRO_BASE, IdentityHelper.ObterErros(result)));
                }

                usuario.Professor = new Professor {
                    UsuarioId = usuario.Id
                };
                await _uow.CommitAsync();
            }

            // vinculando disciplinas no professor
            if (request.Disciplinas != null)
            {
                List <Disciplina> disciplinasInformadas = await _uow.Disciplinas.ObterTodas(request.Disciplinas);

                if (disciplinasInformadas != null)
                {
                    foreach (Disciplina disciplinaInformada in disciplinasInformadas)
                    {
                        disciplinaInformada.AdicionarProfessor(usuario.Professor);
                    }
                }

                await _uow.CommitAsync();
            }
            return(new AppResponse(true, MSG_SUCESSO, new ProfessorComDisciplinaDTO(usuario)));
        }