ProgressController.php 3.5 KB

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