request->getPost(); $question = $this->checkQuestion($post['question_id']); $user = $this->getLoginUser(); $answer = new AnswerModel(); $validator = new AnswerValidator(); $validator->checkIfAllowAnswer($question, $user); /** * @todo 引入自动审核机制 */ $answer->published = AnswerModel::PUBLISH_APPROVED; $answer->content = $validator->checkContent($post['content']); $answer->client_type = $this->getClientType(); $answer->client_ip = $this->getClientIp(); $answer->question_id = $question->id; $answer->owner_id = $user->id; $answer->create(); $question->last_answer_id = $answer->id; $question->last_replier_id = $answer->owner_id; $question->last_reply_time = $answer->create_time; $question->update(); $this->incrUserAnswerCount($user); $this->incrQuestionAnswerCount($question); $this->handleAnswerPoint($answer); $this->handleAnswerNotice($answer); $this->eventsManager->fire('Answer:afterCreate', $this, $answer); return $answer; } protected function incrQuestionAnswerCount(QuestionModel $question) { $question->answer_count += 1; $question->update(); } protected function incrUserAnswerCount(UserModel $user) { $user->answer_count += 1; $user->update(); } protected function handleAnswerNotice(AnswerModel $answer) { $notice = new QuestionAnsweredNotice(); $notice->handle($answer); } protected function handleAnswerPoint(AnswerModel $answer) { $service = new AnswerPostPointHistory(); $service->handle($answer); } }