Пример #1
0
 public JsonResult Edit(UserManageModel user, HttpPostedFileBase avatar)
 {
     if(ModelState.IsValid)
         return Json(_userServices.SaveUserManageModel(user, avatar));
     return Json(new ResponseModel
     {
         Success = false,
         Message = GetFirstValidationResults(ModelState).Message
     });
 }
Пример #2
0
        public ResponseModel SaveUserManageModel(UserManageModel model, HttpPostedFileBase avatar)
        {
            ResponseModel response;
            var user = GetById(model.Id);

            #region Edit User
            if (user != null)
            {
                user.FirstName = model.FirstName;
                user.LastName = model.LastName;
                user.Email = model.Email;
                user.Status = model.Status;
                user.Phone = model.Phone;
                user.Gender = model.Gender;
                user.About = model.About;
                user.Address = model.Address;
                user.IdentityNumber = model.IdentityNumber;
                user.LastLogin = model.LastLogin;
                user.Facebook = model.Facebook;
                user.Twitter = model.Twitter;
                user.Google = model.Google;
                user.AvatarFileName = model.AvatarFileName;
                user.BirthDay = model.BirthDay;

                user.Password = model.Password;

                var currentGroups = user.UserInGroups.Select(nc => nc.UserGroupId).ToList();
                foreach (var id in model.UserGroupIds)
                {
                    if (!model.UserGroupIds.Contains(id))
                    {
                        _userInGroupRepository.Delete(id);
                    }
                }
                foreach (var groupId in model.UserGroupIds)
                {
                    if (currentGroups.All(n => n != groupId))
                    {
                        var userInGroup = new UserInGroup
                        {
                            UserId = user.Id,
                            UserGroupId = groupId
                        };
                        _userInGroupRepository.Insert(userInGroup);
                    }
                }

                //Get page record order
                response = Update(user);
                if (response.Success && avatar != null)
                {
                    UploadAvatar(user.Id, avatar);
                }
                return response.SetMessage(response.Success ?
                    _localizedResourceServices.T("AdminModule:::Users:::Messages:::UpdateSuccessfully:::Update user successfully.")
                    : _localizedResourceServices.T("AdminModule:::Users:::Messages:::UpdateFailure:::Update user failed. Please try again later."));
            }
            #endregion

            user = new User
            {
                Id = model.Id,
                FirstName = model.FirstName,
                LastName = model.LastName,
                Email = model.Email,
                Password = model.Password,
                Status = model.Status,
                Phone = model.Phone,
                Gender = model.Gender,
                About = model.About,
                Address = model.Address,
                IdentityNumber = model.IdentityNumber,
                LastLogin = model.LastLogin,
                Facebook = model.Facebook,
                Twitter = model.Twitter,
                Google = model.Google,
                AvatarFileName = model.AvatarFileName,
                BirthDay = model.BirthDay,
                Created = model.Created,
                CreatedBy = model.CreatedBy,
                Updated = model.Updated,
                UpdatedBy = model.UpdatedBy,
            };

            response = Insert(user);
            foreach (var groupId in model.UserGroupIds)
            {
                var userInGroup = new UserInGroup
                {
                    UserId = user.Id,
                    UserGroupId = groupId
                };
                _userInGroupRepository.Insert(userInGroup);
            }
            if (response.Success && avatar != null)
            {
                UploadAvatar(user.Id, avatar);
            }
            return response.SetMessage(response.Success ?
                _localizedResourceServices.T("AdminModule:::Users:::Messages:::CreateSuccessfully:::Create user successfully.")
                : _localizedResourceServices.T("AdminModule:::Users:::Messages:::CreateFailure:::Create user failed. Please try again later."));
        }