Ejemplo n.º 1
0
 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;
             }
         }
     }
 }
Ejemplo n.º 2
0
 private void EditUser(User user)
 {
     if (user.Roles.All(x => x != UserRole.Admin))
     {
         editUserRequest.Raise(new EditUserViewModel(usersRepository, user), Callback);
     }
 }
Ejemplo n.º 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<ChildWindow>(Save);
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
 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;
 }
Ejemplo n.º 6
0
        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;
            }
        }