Пример #1
0
 private void EditUser(User user)
 {
     if (user.Roles.All(x => x != UserRole.Admin))
     {
         editUserRequest.Raise(new EditUserViewModel(usersRepository, user), Callback);
     }
 }
Пример #2
0
 public async Task<AsyncResult> CreateUser(User user)
 {
     var result = new AsyncResult();
     using (var client = httpClientFactory())
     {
         var data = JsonConvert.SerializeObject(user);
         using (var content = new StringContent(data, Encoding.UTF8, "application/json"))
         {
             var uri = new Uri("api/users", UriKind.Relative);
             using (var resp = await client.PostAsync(uri, content))
             {
                 if (resp.StatusCode == HttpStatusCode.Created)
                 {
                     result.Succeed = true;
                     return result;
                 }
                 if (resp.StatusCode == HttpStatusCode.InternalServerError)
                 {
                     result.ErrorMessage = "Произошла ошибка на сервере";
                 }
                 else if (resp.StatusCode == HttpStatusCode.BadRequest)
                 {
                     var err = await resp.Content.ReadAsStringAsync();
                     result.ErrorMessage = TranslateError(err);
                 }
                 return result;
             }
         }
     }
 }
Пример #3
0
        public EditUserViewModel(IUsersRepository repository, User user)
        {
            this.repository = repository;

            Title = user.UserName;

            Roles = new Dictionary<string, string>
            {
                { UserRole.Editor, RoleToStringConverter.RoleTranslations[UserRole.Editor] },
                { UserRole.User, RoleToStringConverter.RoleTranslations[UserRole.User] },
            };
            Role = user.Roles[0];

            SaveCommand = new DelegateCommand(Save);
            CloseCommand = new DelegateCommand(Close);
        }
Пример #4
0
        private async void Save()
        {
            var user = new User
            {
                UserName = Title,
                Roles = new[] { Role },
            };

            IsBusy = true;
            var task = await repository.UpdateUser(user);
            IsBusy = false;
            if (task.Succeed)
            {
                Confirmed = true;
                Close();
            }
        }
Пример #5
0
        private async void Save()
        {
            Error = null;

            ValidateName();
            ValidatePassword();
            if (HasErrors) return;

            var user = new User
            {
                UserName = Name,
                Roles = new [] { Role },
                Password = Password,
            };

            IsBusy = true;
            var task = await repository.CreateUser(user);
            IsBusy = false;
            if (task.Succeed)
            {
                Confirmed = true;
                Close();
            }
            else
            {
                Error = task.ErrorMessage;
            }
        }
Пример #6
0
 public async Task<AsyncResult> UpdateUser(User user)
 {
     var result = new AsyncResult();
     using (var client = httpClientFactory())
     {
         var data = JsonConvert.SerializeObject(user);
         using (var content = new StringContent(data, Encoding.UTF8, "application/json"))
         {
             var uri = new Uri("api/users", UriKind.Relative);
             using (var resp = await client.PutAsync(uri, content))
             {
                 if (resp.StatusCode == HttpStatusCode.OK)
                 {
                     result.Succeed = true;
                 }
             }
         }
     }
     return result;
 }