public async Task UpdateUserAsyncTest()
        {
            // Create user
            var created = await UserHelper.CreateNewUserAsync();

            // Get auth token
            await AppContext.LoginAsync(new UsernamePasswordCredentials(created.Username, created.Password));

            // Update user
            created.Username = "******" + Unique.String;
            created.Email = "john.doe@" + Unique.String + ".com";
            created.DateOfBirth = DateTime.Today.AddYears(-30);
            created.FirstName = "John_updated";
            created.LastName = "Doe_updated";
            created.Phone = "999-888-1234";
            created.Location = new Geocode(20, 21);

            var updateRequest = new UpdateUserRequest() { Id = created.Id };
            updateRequest.PropertyUpdates["username"] = created.Username;
            updateRequest.PropertyUpdates["email"] = created.Email;
            updateRequest.PropertyUpdates["firstname"] = created.FirstName;
            updateRequest.PropertyUpdates["lastname"] = created.LastName;
            updateRequest.PropertyUpdates["phone"] = created.Phone;
            updateRequest.PropertyUpdates["location"] = created.Location.ToString();
            updateRequest.PropertyUpdates["birthdate"] = created.DateOfBirth.Value.ToString("yyyy-MM-dd");
            var response = await updateRequest.ExecuteAsync();

            // Ensure fields are updated
            Assert.IsNotNull(response, "Update user response is null.");
            Assert.IsNotNull(response.Status, "Update user response.status is null.");
            if (response.Status.IsSuccessful == false)
                Assert.Fail(response.Status.Message);
            Assert.IsNotNull(response.User);


            // Get updated user (just to be sure)
            var updated = await UserHelper.GetExistingUserAsync(created.Id);
            Console.WriteLine("Matching existing with updated user.");
            UserHelper.MatchUsers(updated, created);
        }
        protected override async Task<Entity> UpdateAsync(IDictionary<string, object> propertyUpdates, IDictionary<string, string> attributeUpdates, IEnumerable<string> addedTags, IEnumerable<string> removedTags, int specificRevision)
        {
            var request = new UpdateUserRequest()
            {
                SessionToken = AppacitiveContext.SessionToken,
                Environment = AppacitiveContext.Environment,
                UserToken = AppacitiveContext.UserToken,
                Verbosity = AppacitiveContext.Verbosity,
                Revision = specificRevision,
                UserId = this.Id
            };

            if (propertyUpdates != null && propertyUpdates.Count > 0)
                propertyUpdates.For(x => request.PropertyUpdates[x.Key] = x.Value);
            if (attributeUpdates != null && attributeUpdates.Count > 0)
                attributeUpdates.For(x => request.AttributeUpdates[x.Key] = x.Value);

            if (addedTags != null)
                request.AddedTags.AddRange(addedTags);
            if (removedTags != null)
                request.RemovedTags.AddRange(removedTags);

            // Check if an update is needed.
            if (request.PropertyUpdates.Count == 0 &&
                request.AttributeUpdates.Count == 0 &&
                request.AddedTags.Count == 0 &&
                request.RemovedTags.Count == 0)
                return null;

            var response = await request.ExecuteAsync();
            if (response.Status.IsSuccessful == false)
                throw response.Status.ToFault();

            // 3. Update the last known state based on the differences
            Debug.Assert(response.User != null, "If status is successful, then updated user should not be null.");
            return response.User;
        }
Beispiel #3
0
        protected override async Task<Entity> UpdateAsync(IDictionary<string, object> propertyUpdates, IDictionary<string, string> attributeUpdates, IEnumerable<string> addedTags, IEnumerable<string> removedTags, int specificRevision, ApiOptions options, bool forceUpdate)
        {
            var request = new UpdateUserRequest()
            {
                Revision = specificRevision,
                Id = this.Id
            };
            ApiOptions.Apply(request, options);
            if (propertyUpdates != null && propertyUpdates.Count > 0)
                propertyUpdates.For(x => request.PropertyUpdates[x.Key] = x.Value);
            if (attributeUpdates != null && attributeUpdates.Count > 0)
                attributeUpdates.For(x => request.AttributeUpdates[x.Key] = x.Value);

            if (addedTags != null)
                request.AddedTags.AddRange(addedTags);
            if (removedTags != null)
                request.RemovedTags.AddRange(removedTags);

            // Check if acls are to be added
            request.AllowClaims.AddRange(this.Acl.Allowed);
            request.DenyClaims.AddRange(this.Acl.Denied);
            request.ResetClaims.AddRange(this.Acl.Reset);

            // Check if an update is needed.
            if (request.PropertyUpdates.Count == 0 &&
                request.AttributeUpdates.Count == 0 &&
                request.AddedTags.Count == 0 &&
                request.RemovedTags.Count == 0 &&
                request.AllowClaims.Count == 0 &&
                request.DenyClaims.Count == 0 &&
                request.ResetClaims.Count == 0 && 
                forceUpdate == false)
                return null;

            var response = await request.ExecuteAsync();
            if (response.Status.IsSuccessful == false)
                throw response.Status.ToFault();

            // 3. Update the last known state based on the differences
            Debug.Assert(response.User != null, "If status is successful, then updated user should not be null.");
            return response.User;
        }