示例#1
0
        public async Task <Result <bool> > Collect(int topicID)
        {
            using (var uw = this.CreateUnitOfWork())
            {
                var topic = await uw.GetAsync <Topic>(t => t.ID == topicID && !t.IsDelete);

                if (topic == null)
                {
                    return(Result <bool> .ErrorResult("该主题不存在"));
                }

                var entity = await uw.GetAsync <TopicCollect>(t => t.TopicID == topicID && t.UserID == SecurityManager.CurrentUser.ID);

                bool result = false;

                if (entity == null)
                {
                    entity = new TopicCollect
                    {
                        CreateDate = DateTime.Now,
                        TopicID    = topicID,
                        UserID     = SecurityManager.CurrentUser.ID
                    };
                    await uw.InsertAsync(entity);

                    result = true;
                }
                else
                {
                    await uw.DeleteAsync(entity);
                }

                return(Result.SuccessResult(result));
            }
        }
        public async Task <JsonResult> Collecte(int id)
        {
            var loginUserName = User.Identity.Name;
            var loginUser     = await this._userRepo.FindAsync(p => p.Name == loginUserName);

            if (loginUser == null)
            {
                return(Json(new { status = "error" }));
            }
            loginUser.Collect_Topic_Count += 1;
            var topicCollect = new TopicCollect()
            {
                UserId         = loginUser.UserId,
                TopicId        = id,
                CreateDateTime = DateTime.Now
            };

            await this._userRepo.UpdateAsync(loginUser, false);

            var success = await this._topicCollectRepo.AddAsync(topicCollect);

            if (success)
            {
                return(Json(new { status = "success" }));
            }
            else
            {
                return(Json(new { status = "error" }));
            }
        }