/// <summary> /// 赞或者取消赞 /// </summary> /// <param name="personId">PersonID</param> /// <param name="entityId">EntityID</param> /// <param name="entityTable">EntityTable</param> /// <returns></returns> public async Task<Tuple<bool, int>> LikeAsync(long personId, long entityId, Type entityTable) { bool isLike = false; //1. 获取EntityConcurruentLock对象,用于处理Concurrent。 EntityConcurrentLockHandler entityConcurrentLockHandler = new EntityConcurrentLockHandler(_dbContext); var entityLikeLockObj = await entityConcurrentLockHandler.GetByIdAsync(personId); if(entityLikeLockObj == null) { EntityConcurrentLock entityLikeLock = new EntityConcurrentLock() { PersonID = personId }; entityConcurrentLockHandler.Add(entityLikeLock); } else { entityConcurrentLockHandler.MarkAsModified(entityLikeLockObj); } //2. 获取EntityLike对象。 var entityLikeObj = await Fetch(x => x.PersonID == personId && x.EntityID == entityId && x.EntityTableName == entityTable.Name).SingleOrDefaultAsync(); //3. 如果不存在则为点赞,存在则取消赞。 if(entityLikeObj == null) { entityLikeObj = new EntityLike() { PersonID = personId, EntityID = entityId, EntityTableName = entityTable.Name }; Add(entityLikeObj); isLike = true; } else { MarkAsDeleted(entityLikeObj); isLike = false; } await _dbContext.SaveChangesAsync(); //4. 获取此点赞的Entity的总赞数。 int entityLikeCount = await CountAsync(x => x.EntityID == entityId && x.EntityTableName == entityTable.Name); return new Tuple<bool, int>(isLike, entityLikeCount); }
/// <summary> /// 收藏或者取消收藏 /// </summary> /// <param name="personId">PersonID</param> /// <param name="blogId">BlogID</param> /// <returns></returns> public async Task<bool> CollectAsync(long personId, long blogId) { bool isFavorite = false; //1. 获取EntityConcurruentLock对象,用于处理Concurrent。 EntityConcurrentLockHandler entityConcurrentLockHandler = new EntityConcurrentLockHandler(_dbContext); var entityLikeLockObj = await entityConcurrentLockHandler.GetByIdAsync(personId); if (entityLikeLockObj == null) { EntityConcurrentLock entityLikeLock = new EntityConcurrentLock() { PersonID = personId }; entityConcurrentLockHandler.Add(entityLikeLock); } else { entityConcurrentLockHandler.MarkAsModified(entityLikeLockObj); } //2. 获取Favorite对象。 var favoriteObj = await Fetch(x => x.PersonID == personId && x.BlogID == blogId).SingleOrDefaultAsync(); //3. 如果Favorite不存在则为收藏,存在则取消收藏。 if(favoriteObj == null) { favoriteObj = new Favorite() { PersonID = personId, BlogID = blogId }; Add(favoriteObj); isFavorite = true; } else { MarkAsDeleted(favoriteObj); isFavorite = false; } await _dbContext.SaveChangesAsync(); return isFavorite; }