/// <summary> /// Update user relationship /// </summary> /// <param name="storageConsistencyMode">Consistency mode</param> /// <param name="relationshipHandle">Relationship handle</param> /// <param name="userHandle">User handle</param> /// <param name="relationshipUserHandle">Relationship user handle</param> /// <param name="appHandle">App handle</param> /// <param name="userRelationshipStatus">Relationship status</param> /// <param name="lastUpdatedTime">Last updated time</param> /// <param name="readUserRelationshipLookupEntity">Read user relationship lookup entity</param> /// <param name="store">store object</param> /// <param name="lookupTable">lookup table</param> /// <param name="feedTable">feed table</param> /// <param name="countTable">count table</param> /// <returns>Update follower relationship task</returns> private async Task UpdateUserRelationship( StorageConsistencyMode storageConsistencyMode, string relationshipHandle, string userHandle, string relationshipUserHandle, string appHandle, UserRelationshipStatus userRelationshipStatus, DateTime lastUpdatedTime, IUserRelationshipLookupEntity readUserRelationshipLookupEntity, CTStore store, ObjectTable lookupTable, FeedTable feedTable, CountTable countTable) { string objectKey = this.GetObjectKey(appHandle, relationshipUserHandle); string feedKey = this.GetFeedKey(appHandle, userRelationshipStatus); string countKey = this.GetCountKey(appHandle, userRelationshipStatus); Transaction transaction = new Transaction(); UserRelationshipFeedEntity relationshipFeedEntity = new UserRelationshipFeedEntity() { RelationshipHandle = relationshipHandle, UserHandle = relationshipUserHandle }; if (readUserRelationshipLookupEntity == null) { UserRelationshipLookupEntity newRelationshipLookupEntity = new UserRelationshipLookupEntity() { RelationshipHandle = relationshipHandle, LastUpdatedTime = lastUpdatedTime, UserRelationshipStatus = userRelationshipStatus, }; transaction.Add(Operation.Insert(lookupTable, userHandle, objectKey, newRelationshipLookupEntity)); if (userRelationshipStatus != UserRelationshipStatus.None) { transaction.Add(Operation.Insert(feedTable, userHandle, feedKey, relationshipHandle, relationshipFeedEntity)); transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, countKey)); } } else { UserRelationshipStatus oldUserRelationshipStatus = readUserRelationshipLookupEntity.UserRelationshipStatus; string oldRelationshipHandle = readUserRelationshipLookupEntity.RelationshipHandle; readUserRelationshipLookupEntity.RelationshipHandle = relationshipHandle; readUserRelationshipLookupEntity.UserRelationshipStatus = userRelationshipStatus; readUserRelationshipLookupEntity.LastUpdatedTime = lastUpdatedTime; string oldFeedKey = this.GetFeedKey(appHandle, oldUserRelationshipStatus); string oldCountKey = this.GetCountKey(appHandle, oldUserRelationshipStatus); transaction.Add(Operation.Replace(lookupTable, userHandle, objectKey, readUserRelationshipLookupEntity as UserRelationshipLookupEntity)); if (userRelationshipStatus == oldUserRelationshipStatus) { if (userRelationshipStatus != UserRelationshipStatus.None && relationshipHandle != oldRelationshipHandle) { transaction.Add(Operation.Delete(feedTable, userHandle, oldFeedKey, oldRelationshipHandle)); transaction.Add(Operation.Insert(feedTable, userHandle, feedKey, relationshipHandle, relationshipFeedEntity)); } } else { if (userRelationshipStatus != UserRelationshipStatus.None) { transaction.Add(Operation.Insert(feedTable, userHandle, feedKey, relationshipHandle, relationshipFeedEntity)); transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, countKey)); } if (oldUserRelationshipStatus != UserRelationshipStatus.None) { transaction.Add(Operation.Delete(feedTable, userHandle, oldFeedKey, oldRelationshipHandle)); transaction.Add(Operation.Increment(countTable, userHandle, oldCountKey, -1.0)); } } } await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode()); }
/// <summary> /// Update topic follower relationship. /// Follower user : someone who follows the topic. /// </summary> /// <param name="storageConsistencyMode">Consistency mode</param> /// <param name="relationshipHandle">Relationship handle</param> /// <param name="topicHandle">topic handle</param> /// <param name="relationshipUserHandle">Relationship user handle</param> /// <param name="appHandle">App handle</param> /// <param name="topicRelationshipStatus">Topic relationship status</param> /// <param name="lastUpdatedTime">Last updated time</param> /// <param name="readTopicRelationshipLookupEntity">Read topic relationship lookup entity</param> /// <returns>Update follower relationship task</returns> public async Task UpdateTopicFollowerRelationship( StorageConsistencyMode storageConsistencyMode, string relationshipHandle, string topicHandle, string relationshipUserHandle, string appHandle, TopicRelationshipStatus topicRelationshipStatus, DateTime lastUpdatedTime, ITopicRelationshipLookupEntity readTopicRelationshipLookupEntity) { CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.TopicFollowers); ObjectTable lookupTable = this.tableStoreManager.GetTable(ContainerIdentifier.TopicFollowers, TableIdentifier.TopicFollowersLookup) as ObjectTable; FeedTable feedTable = this.tableStoreManager.GetTable(ContainerIdentifier.TopicFollowers, TableIdentifier.TopicFollowersFeed) as FeedTable; CountTable countTable = this.tableStoreManager.GetTable(ContainerIdentifier.TopicFollowers, TableIdentifier.TopicFollowersCount) as CountTable; string objectKey = this.GetObjectKey(appHandle, relationshipUserHandle); string feedKey = this.GetFeedKey(appHandle, topicRelationshipStatus); string countKey = this.GetCountKey(appHandle, topicRelationshipStatus); Transaction transaction = new Transaction(); UserRelationshipFeedEntity relationshipFeedEntity = new UserRelationshipFeedEntity() { RelationshipHandle = relationshipHandle, UserHandle = relationshipUserHandle }; if (readTopicRelationshipLookupEntity == null) { // if readTopicRelationshipLookupEntity is null, then we are inserting a new relationship TopicRelationshipLookupEntity newRelationshipLookupEntity = new TopicRelationshipLookupEntity() { RelationshipHandle = relationshipHandle, LastUpdatedTime = lastUpdatedTime, TopicRelationshipStatus = topicRelationshipStatus, }; transaction.Add(Operation.Insert(lookupTable, topicHandle, objectKey, newRelationshipLookupEntity)); if (topicRelationshipStatus != TopicRelationshipStatus.None) { transaction.Add(Operation.Insert(feedTable, topicHandle, feedKey, relationshipHandle, relationshipFeedEntity)); transaction.Add(Operation.InsertOrIncrement(countTable, topicHandle, countKey)); } } else { // otherwise, we are updating an existing relationship TopicRelationshipStatus oldTopicRelationshipStatus = readTopicRelationshipLookupEntity.TopicRelationshipStatus; string oldRelationshipHandle = readTopicRelationshipLookupEntity.RelationshipHandle; string oldFeedKey = this.GetFeedKey(appHandle, oldTopicRelationshipStatus); string oldCountKey = this.GetCountKey(appHandle, oldTopicRelationshipStatus); readTopicRelationshipLookupEntity.RelationshipHandle = relationshipHandle; readTopicRelationshipLookupEntity.TopicRelationshipStatus = topicRelationshipStatus; readTopicRelationshipLookupEntity.LastUpdatedTime = lastUpdatedTime; transaction.Add(Operation.Replace(lookupTable, topicHandle, objectKey, readTopicRelationshipLookupEntity as TopicRelationshipLookupEntity)); if (topicRelationshipStatus == oldTopicRelationshipStatus) { if (topicRelationshipStatus != TopicRelationshipStatus.None && relationshipHandle != oldRelationshipHandle) { transaction.Add(Operation.Delete(feedTable, topicHandle, oldFeedKey, oldRelationshipHandle)); transaction.Add(Operation.Insert(feedTable, topicHandle, feedKey, relationshipHandle, relationshipFeedEntity)); } } else { if (topicRelationshipStatus != TopicRelationshipStatus.None) { transaction.Add(Operation.Insert(feedTable, topicHandle, feedKey, relationshipHandle, relationshipFeedEntity)); transaction.Add(Operation.Increment(countTable, topicHandle, countKey)); } if (oldTopicRelationshipStatus != TopicRelationshipStatus.None) { transaction.Add(Operation.Delete(feedTable, topicHandle, oldFeedKey, oldRelationshipHandle)); transaction.Add(Operation.Increment(countTable, topicHandle, oldCountKey, -1.0)); } } } await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode()); }