public DateTime GetTime3()
        {
            DateTime now = DateTime.Now;

            using (var uow = _freesql.CreateUnitOfWork())
            {
                ICapTransaction trans = uow.BeginTransaction(_capPublisher, false);
                var             repo  = uow.GetRepository <WeatherForecast>();

                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "summary",
                    TemperatureC = 100
                });

                _capPublisher.Publish("time", now);
                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "summarysuy",
                    TemperatureC = 200
                });
                trans.Commit();
                //uow.Commit();
            }

            return(now);
        }
Beispiel #2
0
        public CapEFDbTransaction(ICapTransaction transaction)
        {
            _transaction = transaction;
            var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction !;

            TransactionId = dbContextTransaction.TransactionId;
        }
Beispiel #3
0
        public DateTime FreeSqlWithTransaction()
        {
            DateTime now = DateTime.Now;

            using (var uow = _freeSql.CreateUnitOfWork())
            {
                using ICapTransaction trans = uow.BeginTransaction(_capBus, false);
                var repo = uow.GetRepository <WeatherForecast>();

                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "summary",
                    TemperatureC = 100
                });

                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "11222",
                    TemperatureC = 200
                });
                _capBus.Publish("freesql.time", now);
                trans.Commit();
            }

            return(now);
        }
Beispiel #4
0
        protected override Task ExecuteAsync(CapPublishedMessage message, ICapTransaction transaction,
                                             CancellationToken cancel = default(CancellationToken))
        {
            var insertOptions = new InsertOneOptions {
                BypassDocumentValidation = false
            };

            var collection = _client
                             .GetDatabase(_options.DatabaseName)
                             .GetCollection <PublishedMessage>(_options.PublishedCollection);

            var store = new PublishedMessage()
            {
                Id         = message.Id,
                Name       = message.Name,
                Content    = message.Content,
                Added      = message.Added,
                StatusName = message.StatusName,
                ExpiresAt  = message.ExpiresAt,
                Retries    = message.Retries,
                Version    = _options.Version,
            };

            if (NotUseTransaction)
            {
                return(collection.InsertOneAsync(store, insertOptions, cancel));
            }

            var dbTrans = (IClientSessionHandle)transaction.DbTransaction;

            return(collection.InsertOneAsync(dbTrans, store, insertOptions, cancel));
        }
        public DateTime Flush(int id = 0)
        {
            DateTime now = DateTime.Now;

            using (var uow = _freeSql.CreateUnitOfWork())
            {
                //这个不能使用using,因为这个using掉,uow.Dispose()时就会导致FreeSql,提示cannot access dispose transaction
                ICapTransaction trans = uow.BeginTransaction(_capBus, false);
                var             repo  = uow.GetRepository <WeatherForecast>();
                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "summary" + (id == 1? "summarysummarysummarysummarysummarysummarysummarysummarysummarysummarysummary" : ""),
                    TemperatureC = 100
                });

                if (id == 0)
                {
                    throw new Exception("异常,事务不正常!!");
                }

                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "11222",
                    TemperatureC = 200
                });

                _capBus.Publish("FreeSqlController.time", now);

                uow.Commit(trans);
            }

            return(now);
        }
Beispiel #6
0
        public async Task DeleteAsync(long subscribeUserId)
        {
            bool any = await _userSubscribeRepository.Select.AnyAsync(r =>
                                                                      r.CreateUserId == _currentUser.Id && r.SubscribeUserId == subscribeUserId);

            if (!any)
            {
                throw new LinCmsException("已取消关注");
            }

            using IUnitOfWork unitOfWork         = _unitOfWorkManager.Begin();
            using ICapTransaction capTransaction = unitOfWork.BeginTransaction(_capBus, false);

            await _userSubscribeRepository.DeleteAsync(r =>
                                                       r.SubscribeUserId == subscribeUserId && r.CreateUserId == _currentUser.Id);

            await _capBus.PublishAsync("NotificationController.Post", new CreateNotificationDto()
            {
                NotificationType       = NotificationType.UserLikeUser,
                NotificationRespUserId = subscribeUserId,
                UserInfoId             = _currentUser.Id ?? 0,
                CreateTime             = DateTime.Now,
                IsCancel = true
            });

            await capTransaction.CommitAsync();
        }
Beispiel #7
0
        public async Task <UnifyResponseDto> DeleteMyComment(Guid id)
        {
            Comment comment = await _commentAuditBaseRepository.Select.Where(r => r.Id == id).FirstAsync();

            if (comment == null)
            {
                return(UnifyResponseDto.Error("该评论已删除"));
            }

            if (comment.CreateUserId != _currentUser.Id)
            {
                return(UnifyResponseDto.Error("无权限删除他人的评论"));
            }

            using (IUnitOfWork uow = _unitOfWorkManager.Begin())
            {
                using ICapTransaction trans = uow.BeginTransaction(_capBus, false);

                await _commentService.DeleteAsync(comment);

                await _capBus.PublishAsync("NotificationController.Post", new CreateNotificationDto()
                {
                    NotificationType = NotificationType.UserCommentOnArticle,
                    ArticleId        = comment.SubjectId,
                    UserInfoId       = (long)_currentUser.Id,
                    CommentId        = comment.Id,
                    IsCancel         = true
                });

                await trans.CommitAsync();
            }


            return(UnifyResponseDto.Success());
        }
Beispiel #8
0
        public async Task DeleteMyComment(Guid id)
        {
            Comment comment = await _commentRepository.Select.Where(r => r.Id == id).FirstAsync();

            if (comment == null)
            {
                throw new LinCmsException("该评论已删除");
            }

            if (comment.CreateUserId != CurrentUser.Id)
            {
                throw new LinCmsException("无权限删除他人的评论");
            }

            using ICapTransaction capTransaction = UnitOfWorkManager.Current.BeginTransaction(_capBus, false);

            await this.DeleteAsync(comment);

            await _capBus.PublishAsync(CreateNotificationDto.CreateOrCancelAsync, new CreateNotificationDto()
            {
                NotificationType = NotificationType.UserCommentOnArticle,
                ArticleId        = comment.SubjectId,
                UserInfoId       = (long)CurrentUser.Id,
                CommentId        = comment.Id,
                IsCancel         = true
            });

            capTransaction.Commit(UnitOfWorkManager.Current);
        }
        public DateTime Transaction(int id = 0)
        {
            DateTime now = DateTime.Now;

            using (var uow = _freeSql.CreateUnitOfWork())
            {
                using ICapTransaction trans = uow.BeginTransaction(_capBus, false);
                var repo = uow.GetRepository <WeatherForecast>();

                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "summary" + (id == 1?"summarysummarysummarysummarysummarysummarysummarysummarysummarysummarysummary":""),
                    TemperatureC = 100
                });
                _capBus.Publish("FreeSqlController.time", now);
                if (id == 0)
                {
                    throw  new Exception("异常,事务不正常!!");
                }
                repo.Insert(new WeatherForecast()
                {
                    Date         = now,
                    Summary      = "11222",
                    TemperatureC = 200
                });
                trans.Commit();
            }

            return(now);
        }
Beispiel #10
0
 public UowAsyncInterceptor(IUnitOfWork unitOfWork
                            , ICapPublisher capPublisher     = null
                            , ICapTransaction capTransaction = null)
 {
     _unitOfWork     = unitOfWork;
     _capPublisher   = capPublisher;
     _capTransaction = capTransaction;
 }
Beispiel #11
0
        public static ICapTransaction Begin(this ICapTransaction transaction,
                                            IDbContextTransaction dbTransaction, bool autoCommit = false)
        {
            transaction.DbTransaction = dbTransaction;
            transaction.AutoCommit    = autoCommit;

            return(transaction);
        }
Beispiel #12
0
        public static ICapTransaction Begin(this ICapTransaction transaction,
                                            IUnitOfWork unitOfWork, bool autoCommit = false)
        {
            transaction.DbTransaction = unitOfWork;
            transaction.AutoCommit    = autoCommit;

            return(transaction);
        }
Beispiel #13
0
        protected override Task ExecuteAsync(CapPublishedMessage message, ICapTransaction transaction,
                                             CancellationToken cancel = default(CancellationToken))
        {
            var connection = (InMemoryStorageConnection)ServiceProvider.GetService <IStorageConnection>();

            connection.PublishedMessages.Add(message);

            return(Task.CompletedTask);
        }
        public static ICapTransaction Begin(this ICapTransaction transaction,
                                            IClientSessionHandle dbTransaction, bool autoCommit = false)
        {
            if (!dbTransaction.IsInTransaction)
            {
                dbTransaction.StartTransaction();
            }

            transaction.DbTransaction = dbTransaction;
            transaction.AutoCommit    = autoCommit;

            return(transaction);
        }
        public async Task <UnifyResponseDto> CreateOrCancelAsync([FromBody] CreateUpdateUserLikeDto createUpdateUserLike)
        {
            using IUnitOfWork unitOfWork = _unitOfWorkManager.Begin();
            using ICapTransaction trans  = unitOfWork.BeginTransaction(_capBus, false);

            bool isCancel = await _userLikeService.CreateOrCancelAsync(createUpdateUserLike);

            await PublishUserLikeNotification(createUpdateUserLike, isCancel);

            trans.Commit();

            return(UnifyResponseDto.Success(isCancel == false? "点赞成功": "已取消点赞"));
        }
Beispiel #16
0
        public async Task Post(long subscribeUserId)
        {
            if (subscribeUserId == _currentUser.Id)
            {
                throw new LinCmsException("您无法关注自己");
            }

            LinUser linUser = _userRepository.Select.Where(r => r.Id == subscribeUserId).ToOne();

            if (linUser == null)
            {
                throw new LinCmsException("该用户不存在");
            }

            if (!linUser.IsActive())
            {
                throw new LinCmsException("该用户已被拉黑");
            }

            bool any = _userSubscribeRepository.Select.Any(r =>
                                                           r.CreateUserId == _currentUser.Id && r.SubscribeUserId == subscribeUserId);

            if (any)
            {
                throw new LinCmsException("您已关注该用户");
            }

            using (IUnitOfWork unitOfWork = _unitOfWorkManager.Begin())
            {
                using ICapTransaction capTransaction = unitOfWork.BeginTransaction(_capBus, false);

                UserSubscribe userSubscribe = new UserSubscribe()
                {
                    SubscribeUserId = subscribeUserId
                };
                await _userSubscribeRepository.InsertAsync(userSubscribe);

                await _capBus.PublishAsync("NotificationController.Post", new CreateNotificationDto()
                {
                    NotificationType       = NotificationType.UserLikeUser,
                    NotificationRespUserId = subscribeUserId,
                    UserInfoId             = _currentUser.Id ?? 0,
                    CreateTime             = DateTime.Now,
                });

                await capTransaction.CommitAsync();
            }
        }
        public DateTime FreeSqlTransaction(int id)
        {
            DateTime now = DateTime.Now;

            using (var uow = _freeSql.CreateUnitOfWork())
            {
                using ICapTransaction trans = uow.BeginTransaction(_capBus, false);
                var repo = uow.GetRepository <Book>();

                repo.Insert(new Book()
                {
                    Author       = "luoyunchong" + (id == 1?"luoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchong":""),
                    Summary      = "1",
                    Title        = "122",
                    IsDeleted    = false,
                    CreateTime   = DateTime.Now,
                    CreateUserId = 1
                });
                repo.Insert(new Book()
                {
                    Author       = "luoyunchong",
                    Summary      = "2",
                    Title        = "122",
                    IsDeleted    = false,
                    CreateTime   = DateTime.Now,
                    CreateUserId = 2
                });
                if (id == 0)
                {
                    throw new Exception("异常,事务不正常!!");
                }
                repo.Insert(new Book()
                {
                    Author       = "luoyunchong",
                    Summary      = "summary",
                    Title        = "122",
                    IsDeleted    = false,
                    CreateTime   = DateTime.Now,
                    CreateUserId = 3
                });

                _capBus.Publish("freesql.time", now);
                trans.Commit();
            }

            return(now);
        }
Beispiel #18
0
        public DateTime FreeSqlUnitOfWorkManagerTransaction(int id, [FromServices] IBaseRepository <Book> repo)
        {
            DateTime now = DateTime.Now;

            using (IUnitOfWork uow = _unitOfWorkManager.Begin())
            {
                ICapTransaction trans = _unitOfWorkManager.Current.BeginTransaction(_capBus, false);

                repo.Insert(new Book()
                {
                    Author       = "luoyunchong" + (id == 1 ? "luoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchongluoyunchong" : ""),
                    Summary      = "1",
                    Title        = "122",
                    IsDeleted    = false,
                    CreateTime   = DateTime.Now,
                    CreateUserId = 1
                });
                repo.Insert(new Book()
                {
                    Author       = "luoyunchong",
                    Summary      = "2",
                    Title        = "122",
                    IsDeleted    = false,
                    CreateTime   = DateTime.Now,
                    CreateUserId = 2
                });
                if (id == 0)
                {
                    throw new Exception("异常,事务不正常!!");
                }
                repo.Insert(new Book()
                {
                    Author       = "luoyunchong",
                    Summary      = "summary",
                    Title        = "122",
                    IsDeleted    = false,
                    CreateTime   = DateTime.Now,
                    CreateUserId = 3
                });

                _capBus.Publish("freesql.time", now);
                trans.Commit(uow);
            }

            return(now);
        }
Beispiel #19
0
        public async Task <UnifyResponseDto> CreateAsync([FromBody] CreateCommentDto createCommentDto)
        {
            using IUnitOfWork uow       = _unitOfWorkManager.Begin();
            using ICapTransaction trans = uow.BeginTransaction(_capBus, false);

            Comment comment = _mapper.Map <Comment>(createCommentDto);
            await _commentAuditBaseRepository.InsertAsync(comment);

            if (createCommentDto.RootCommentId.HasValue)
            {
                await _commentAuditBaseRepository.UpdateDiy
                .Set(r => r.ChildsCount + 1)
                .Where(r => r.Id == createCommentDto.RootCommentId)
                .ExecuteAffrowsAsync();
            }

            switch (createCommentDto.SubjectType)
            {
            case 1:
                await _articleRepository.UpdateDiy
                .Set(r => r.CommentQuantity + 1)
                .Where(r => r.Id == createCommentDto.SubjectId)
                .ExecuteAffrowsAsync();

                break;
            }

            if (_currentUser.Id != createCommentDto.RespUserId)
            {
                await _capBus.PublishAsync("NotificationController.Post", new CreateNotificationDto()
                {
                    NotificationType       = NotificationType.UserCommentOnArticle,
                    ArticleId              = createCommentDto.SubjectId,
                    NotificationRespUserId = createCommentDto.RespUserId,
                    UserInfoId             = _currentUser.Id ?? 0,
                    CreateTime             = comment.CreateTime,
                    CommentId              = comment.Id
                });
            }

            await trans.CommitAsync();

            return(UnifyResponseDto.Success("评论成功"));
        }
        protected override async Task ExecuteAsync(CapPublishedMessage message,
                                                   ICapTransaction transaction = null,
                                                   CancellationToken cancel    = default(CancellationToken))
        {
            if (transaction == null)
            {
                using (var connection = new MySqlConnection(_options.ConnectionString))
                {
                    await connection.ExecuteAsync(PrepareSql(), message);

                    return;
                }
            }

            var dbTrans = transaction.DbTransaction as IDbTransaction;

            var conn = dbTrans?.Connection;
            await conn.ExecuteAsync(PrepareSql(), message, dbTrans);
        }
Beispiel #21
0
        public async Task CreateAsync(CreateCommentDto createCommentDto)
        {
            using IUnitOfWork uow       = UnitOfWorkManager.Begin();
            using ICapTransaction trans = uow.BeginTransaction(_capBus, false);

            Comment comment = Mapper.Map <Comment>(createCommentDto);
            await _commentRepository.InsertAsync(comment);

            if (createCommentDto.RootCommentId.HasValue)
            {
                await _commentRepository.UpdateDiy
                .Set(r => r.ChildsCount + 1)
                .Where(r => r.Id == createCommentDto.RootCommentId)
                .ExecuteAffrowsAsync();
            }

            switch (createCommentDto.SubjectType)
            {
            case 1:
                await _articleRepository.UpdateDiy
                .Set(r => r.CommentQuantity + 1)
                .Where(r => r.Id == createCommentDto.SubjectId)
                .ExecuteAffrowsAsync();

                break;
            }

            if (CurrentUser.Id != createCommentDto.RespUserId)
            {
                await _capBus.PublishAsync(CreateNotificationDto.CreateOrCancelAsync, new CreateNotificationDto()
                {
                    NotificationType       = NotificationType.UserCommentOnArticle,
                    ArticleId              = createCommentDto.SubjectId,
                    NotificationRespUserId = createCommentDto.RespUserId,
                    UserInfoId             = CurrentUser.Id ?? 0,
                    CreateTime             = comment.CreateTime,
                    CommentId              = comment.Id
                });
            }

            await trans.CommitAsync();
        }
Beispiel #22
0
        protected override Task ExecuteAsync(CapPublishedMessage message, ICapTransaction transaction,
                                             CancellationToken cancel = default(CancellationToken))
        {
            var insertOptions = new InsertOneOptions {
                BypassDocumentValidation = false
            };

            var collection = _client
                             .GetDatabase(_options.DatabaseName)
                             .GetCollection <CapPublishedMessage>(_options.PublishedCollection);

            if (NotUseTransaction)
            {
                return(collection.InsertOneAsync(message, insertOptions, cancel));
            }

            var dbTrans = (IClientSessionHandle)transaction.DbTransaction;

            return(collection.InsertOneAsync(dbTrans, message, insertOptions, cancel));
        }
        private void PublishUserLikeNotification(CreateUpdateUserLikeDto createUpdateUserLike)
        {
            //根据用户点赞类型:文章、评论,得到消息的NotificationRespUserId的值
            var createNotificationDto = new CreateNotificationDto()
            {
                UserInfoId = _currentUser.Id ?? 0,
                CreateTime = DateTime.Now,
            };

            switch (createUpdateUserLike.SubjectType)
            {
            case UserLikeSubjectType.UserLikeArticle:

                Article subjectArticle = _articleAuditBaseRepository.Where(r => r.Id == createUpdateUserLike.SubjectId).ToOne();

                createNotificationDto.NotificationRespUserId = subjectArticle.CreateUserId;
                createNotificationDto.NotificationType       = NotificationType.UserLikeArticle;
                createNotificationDto.ArticleId = createUpdateUserLike.SubjectId;
                break;

            case UserLikeSubjectType.UserLikeComment:

                Comment subjectComment = _commentRepository.Where(r => r.Id == createUpdateUserLike.SubjectId).ToOne();

                createNotificationDto.NotificationRespUserId = subjectComment.CreateUserId;
                createNotificationDto.NotificationType       = NotificationType.UserLikeArticleComment;
                createNotificationDto.ArticleId = subjectComment.SubjectId;
                createNotificationDto.CommentId = createUpdateUserLike.SubjectId;
                break;
            }


            if (createNotificationDto.NotificationRespUserId != 0 && _currentUser.Id != createNotificationDto.NotificationRespUserId)
            {
                using ICapTransaction trans = UnitOfWork.BeginTransaction(_capBus, false);

                _capBus.Publish("NotificationController.Post", createNotificationDto);

                trans.Commit();
            }
        }
Beispiel #24
0
        /// <summary>
        /// 根据用户点赞类型:文章、评论,得到消息的NotificationRespUserId的值
        /// </summary>
        /// <param name="createUpdateUserLike"></param>
        /// <returns></returns>
        private async Task PublishUserLikeNotification(CreateUpdateUserLikeDto createUpdateUserLike)
        {
            var createNotificationDto = new CreateNotificationDto()
            {
                UserInfoId = _currentUser.Id ?? 0,
                CreateTime = DateTime.Now,
            };

            switch (createUpdateUserLike.SubjectType)
            {
            case UserLikeSubjectType.UserLikeArticle:

                Article subjectArticle = await _articleRepository.Where(r => r.Id == createUpdateUserLike.SubjectId).ToOneAsync();

                createNotificationDto.NotificationRespUserId = subjectArticle.CreateUserId;
                createNotificationDto.NotificationType       = NotificationType.UserLikeArticle;
                createNotificationDto.ArticleId = createUpdateUserLike.SubjectId;
                break;

            case UserLikeSubjectType.UserLikeComment:

                Comment subjectComment = await _commentRepository.Where(r => r.Id == createUpdateUserLike.SubjectId).ToOneAsync();

                createNotificationDto.NotificationRespUserId = subjectComment.CreateUserId;
                createNotificationDto.NotificationType       = NotificationType.UserLikeArticleComment;
                createNotificationDto.ArticleId = subjectComment.SubjectId;
                createNotificationDto.CommentId = createUpdateUserLike.SubjectId;
                break;
            }


            if (createNotificationDto.NotificationRespUserId != 0 && _currentUser.Id != createNotificationDto.NotificationRespUserId)
            {
                using ICapTransaction trans = _unitOfWorkManager.Begin().BeginTransaction(_capBus, false);

                _capBus.Publish("NotificationController.Post", createNotificationDto);

                trans.Commit();
            }
        }
Beispiel #25
0
        protected override async Task ExecuteAsync(CapPublishedMessage message, ICapTransaction transaction,
                                                   CancellationToken cancel = default(CancellationToken))
        {
            if (NotUseTransaction)
            {
                using (var connection = new SqlConnection(_options.ConnectionString))
                {
                    await connection.ExecuteAsync(PrepareSql(), message);

                    return;
                }
            }

            var dbTrans = transaction.DbTransaction as IDbTransaction;

            if (dbTrans == null && transaction.DbTransaction is IDbContextTransaction dbContextTrans)
            {
                dbTrans = dbContextTrans.GetDbTransaction();
            }

            var conn = dbTrans?.Connection;
            await conn.ExecuteAsync(PrepareSql(), message, dbTrans);
        }
        public static ICapTransaction Begin(this ICapTransaction transaction, bool autoCommit = false)
        {
            transaction.AutoCommit = autoCommit;

            return(transaction);
        }
 public static void Commit(this IUnitOfWork unitOfWork, ICapTransaction capTransaction)
 {
     unitOfWork.Commit();
     capTransaction.Flush();
 }
 public static void Flush(this ICapTransaction capTransaction)
 {
     capTransaction?.GetType().GetMethod("Flush", BindingFlags.Instance | BindingFlags.NonPublic)
     ?.Invoke(capTransaction, null);
 }
Beispiel #29
0
 protected abstract Task ExecuteAsync(CapPublishedMessage message,
                                      ICapTransaction transaction,
                                      CancellationToken cancel = default(CancellationToken));
Beispiel #30
0
 public CapMongoDbClientSessionHandle(ICapTransaction transaction)
 {
     _transaction   = transaction;
     _sessionHandle = (IClientSessionHandle)_transaction.DbTransaction !;
 }