Ejemplo n.º 1
0
        public async Task <IActionResult> saveUSer([FromBody] ApplicationUserSaveModelDtos model)
        {
            if (model == null)
            {
                return(BadRequest(ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            var checkduplicate = _unitofWork.applicationUser.GetAll().Where(x => x.PhoneNumber == model.phoneNumber.Trim()).FirstOrDefault();

            if (checkduplicate == null)
            {
                string rolename = string.Empty;
                if (model.usertype.ToUpper().Trim() == "Buyer".ToUpper().Trim())
                {
                    rolename = SD.Role_Buyer;
                }
                else if (model.usertype.ToUpper().Trim() == "Seller".ToUpper().Trim())
                {
                    rolename = SD.Role_Seller;
                }

                var user = new ApplicationUser
                {
                    name        = model.name,
                    UserName    = model.phoneNumber,
                    PhoneNumber = model.phoneNumber,
                    Email       = model.email,
                    createddate = DateTime.Now,
                    companyName = model.companyName,
                    cityid      = model.cityid
                };


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

                if (result.Succeeded)
                {
                    if (!await _roleManager.RoleExistsAsync(SD.Role_Buyer))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_Buyer));
                    }

                    if (!await _roleManager.RoleExistsAsync(SD.Role_Seller))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_Seller));
                    }
                    if (!await _roleManager.RoleExistsAsync(SD.Role_CHA))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_CHA));
                    }



                    // await _userManager.AddToRoleAsync(user, SD.Role_Admin);
                    if (model.usertype.ToUpper().Trim() == "Buyer".ToUpper().Trim())
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_Buyer);
                    }
                    else if (model.usertype.ToUpper().Trim() == "Seller".ToUpper().Trim())
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_Seller);
                    }
                    else if (model.usertype.ToUpper().Trim() == "CHA".ToUpper().Trim())
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_CHA);
                    }


                    string output      = JsonConvert.SerializeObject(user);
                    string finalResult = "{\"success\" : 1, \"message\" : \" User Saved Successfully\", \"data\" :" + output + "}";

                    return(Ok(finalResult));
                }

                else
                {
                    ModelState.AddModelError("", $"Something went wrong saving record");
                    return(StatusCode(500, ModelState));
                }
            }
            else
            {
                ModelState.AddModelError("", "This Mobile No Exists!");
                return(StatusCode(404, ModelState));
            }
        }
        public async Task <IActionResult> SaveUser([FromBody] ApplicationUserSaveModelDtos model)
        {
            if (model == null)
            {
                return(BadRequest(ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            var checkduplicate = _unitofWork.applicationUser.GetAll().Where(x => x.PhoneNumber == model.phoneNumber.Trim()).FirstOrDefault();

            if (checkduplicate == null)
            {
                string rolename = string.Empty;
                if (model.usertype.ToUpper().Trim() == "Buyer".ToUpper().Trim())
                {
                    rolename = SD.Role_Buyer;
                }
                else if (model.usertype.ToUpper().Trim() == "Seller".ToUpper().Trim())
                {
                    rolename = SD.Role_Seller;
                }

                var user = new ApplicationUser
                {
                    name        = model.name,
                    UserName    = model.phoneNumber,
                    PhoneNumber = model.phoneNumber,
                    Email       = model.email,
                    createddate = DateTime.Now
                };


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

                if (result.Succeeded)
                {
                    if (!await _roleManager.RoleExistsAsync(SD.Role_Buyer))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_Buyer));
                    }

                    if (!await _roleManager.RoleExistsAsync(SD.Role_Seller))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_Seller));
                    }

                    //await _userManager.AddToRoleAsync(user, SD.Role_Admin);
                    if (model.usertype.ToUpper().Trim() == "Buyer".ToUpper().Trim())
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_Buyer);
                    }
                    else if (model.usertype.ToUpper().Trim() == "Seller".ToUpper().Trim())
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_Seller);
                    }

                    string s = HttpContext.GetRequestedApiVersion().ToString();

                    return(CreatedAtRoute("GetUserDetails", new { Version = HttpContext.GetRequestedApiVersion().ToString(), id = user.Id }, user));
                    //      return Ok(user);
                }

                else
                {
                    ModelState.AddModelError("", $"Something went wrong saving record");
                    return(StatusCode(500, ModelState));
                }
            }
            else
            {
                ModelState.AddModelError("", "This Mobile No Exists!");
                return(StatusCode(404, ModelState));
            }
        }