예제 #1
0
        public ActionResult EditUserSetting(UserSettingViewModel model)
        {
            var result = new { Success = "true", Message = "Success" };

            if (ModelState.IsValid)
            {
                try
                {
                    var userId = User.Identity.GetUserId();

                    if (model.Id == 0) //setting for the user doesnt exist yet
                    {
                        var userSetting = new UserSetting()
                        {
                            ApplicationUserID = userId,
                            SettingId         = model.SettingId,
                            SettingValue      = model.SettingValue
                        };

                        _settingService.CreateUserSetting(userSetting);
                    }
                    else
                    {
                        var setting = _settingService.GetUserSettingById(model.Id);

                        if (setting != null)
                        {
                            setting.SettingValue = model.SettingValue;

                            _settingService.UpdateUserSetting(setting);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e, e.Message);
                    result = new { Success = "false", Message = WebResources.ErrorMessage };
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                var error = ModelState.Values.SelectMany(v => v.Errors).FirstOrDefault().ErrorMessage;

                result = new { Success = "false", Message = error };

                return(Json(result, JsonRequestBehavior.AllowGet));
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }