コード例 #1
0
        public async Task <ActionResult <int> > Post([FromBody] DentistDto dentist)
        {
            int recordId;

            try
            {
                recordId = await _dentistRepository.AddAsync(dentist);
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }

            return(recordId);
        }
コード例 #2
0
        public async Task Create(DentistDataModel request)
        {
            var hasDentist = await _dentistRepository.GetAsync(request.Cpf);

            if (hasDentist is not null)
            {
                Notify("Já existe um dentista cadastrado com este cpf informado.");
                return;
            }

            if (request.BirthDate == DateTime.MinValue)
            {
                Notify("Data inválida");
                return;
            }

            var userId = Guid.NewGuid();

            if (request.CreateUser)
            {
                var user = new IdentityUser {
                    UserName = request.Email, Email = request.Email
                };

                userId = Guid.Parse(user.Id);

                var options = _userManager.Options.Password;

                var password = Utils.GeneratePassword(options.RequiredLength, options.RequireNonAlphanumeric, options.RequireDigit, options.RequireLowercase, options.RequireUppercase);

                var result = await _userManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    await _userManager.ConfirmEmailAsync(user, code);

                    await AddClaims(userId);

                    _emailService.SendEmail(user.Email, "Credenciais de Acesso - SisOdonto", "Credenciais", $"Usuário: {user.Email} <br> Senha: {password}");
                }
                else
                {
                    Notify("Erro ao criar usuário.");
                    return;
                }
            }

            var dentist = new Dentist(userId, request.BirthDate, request.Cep, request.City, request.Complement, request.Cpf.Replace(".", "").Replace("-", ""), request.District, request.Email, request.Name, request.Number, request.State, request.Street, request.Cro, request.Expertise);

            if (dentist.IsValid())
            {
                await _dentistRepository.AddAsync(dentist);
            }
            else
            {
                var userManager = await _userManager.FindByIdAsync(userId.ToString());

                if (userManager is not null)
                {
                    await _userManager.DeleteAsync(userManager);
                }

                Notify(dentist.ValidationResult);
                return;
            }

            if (await CommitAsync() is false)
            {
                Notify("Erro ao salvar dados.");

                var userManager = await _userManager.FindByIdAsync(userId.ToString());

                if (userManager is not null)
                {
                    await _userManager.DeleteAsync(userManager);
                }
            }
        }