1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-21 03:17:05 +08:00
2021-05-10 21:35:07 +08:00

166 lines
3.9 KiB
PHP

<?php
namespace App\Http\Home\Controllers;
use App\Http\Home\Services\Answer as AnswerService;
use App\Http\Home\Services\Question as QuestionService;
use App\Services\Logic\Answer\AnswerAccept as AnswerAcceptService;
use App\Services\Logic\Answer\AnswerCreate as AnswerCreateService;
use App\Services\Logic\Answer\AnswerDelete as AnswerDeleteService;
use App\Services\Logic\Answer\AnswerInfo as AnswerInfoService;
use App\Services\Logic\Answer\AnswerLike as AnswerLikeService;
use App\Services\Logic\Answer\AnswerUpdate as AnswerUpdateService;
/**
* @RoutePrefix("/answer")
*/
class AnswerController extends Controller
{
/**
* @Get("/add", name="home.answer.add")
*/
public function addAction()
{
$id = $this->request->getQuery('question_id', 'int', 0);
$service = new QuestionService();
$question = $service->getQuestion($id);
$this->view->setVar('question', $question);
}
/**
* @Get("/{id:[0-9]+}/edit", name="home.answer.edit")
*/
public function editAction($id)
{
$service = new AnswerService();
$answer = $service->getAnswer($id);
$service = new QuestionService();
$question = $service->getQuestion($answer->question_id);
$this->view->setVar('question', $question);
$this->view->setVar('answer', $answer);
}
/**
* @Get("/{id:[0-9]+}", name="home.answer.show")
*/
public function showAction($id)
{
$service = new AnswerInfoService();
$answer = $service->handle($id);
$this->view->setVar('answer', $answer);
}
/**
* @Post("/create", name="home.answer.create")
*/
public function createAction()
{
$service = new AnswerCreateService();
$answer = $service->handle();
$location = $this->url->get([
'for' => 'home.question.show',
'id' => $answer->question_id,
]);
$content = [
'location' => $location,
'msg' => '创建回答成功',
];
return $this->jsonSuccess($content);
}
/**
* @Post("/{id:[0-9]+}/update", name="home.answer.update")
*/
public function updateAction($id)
{
$service = new AnswerUpdateService();
$answer = $service->handle($id);
$location = $this->url->get([
'for' => 'home.question.show',
'id' => $answer->question_id,
]);
$content = [
'location' => $location,
'msg' => '更新回答成功',
];
return $this->jsonSuccess($content);
}
/**
* @Post("/{id:[0-9]+}/delete", name="home.answer.delete")
*/
public function deleteAction($id)
{
$service = new AnswerDeleteService();
$answer = $service->handle($id);
$location = $this->url->get([
'for' => 'home.question.show',
'id' => $answer->question_id,
]);
$content = [
'location' => $location,
'msg' => '删除回答成功',
];
return $this->jsonSuccess($content);
}
/**
* @Post("/{id:[0-9]+}/like", name="home.answer.like")
*/
public function likeAction($id)
{
$service = new AnswerLikeService();
$data = $service->handle($id);
$msg = $data['action'] == 'do' ? '点赞成功' : '取消点赞成功';
return $this->jsonSuccess(['data' => $data, 'msg' => $msg]);
}
/**
* @Post("/{id:[0-9]+}/accept", name="home.answer.accept")
*/
public function acceptAction($id)
{
$service = new AnswerAcceptService();
$data = $service->handle($id);
$msg = $data['action'] == 'do' ? '采纳成功' : '取消采纳成功';
return $this->jsonSuccess(['data' => $data, 'msg' => $msg]);
}
/**
* @Route("/{id:[0-9]+}/report", name="home.answer.report")
*/
public function reportAction($id)
{
}
}