ProgressController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Http\Controllers\V3;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Resources\V3\ProgressResource;
  5. use App\Models\ProgressChapter;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Validation\ValidationException;
  8. class ProgressController extends Controller
  9. {
  10. /**
  11. * 列出章节翻译进度
  12. *
  13. * 按 channel 查询各章节的翻译完成度。view 目前只支持 channel 一种口径,
  14. * 传其它值返回 422。
  15. *
  16. * @unauthenticated
  17. *
  18. * @queryParam view string required 查询口径,目前只有 channel。Enum: channel
  19. * @queryParam channels string required channel uid 列表,**下划线分隔**(不是逗号)
  20. * @queryParam level integer 只返回该层级及以上的章节,需联查 pali_texts
  21. * @queryParam lang string 按译文语言过滤。Example: zh-Hans
  22. * @queryParam book integer 按典籍 id 过滤
  23. * @queryParam order string 排序字段(progress_chapters 的列)。Default: completed_at
  24. * @queryParam dir string 排序方向。Enum: asc,desc Default: desc
  25. * @queryParam page integer 页码。Default: 1
  26. * @queryParam per_page integer 每页数量。Default: 10
  27. */
  28. public function index(Request $request)
  29. {
  30. //
  31. $select = [
  32. 'progress_chapters.book',
  33. 'progress_chapters.para',
  34. 'progress_chapters.lang',
  35. 'progress_chapters.progress',
  36. 'progress_chapters.channel_id',
  37. 'progress_chapters.title',
  38. 'progress_chapters.last_chapter_completed_at',
  39. 'progress_chapters.completed_at',
  40. 'progress_chapters.updated_at',
  41. ];
  42. switch ($request->input('view')) {
  43. case 'channel':
  44. $table = ProgressChapter::select($select)
  45. ->whereIn('progress_chapters.channel_id', explode('_', $request->input('channels', '')));
  46. break;
  47. default:
  48. throw ValidationException::withMessages(['view' => __('site.invalid_parameter')]);
  49. break;
  50. }
  51. if ($request->filled('level')) {
  52. $table = $table->join('pali_texts', function ($join) {
  53. $join->on('progress_chapters.book', '=', 'pali_texts.book')
  54. ->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  55. })->where('pali_texts.level', '<=', (int) $request->input('level'));
  56. }
  57. if ($request->has('lang')) {
  58. $table = $table->where('progress_chapters.lang', $request->input('lang'));
  59. }
  60. if ($request->has('book')) {
  61. $table = $table->where('progress_chapters.book', $request->input('book'));
  62. }
  63. $table = $table->orderBy(
  64. 'progress_chapters.'.$request->input('order', 'completed_at'),
  65. $request->input('dir', 'desc')
  66. );
  67. // 分页与 meta 全由框架算;page 参数 paginate() 自己会读
  68. return ProgressResource::collection(
  69. $table->paginate($request->integer('per_page', 10))
  70. );
  71. }
  72. /**
  73. * Store a newly created resource in storage.
  74. */
  75. public function store(Request $request)
  76. {
  77. //
  78. }
  79. /**
  80. * Display the specified resource.
  81. */
  82. public function show(string $id)
  83. {
  84. //
  85. }
  86. /**
  87. * Update the specified resource in storage.
  88. */
  89. public function update(Request $request, string $id)
  90. {
  91. //
  92. }
  93. /**
  94. * Remove the specified resource from storage.
  95. */
  96. public function destroy(string $id)
  97. {
  98. //
  99. }
  100. }