private void EditUser(User user) { if (user.Roles.All(x => x != UserRole.Admin)) { editUserRequest.Raise(new EditUserViewModel(usersRepository, user), Callback); } }
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; } } } }
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); }
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(); } }
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; } }
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; }