示例#1
0
        /// <summary>
        /// 转换成AskAnswer类型
        /// </summary>
        /// <returns>回答实体</returns>
        public AskAnswer AsAskAnswer()
        {
            AskAnswer askAnswer = null;

            //发布回答
            if (this.AnswerId == 0)
            {
                askAnswer            = AskAnswer.New();
                askAnswer.UserId     = UserContext.CurrentUser.UserId;
                askAnswer.Author     = UserContext.CurrentUser.DisplayName;
                askAnswer.IsBest     = false;
                askAnswer.QuestionId = this.QuestionId;
            }
            //编辑回答
            else
            {
                AskService askService = new AskService();
                askAnswer = askService.GetAnswer(this.AnswerId);
                askAnswer.LastModified = DateTime.UtcNow;
            }

            askAnswer.Body = this.Body;

            return(askAnswer);
        }
示例#2
0
        /// <summary>
        /// 采纳满意回答
        /// </summary>
        /// <remarks>
        /// 1.如果问题有悬赏则悬赏分值转移到回答者的帐户(如有交易税,需要扣除),通过EventModule实现
        /// 2.除了悬赏分的交易,回答被采纳,回答者还会获得新产生的积分,通过EventModule实现
        /// 3.注意需要发送通知给问题的关注者,通过EventModule实现
        /// 4.需要触发的事件:SetBestAnswer的OnAfter
        /// </remarks>
        /// <param name="question">问题实体</param>
        /// <param name="answer">回答实体</param>
        public void SetBestAnswer(AskQuestion question, AskAnswer answer)
        {
            if (!answer.IsBest)
            {
                answer.IsBest = true;

                //调用Service中的Update方法,以触发相应的事件,但是不更新审核状态
                this.UpdateAnswer(answer, false);
                //todo:jiangshl,改为EventModule处理
                //处理威望
                OwnerDataService ownerDataService = new OwnerDataService(TenantTypeIds.Instance().User());

                //用户获得威望值
                PointService pointService = new PointService();
                //提问者获取威望值
                PointItem questionPointItem = pointService.GetPointItem(PointItemKeys.Instance().Ask_AcceptedAnswer());
                ownerDataService.Change(question.UserId, OwnerDataKeys.Instance().UserAskReputation(), questionPointItem.ReputationPoints);
                //回答者获得威望
                PointItem AnswerPointItem = pointService.GetPointItem(PointItemKeys.Instance().Ask_AnswerWereAccepted());
                ownerDataService.Change(answer.UserId, OwnerDataKeys.Instance().UserAskReputation(), AnswerPointItem.ReputationPoints);

                //用户计数
                ownerDataService.Change(answer.UserId, OwnerDataKeys.Instance().AnswerAcceptCount(), 1);
            }
        }
示例#3
0
        /// <summary>
        /// 删除回答
        /// </summary>
        /// <remarks>
        /// 1.管理员删除被采纳的回答时,需要将问题状态变更为“待解决”
        /// 2.注意维护相关问题的回答数,调用UpdateAskQuestion,AnswerCount--
        /// 3.注意需要删除动态,通过EventModule处理;
        /// 4.注意需要扣除增加的积分,通过EventModule处理;
        /// 5.注意需要删除回答者对问题的关注,通过EventModule处理;
        /// 6.需要触发的事件:1)Delete的OnBefore、OnAfter;2)审核状态变更
        /// </remarks>
        /// <param name="answer">回答实体</param>
        public void DeleteAnswer(AskAnswer answer)
        {
            EventBus <AskAnswer> .Instance().OnBefore(answer, new CommonEventArgs(EventOperationType.Instance().Delete()));

            askAnswerRepository.Delete(answer);

            //更新相关问题的内容
            AskQuestion question = askQuestionRepository.Get(answer.QuestionId);

            question.AnswerCount--;
            if (answer.IsBest)
            {
                question.Status = QuestionStatus.Unresolved;
            }
            //调用Service中的Update方法,以触发相应的事件,但是不更新审核状态
            this.UpdateQuestion(question, false);

            //用户计数
            OwnerDataService ownerDataService = new OwnerDataService(TenantTypeIds.Instance().User());

            ownerDataService.Change(answer.UserId, OwnerDataKeys.Instance().AnswerCount(), -1);

            EventBus <AskAnswer> .Instance().OnAfter(answer, new CommonEventArgs(EventOperationType.Instance().Delete()));

            EventBus <AskAnswer, AuditEventArgs> .Instance().OnAfter(answer, new AuditEventArgs(answer.AuditStatus, null));
        }
示例#4
0
        /// <summary>
        /// 编辑回答
        /// </summary>
        /// <remarks>
        /// 1.使用AuditService.ChangeAuditStatusForUpdate设置审核状态;
        /// 2.需要触发的事件:Update的OnBefore、OnAfter;
        /// </remarks>
        /// <param name="answer">回答实体</param>
        /// <param name="changeAuditStatusForUpdate">是否更新审核状态</param>
        public void UpdateAnswer(AskAnswer answer, bool changeAuditStatusForUpdate = true)
        {
            EventBus <AskAnswer> .Instance().OnBefore(answer, new CommonEventArgs(EventOperationType.Instance().Update()));

            AuditStatus prevAuditStatus = answer.AuditStatus;

            //设置审核状态
            if (changeAuditStatusForUpdate)
            {
                new AuditService().ChangeAuditStatusForUpdate(answer.UserId, answer);
            }

            askAnswerRepository.Update(answer);

            //更新相关问题的内容
            AskQuestion question = askQuestionRepository.Get(answer.QuestionId);

            question.LastAnswerUserId = answer.UserId;
            question.LastAnswerAuthor = answer.Author;
            question.LastAnswerDate   = answer.DateCreated;
            if (answer.IsBest)
            {
                question.Status = QuestionStatus.Resolved;
            }
            //将临时附件转换为正式附件
            AttachmentService attachmentService = new AttachmentService(TenantTypeIds.Instance().AskAnswer());

            attachmentService.ToggleTemporaryAttachments(answer.UserId, TenantTypeIds.Instance().AskAnswer(), answer.AnswerId);
            this.UpdateQuestion(question, false);
            //调用Service中的Update方法,以触发相应的事件,但是不更新审核状态
            EventBus <AskAnswer> .Instance().OnAfter(answer, new CommonEventArgs(EventOperationType.Instance().Update()));

            EventBus <AskAnswer, AuditEventArgs> .Instance().OnAfter(answer, new AuditEventArgs(prevAuditStatus, answer.AuditStatus));
        }
示例#5
0
文件: AskAnswer.cs 项目: x1987624/SNS
 /// <summary>
 /// 将数据库中的信息转换成EditModel实体
 /// </summary>
 /// <param name="askAnswer">回答实体</param>
 /// <returns>编辑回答的EditModel</returns>
 public static AskAnswerEditModel AsEditModel(this AskAnswer askAnswer)
 {
     return(new AskAnswerEditModel
     {
         AnswerId = askAnswer.AnswerId,
         QuestionId = askAnswer.QuestionId,
         Body = askAnswer.Body
     });
 }
示例#6
0
文件: AskAnswer.cs 项目: x1987624/SNS
        /// <summary>
        /// 新建实体时使用
        /// </summary>
        public static AskAnswer New()
        {
            AskAnswer answer = new AskAnswer()
            {
                IP           = WebUtility.GetIP(),
                DateCreated  = DateTime.UtcNow,
                LastModified = DateTime.UtcNow
            };

            return(answer);
        }
示例#7
0
        /// <summary>
        /// 发布回答
        /// </summary>
        /// <remarks>
        /// 1.每个回答者针对每个问题只能回答一次,创建回答前需做重复检查
        /// 2.注意维护相关问题的回答数,AnswerCount++
        /// 3.注意需要在EventModule中处理动态、积分、通知、自动关注(回答者自动关注该问题)
        /// 4.注意使用AuditService.ChangeAuditStatusForCreate设置审核状态;
        /// 5.注意调用AttachmentService转换临时附件;
        /// 6.需要触发的事件:1)Create的OnBefore、OnAfter;2)审核状态变更;
        /// </remarks>
        /// <param name="answer">回答实体</param>
        /// <returns>是否创建成功</returns>
        public bool CreateAnswer(AskAnswer answer)
        {
            //先查询当前用户是否已经回答了指定问题
            if (GetUserAnswerByQuestionId(answer.UserId, answer.QuestionId) == null)
            {
                EventBus <AskAnswer> .Instance().OnBefore(answer, new CommonEventArgs(EventOperationType.Instance().Create()));

                //设置审核状态
                new AuditService().ChangeAuditStatusForCreate(answer.UserId, answer);

                askAnswerRepository.Insert(answer);

                if (answer.AnswerId > 0)
                {
                    //维护相关问题的内容
                    AskQuestion question = askQuestionRepository.Get(answer.QuestionId);
                    question.LastAnswerUserId = answer.UserId;
                    question.LastAnswerAuthor = answer.Author;
                    question.LastAnswerDate   = answer.DateCreated;
                    question.AnswerCount++;

                    //调用Service中的Update方法,以触发相应的事件,但是不更新审核状态
                    this.UpdateQuestion(question, false);

                    //将临时附件转换为正式附件
                    AttachmentService attachmentService = new AttachmentService(TenantTypeIds.Instance().AskAnswer());
                    attachmentService.ToggleTemporaryAttachments(answer.UserId, TenantTypeIds.Instance().AskAnswer(), answer.AnswerId);

                    //用户回答数计数
                    OwnerDataService ownerDataService = new OwnerDataService(TenantTypeIds.Instance().User());
                    ownerDataService.Change(answer.UserId, OwnerDataKeys.Instance().AnswerCount(), 1);

                    EventBus <AskAnswer> .Instance().OnAfter(answer, new CommonEventArgs(EventOperationType.Instance().Create()));

                    EventBus <AskAnswer, AuditEventArgs> .Instance().OnAfter(answer, new AuditEventArgs(null, answer.AuditStatus));
                }
            }

            return(answer.AnswerId > 0);
        }
示例#8
0
        /// <summary>
        /// 管理员审核回答
        /// </summary>
        /// <remarks>
        /// 1.审核状态未变化不用进行任何操作;
        /// 2.审核状态的变化影响动态的生成与删除、积分的变化
        /// 3.需要触发的事件:1) 批准或不批准;2) 审核状态变更;
        /// </remarks>
        /// <param name="answerId">问题id</param>
        /// <param name="isApproved">是否通过审核</param>
        public void ApproveAnswer(long answerId, bool isApproved)
        {
            AskAnswer   answer          = askAnswerRepository.Get(answerId);
            AuditStatus prevAuditStatus = answer.AuditStatus;
            AuditStatus auditStatus     = AuditStatus.Fail;

            if (isApproved)
            {
                auditStatus = AuditStatus.Success;
            }
            if (answer.AuditStatus != auditStatus)
            {
                answer.AuditStatus = auditStatus;

                this.UpdateAnswer(answer, false);

                string eventOperationType = isApproved ? EventOperationType.Instance().Approved() : EventOperationType.Instance().Disapproved();
                EventBus <AskAnswer> .Instance().OnAfter(answer, new CommonEventArgs(eventOperationType));

                EventBus <AskAnswer, AuditEventArgs> .Instance().OnAfter(answer, new AuditEventArgs(prevAuditStatus, answer.AuditStatus));
            }
        }
示例#9
0
 /// <summary>
 /// 编辑回答
 /// </summary>
 /// <remarks>
 /// 1.如果问题已解决(答案被采纳后),不允许回答者编辑,其余情况允许回答者编辑
 /// 2.管理员可以编辑所有的回答
 /// </remarks>
 public static bool Answer_Edit(this Authorizer authorizer, AskQuestion question, AskAnswer answer)
 {
     if (question.Status == QuestionStatus.Unresolved)
     {
         if (UserContext.CurrentUser != null && UserContext.CurrentUser.UserId == answer.UserId)
         {
             return(true);
         }
     }
     if (authorizer.IsAdministrator(AskConfig.Instance().ApplicationId))
     {
         return(true);
     }
     return(false);
 }