Exemple #1
0
        public async Task <ActionResult> RegisterCooperativa(RegisterCooperativaViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName     = model.email,
                    Email        = model.email,
                    ativo        = true,
                    dataCadastro = DateTime.Now
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    NetGoogleGeocoding geoCoder = new NetGoogleGeocoding();

                    string endereco         = model.endereco + ", " + model.numero;
                    string enderecoConsulta = endereco + " - " + model.cidade + " - " + model.estado;

                    Cooperativas c = new Cooperativas();
                    c.cooperativaId = user.Id;
                    c.cnpj          = model.cnpj;
                    c.razaoSocial   = model.razaoSocial;
                    c.endereco      = endereco;
                    c.cidade        = model.cidade;
                    c.estado        = model.estado;

                    try
                    {
                        var response = geoCoder.GoogleGeocode(enderecoConsulta).GeoCodes[0];
                        if (response != null)
                        {
                            c.enderecoCoordenada = geoCoder.ConvertLatLonToDbGeometry(response.Longitude, response.Latitude);
                        }
                        else
                        {
                            c.enderecoCoordenada = null;
                        }
                    }
                    catch (Exception)
                    {
                        c.enderecoCoordenada = null;
                    }

                    Roles r = db.Roles.Find("2");
                    if (r == null)
                    {
                        r      = new Roles();
                        r.Id   = "2";
                        r.Name = "Cooperativa";
                        db.Roles.Add(r);
                    }

                    UsuarioRole ur = new UsuarioRole();
                    ur.UserId = user.Id;
                    ur.RoleId = "2";

                    db.UsuarioRole.Add(ur);

                    db.Cooperativas.Add(c);
                    db.SaveChanges();

                    LoginViewModel lvm = new LoginViewModel()
                    {
                        Email      = model.email,
                        Password   = model.Password,
                        RememberMe = false
                    };

                    return(await Login(lvm, null));
                }
                AddErrors(result);
            }

            ViewBag.retorno = @Url.Action("Login", "Account");

            return(View(model));
        }
Exemple #2
0
        public async Task <ActionResult> RegisterCliente(RegisterClienteViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName     = model.email,
                    Email        = model.email,
                    ativo        = true,
                    dataCadastro = DateTime.Now
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    NetGoogleGeocoding geoCoder = new NetGoogleGeocoding();

                    string endereco         = model.endereco + ", " + model.numero;
                    string enderecoConsulta = endereco + " - " + model.cidade + " - " + model.estado;

                    Clientes c = new Clientes();
                    c.clienteId = user.Id;
                    //c.cpf = model.cpf.Replace(".", "").Replace("-", ""); // Se Habilitar o cadastro com CPF, descomente está linha
                    c.tipo           = model.tipo.ToString();
                    c.nome           = model.nome;
                    c.endereco       = enderecoConsulta;
                    c.email          = model.email;
                    c.sexo           = model.sexo.ToString();
                    c.dataNascimento = model.dataNascimento;
                    if (model.telefone != null)
                    {
                        c.telefone = model.telefone.Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""); // adicionei o Replace (Teste) - Titans
                    }
                    c.celular       = model.celular.Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""); // adicionei o Replace (Teste) - Titans
                    c.cooperativaId = model.cooperativaId;

                    try
                    {
                        var response = geoCoder.GoogleGeocode(enderecoConsulta).GeoCodes[0];
                        if (response != null)
                        {
                            c.enderecoCoordenada = geoCoder.ConvertLatLonToDbGeometry(response.Longitude, response.Latitude);
                        }
                        else
                        {
                            c.enderecoCoordenada = null;
                        }
                    }
                    catch (Exception)
                    {
                        c.enderecoCoordenada = null;
                    }

                    Roles r = db.Roles.Find("1");
                    if (r == null)
                    {
                        r      = new Roles();
                        r.Id   = "1";
                        r.Name = "Cliente";
                        db.Roles.Add(r);
                    }

                    UsuarioRole ur = new UsuarioRole();
                    ur.UserId = user.Id;
                    ur.RoleId = "1";

                    db.UsuarioRole.Add(ur);

                    db.Clientes.Add(c);
                    db.SaveChanges();

                    LoginViewModel lvm = new LoginViewModel()
                    {
                        Email      = model.email,
                        Password   = model.Password,
                        RememberMe = false
                    };

                    return(await Login(lvm, null));
                }
                AddErrors(result);
            }

            #region ValidaçãoFalha
            var SexoList = new List <dynamic>();
            SexoList.Add(new { Id = "M", Text = "Masculino" });
            SexoList.Add(new { Id = "F", Text = "Feminino" });
            ViewBag.sexo = new SelectList(SexoList, "Id", "Text");

            var TipoList = new List <dynamic>();
            TipoList.Add(new { Id = "D", Text = "Doador" });
            TipoList.Add(new { Id = "V", Text = "Vendedor" });
            ViewBag.tipo = new SelectList(TipoList, "Id", "Text");

            var EstadoList = new List <dynamic>();
            EstadoList.Add(new { Id = "SE", Text = "Sergipe" });
            EstadoList.Add(new { Id = "BA", Text = "Bahia" });
            ViewBag.estado = new SelectList(EstadoList, "Id", "Text");

            ViewBag.cooperativas = new SelectList(db.Cooperativas, "cooperativaId", "cnpj", model.cooperativaId);

            ViewBag.retorno = @Url.Action("Login", "Account");

            #endregion

            return(View(model));
        }