Без опису

SectionController.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Professor;
  4. use App\Section;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class SectionController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. //
  18. }
  19. /**
  20. * Show the form for creating a new resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function create()
  25. {
  26. //
  27. }
  28. /**
  29. * Store a newly created resource in storage.
  30. *
  31. * @param \Illuminate\Http\Request $request
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function store(Request $request)
  35. {
  36. $data = $request->validate([
  37. 'course_id' => ['required', 'integer', 'exists:courses,id'],
  38. 'semester_code' => ['required', 'size:3', 'exists:semesters,code'],
  39. 'section_count' => ['required', 'integer', 'max:50'],
  40. ]);
  41. // TODO: Check if section code is taken
  42. for ($i=1; $i <= $request['section_count']; $i++) {
  43. Section::create([
  44. 'course_id' => $request->course_id,
  45. 'semester_code' => $request->semester_code,
  46. 'code' => str_pad($i, 3, '0', STR_PAD_LEFT)
  47. ]);
  48. }
  49. return redirect()->back();
  50. }
  51. /**
  52. * Display the specified resource.
  53. *
  54. * @param \App\Section $section
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function show(Section $section)
  58. {
  59. //
  60. }
  61. /**
  62. * Show the form for editing the specified resource.
  63. *
  64. * @param \App\Section $section
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function edit(Section $section)
  68. {
  69. //
  70. }
  71. /**
  72. * Update the specified resource in storage.
  73. *
  74. * @param \Illuminate\Http\Request $request
  75. * @param \App\Section $section
  76. * @return \Illuminate\Http\Response
  77. */
  78. public function update(Request $request, Section $section)
  79. {
  80. // dd($request);
  81. $data = array_filter($request->validate([
  82. 'code' => ['nullable', 'size:3', 'string'],
  83. 'professor_id' => ['nullable', 'array'],
  84. 'professor_id.*' => ['exists:professors,id'],
  85. 'credits' => ['nullable', 'numeric'],
  86. 'student_count' => ['nullable', 'integer'],
  87. ]));
  88. $section->update($data);
  89. // TODO: Insert syllabus
  90. if ($request->file('syllabus')) {
  91. $path = $request->file('syllabus')->store('section_syllabus');
  92. $section->update(['syllabus' => '/storage/'.$path]);
  93. }
  94. if (isset($data['professor_id'])) {
  95. $section->professors()->detach();
  96. foreach($data['professor_id'] as $professor_id) {
  97. $section->professors()->attach($professor_id);
  98. }
  99. }
  100. return redirect()->back();
  101. }
  102. /**
  103. * Remove the specified resource from storage.
  104. *
  105. * @param \App\Section $section
  106. * @return \Illuminate\Http\Response
  107. */
  108. public function destroy(Section $section)
  109. {
  110. //
  111. }
  112. }