Exemple #1
0
        public ActionResult SaveAdmin(AddAdministratorViewModel admin)
        {
            int id = (int)Session["super admin"];

            if (!ModelState.IsValid)
            {
                admin.Countries = _context.GetCountries();
                return(View("AddAdministrator", admin));
            }
            var adminInDb = _context.GetAdmin(admin.AddAdmin.Id);

            if (adminInDb == null)
            {
                var IsExist = _context.CheckUserIsExistOrNot(admin.AddAdmin.Email);
                if (IsExist)
                {
                    ModelState.AddModelError("Exist", "Email is Already Exists.");
                    admin.Countries = _context.GetCountries();
                    return(View("AddAdministrator", admin));
                }
                User addAdmin = new User
                {
                    RoleID          = _context.GetRolesByName("admin"),
                    FirstName       = admin.AddAdmin.FirstName,
                    LastName        = admin.AddAdmin.LastName,
                    EmailID         = admin.AddAdmin.Email,
                    Password        = "******",
                    IsEmailVerified = true,
                    CreatedDate     = DateTime.Now,
                    CreatedBy       = id,
                    ModifiedDate    = DateTime.Now,
                    ModifiedBy      = id,
                    IsActive        = true
                };
                int adminId = _context.AddUser(addAdmin);

                UserProfile adminDetails = new UserProfile
                {
                    UserID = adminId,
                    PhoneNumberCounrtyCode = admin.AddAdmin.CountryCodeId,
                    PhoneNumber            = admin.AddAdmin.PhoneNumber,
                    AddressLine1           = "Admin",
                    AddressLine2           = "Admin",
                    City    = "Admin",
                    State   = "Admin",
                    ZipCode = "Admin",
                    Country = "Admin",
                };
                _context.AddUserDetails(adminDetails);

                return(RedirectToAction("ManageAdministrator", "Admin"));
            }
            else
            {
                adminInDb.FirstName = admin.AddAdmin.FirstName;
                adminInDb.LastName  = admin.AddAdmin.LastName;
                adminInDb.EmailID   = admin.AddAdmin.Email;
                adminInDb.IsActive  = true;
                var adminDetails = _context.GetUserProfileDetails(adminInDb.ID);
                adminDetails.PhoneNumber            = admin.AddAdmin.PhoneNumber;
                adminDetails.PhoneNumberCounrtyCode = admin.AddAdmin.CountryCodeId;

                _context.UpdateUp();

                return(RedirectToAction("ManageAdministrator", "Admin"));
            }
        }