aboutsummaryrefslogtreecommitdiffstats
path: root/app/Http/Controllers/StudentController.php
blob: 178380b7f1a7290e41201b60324b76ba7f281de3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

namespace App\Http\Controllers;

use App\Models\Student;
use App\Models\Group;
use App\Models\SubGroup;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;

class StudentController extends Controller
{
    /**
     * Display a listing of students.
     */
    public function index(Request $request): View|JsonResponse
    {
        $query = Student::with(['group', 'subGroup']);

        if ($request->has('group_id') && $request->group_id) {
            $query->where('group_id', $request->group_id);
        }

        if ($request->has('sub_group_id') && $request->sub_group_id) {
            $query->where('sub_group_id', $request->sub_group_id);
        }

        $students = $query->get();

        if ($request->expectsJson()) {
            return response()->json($students);
        }

        $groups = Group::pluck('name', 'id');
        return view('students.index', compact('students', 'groups'));
    }

    /**
     * Show the form for creating a new student.
     */
    public function create(): View
    {
        $groups = Group::pluck('name', 'id');
        $subGroups = SubGroup::all()->mapWithKeys(function ($subGroup) {
            return [$subGroup->id => $subGroup->name . ' (' . ($subGroup->group->name ?? 'No group') . ')'];
        });

        return view('students.create', compact('groups', 'subGroups'));
    }

    /**
     * Store a newly created student.
     */
    public function store(Request $request): JsonResponse|RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'age' => 'required|integer|min:1|max:120',
            'group_id' => 'required|exists:groups,id',
            'sub_group_id' => 'nullable|exists:sub_groups,id',
            'points' => 'nullable|integer|min:0',
            'enrollment_date' => 'nullable|date',
        ]);

        $student = Student::create($validated);

        if ($request->expectsJson()) {
            return response()->json($student->load(['group', 'subGroup']), 201);
        }

        return redirect()->route('students.index')->with('success', 'Student created successfully.');
    }

    /**
     * Display the specified student.
     */
    public function show(Request $request, Student $student): View|JsonResponse
    {
        $student->load(['group', 'subGroup', 'lessons', 'activities']);

        if ($request->expectsJson()) {
            return response()->json($student);
        }

        return view('students.show', compact('student'));
    }

    /**
     * Show the form for editing the specified student.
     */
    public function edit(Student $student): View
    {
        $groups = Group::pluck('name', 'id');
        $subGroups = SubGroup::all()->mapWithKeys(function ($subGroup) {
            return [$subGroup->id => $subGroup->name . ' (' . ($subGroup->group->name ?? 'No group') . ')'];
        });

        return view('students.edit', compact('student', 'groups', 'subGroups'));
    }

    /**
     * Update the specified student.
     */
    public function update(Request $request, Student $student): JsonResponse|RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'sometimes|required|string|max:255',
            'last_name' => 'sometimes|required|string|max:255',
            'age' => 'sometimes|required|integer|min:1|max:120',
            'group_id' => 'sometimes|required|exists:groups,id',
            'sub_group_id' => 'nullable|exists:sub_groups,id',
            'points' => 'nullable|integer|min:0',
            'enrollment_date' => 'nullable|date',
        ]);

        $student->update($validated);

        if ($request->expectsJson()) {
            return response()->json($student->load(['group', 'subGroup']));
        }

        return redirect()->route('students.index')->with('success', 'Student updated successfully.');
    }

    /**
     * Remove the specified student.
     */
    public function destroy(Request $request, Student $student): JsonResponse|RedirectResponse
    {
        $student->delete();

        if ($request->expectsJson()) {
            return response()->json(null, 204);
        }

        return redirect()->route('students.index')->with('success', 'Student deleted successfully.');
    }

    /**
     * Add points to a student.
     */
    public function addPoints(Request $request, Student $student): JsonResponse|RedirectResponse
    {
        $validated = $request->validate([
            'points' => 'required|integer',
        ]);

        $student->increment('points', $validated['points']);

        if ($request->expectsJson()) {
            return response()->json($student);
        }

        return redirect()->back()->with('success', 'Points added successfully.');
    }
}