ProgressController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\ProgressChapter;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Response;
  6. class ProgressController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return Response
  12. */
  13. public function index(Request $request)
  14. {
  15. //
  16. $select = [
  17. 'progress_chapters.book',
  18. 'progress_chapters.para',
  19. 'progress_chapters.lang',
  20. 'progress_chapters.progress',
  21. 'progress_chapters.channel_id',
  22. 'progress_chapters.title',
  23. 'progress_chapters.last_chapter_completed_at',
  24. 'progress_chapters.completed_at',
  25. 'progress_chapters.updated_at',
  26. ];
  27. switch ($request->input('view')) {
  28. case 'channel':
  29. $table = ProgressChapter::select($select)
  30. ->whereIn('progress_chapters.channel_id', explode('_', $request->input('channels', '')));
  31. break;
  32. default:
  33. return $this->error('invalid view', 400, 400);
  34. break;
  35. }
  36. if ($request->filled('level')) {
  37. $table = $table->join('pali_texts', function ($join) {
  38. $join->on('progress_chapters.book', '=', 'pali_texts.book')
  39. ->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  40. })->where('pali_texts.level', '<=', (int) $request->input('level'));
  41. }
  42. if ($request->has('lang')) {
  43. $table = $table->where('progress_chapters.lang', $request->input('lang'));
  44. }
  45. if ($request->has('book')) {
  46. $table = $table->where('progress_chapters.book', $request->input('book'));
  47. }
  48. $count = $table->count();
  49. $table = $table->orderBy(
  50. 'progress_chapters.'.$request->input('order', 'completed_at'),
  51. $request->input('dir', 'desc')
  52. );
  53. $table = $table->skip($request->input('offset', 0))
  54. ->take($request->input('limit', 10));
  55. $result = $table->get();
  56. return $this->ok(
  57. [
  58. 'rows' => $result->toArray(),
  59. 'total' => $count,
  60. ]
  61. );
  62. }
  63. /**
  64. * Store a newly created resource in storage.
  65. */
  66. public function store(Request $request)
  67. {
  68. //
  69. }
  70. /**
  71. * Display the specified resource.
  72. */
  73. public function show(string $id)
  74. {
  75. //
  76. }
  77. /**
  78. * Update the specified resource in storage.
  79. */
  80. public function update(Request $request, string $id)
  81. {
  82. //
  83. }
  84. /**
  85. * Remove the specified resource from storage.
  86. */
  87. public function destroy(string $id)
  88. {
  89. //
  90. }
  91. }