public async Task<AsyncResult> CreateUser(User user) { var result = new AsyncResult(); var token = authStore.LoadToken(); using (var client = new BearerHttpClient(token.AccessToken)) { 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; } } } }
private void EditUser(User user) { if (user.Roles.All(x => x != UserRole.Admin)) { editUserRequest.Raise(new EditUserViewModel(usersRepository, user), Callback); } }
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<ChildWindow>(Save); }
private async void Save(ChildWindow window) { var user = new User { UserName = Title, Roles = new[] { Role }, }; IsBusy = true; var task = await repository.UpdateUser(user); IsBusy = false; if (task.Succeed) { Confirmed = true; } window.Close(); }
public async Task<AsyncResult> UpdateUser(User user) { var result = new AsyncResult(); var token = authStore.LoadToken(); using (var client = new BearerHttpClient(token.AccessToken)) { 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; }
private async void Save(ChildWindow window) { 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; window.Close(); } else { Error = task.ErrorMessage; } }