1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-19 22:52:59 +08:00

优化代码

This commit is contained in:
xiaochong0302 2020-11-19 21:26:01 +08:00
parent 10c43d0ab3
commit 70886569d1
11 changed files with 126 additions and 5 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Api\Controllers;
use App\Services\Logic\Chapter\ChapterInfo as ChapterInfoService;
use App\Services\Logic\Chapter\ChapterLike as ChapterLikeService;
use App\Services\Logic\Chapter\ConsultList as ChapterConsultListService;
use App\Services\Logic\Chapter\Learning as ChapterLearningService;
use App\Services\Logic\Chapter\ResourceList as ChapterResourceListService;
@ -13,6 +14,18 @@ use App\Services\Logic\Chapter\ResourceList as ChapterResourceListService;
class ChapterController extends Controller
{
/**
* @Get("/{id:[0-9]+}/consults", name="api.chapter.consults")
*/
public function consultsAction($id)
{
$service = new ChapterConsultListService();
$pager = $service->handle($id);
return $this->jsonSuccess(['pager' => $pager]);
}
/**
* @Get("/{id:[0-9]+}/resources", name="api.chapter.resourses")
*/

View File

@ -45,7 +45,7 @@ class SearchController extends Controller
{
switch ($type) {
case 'group':
$service = new GroupSearchService;
$service = new GroupSearchService();
break;
case 'user':
$service = new UserSearchService();

View File

@ -47,7 +47,7 @@ class SearchController extends Controller
{
switch ($type) {
case 'group':
$service = new GroupSearchService;
$service = new GroupSearchService();
break;
case 'user':
$service = new UserSearchService();

View File

@ -17,7 +17,6 @@
{%- macro live_lesson_info(lesson) %}
{% set url = lesson.me.owned ? url({'for':'home.chapter.show','id':lesson.id}) : '' %}
{% set priv = lesson.me.owned ? 'allow' : 'deny' %}
{% set over_flag = lesson.attrs.end_time < time() ? '已结束' : '' %}
<a class="{{ priv }} view-lesson" href="javascript:" data-url="{{ url }}">
<i class="layui-icon layui-icon-video"></i>
<span class="title">{{ lesson.title }}</span>

View File

@ -28,10 +28,18 @@ class Consult extends Repository
$builder->andWhere('course_id = :course_id:', ['course_id' => $where['course_id']]);
}
if (!empty($where['chapter_id'])) {
$builder->andWhere('chapter_id = :chapter_id:', ['chapter_id' => $where['chapter_id']]);
}
if (!empty($where['owner_id'])) {
$builder->andWhere('owner_id = :owner_id:', ['owner_id' => $where['owner_id']]);
}
if (!empty($where['replied'])) {
$builder->andWhere('reply_time > 0');
}
if (isset($where['private'])) {
$builder->andWhere('private = :private:', ['private' => $where['private']]);
}

View File

@ -72,6 +72,7 @@ class BasicInfo extends Service
'model' => $chapter->model,
'play_urls' => $playUrls,
'resource_count' => $chapter->resource_count,
'consult_count' => $chapter->consult_count,
'user_count' => $chapter->user_count,
'like_count' => $chapter->like_count,
];
@ -99,6 +100,7 @@ class BasicInfo extends Service
'end_time' => $live->end_time,
'status' => $live->status,
'resource_count' => $chapter->resource_count,
'consult_count' => $chapter->consult_count,
'user_count' => $chapter->user_count,
'like_count' => $chapter->like_count,
];
@ -117,6 +119,7 @@ class BasicInfo extends Service
'model' => $chapter->model,
'content' => $read->content,
'resource_count' => $chapter->resource_count,
'consult_count' => $chapter->consult_count,
'user_count' => $chapter->user_count,
'like_count' => $chapter->like_count,
];

View File

@ -0,0 +1,36 @@
<?php
namespace App\Services\Logic\Chapter;
use App\Library\Paginator\Query as PagerQuery;
use App\Services\Logic\ChapterTrait;
use App\Services\Logic\Consult\ConsultList as ConsultListHandler;
use App\Services\Logic\Service;
class ConsultList extends Service
{
use ChapterTrait;
public function handle($id)
{
$chapter = $this->checkChapter($id);
$pagerQuery = new PagerQuery();
$sort = $pagerQuery->getSort();
$page = $pagerQuery->getPage();
$limit = $pagerQuery->getLimit();
$params = [
'chapter_id' => $chapter->id,
'private' => 0,
'published' => 1,
];
$service = new ConsultListHandler();
return $service->paginate($params, $sort, $page, $limit);
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace App\Services\Logic\Consult;
use App\Builders\ConsultList as ConsultListBuilder;
use App\Repos\Consult as ConsultRepo;
use App\Services\Logic\Service;
class ConsultList extends Service
{
public function paginate($params, $sort, $page, $limit)
{
$consultRepo = new ConsultRepo();
$pager = $consultRepo->paginate($params, $sort, $page, $limit);
return $this->handleConsults($pager);
}
protected function handleConsults($pager)
{
if ($pager->total_items == 0) {
return $pager;
}
$consults = $pager->items->toArray();
$builder = new ConsultListBuilder();
$users = $builder->getUsers($consults);
$items = [];
foreach ($consults as $consult) {
$owner = $users[$consult['owner_id']] ?? new \stdClass();
$items[] = [
'id' => $consult['id'],
'question' => $consult['question'],
'answer' => $consult['answer'],
'like_count' => $consult['like_count'],
'reply_time' => $consult['reply_time'],
'create_time' => $consult['create_time'],
'update_time' => $consult['update_time'],
'owner' => $owner,
];
}
$pager->items = $items;
return $pager;
}
}

View File

@ -67,6 +67,7 @@ class Group extends Handler
'avatar' => $item['avatar'],
'about' => $item['about'],
'user_count' => (int)$item['user_count'],
'msg_count' => (int)$item['msg_count'],
'owner' => json_decode($item['owner'], true),
];
}

View File

@ -3,7 +3,7 @@
namespace App\Services\Logic\Teacher;
use App\Library\Paginator\Query as PagerQuery;
use App\Models\User;
use App\Models\User as UserModel;
use App\Repos\User as UserRepo;
use App\Services\Logic\Service;
@ -16,7 +16,7 @@ class TeacherList extends Service
$params = $pagerQuery->getParams();
$params['edu_role'] = User::EDU_ROLE_TEACHER;
$params['edu_role'] = UserModel::EDU_ROLE_TEACHER;
$params['deleted'] = 0;
$sort = $pagerQuery->getSort();
@ -52,6 +52,8 @@ class TeacherList extends Service
'avatar' => $user['avatar'],
'title' => $user['title'],
'about' => $user['about'],
'gender' => $user['gender'],
'area' => $user['area'],
];
}

View File

@ -26,3 +26,6 @@ type = string
[user_count]
type = string
[msg_count]
type = string