Skip to content

Commit ed204f7

Browse files
committed
Update diemSo column type to decimal(5,2) and enforce score limits
- Changed column type of 'diemSo' in 'tb_KetQua' table to decimal(5,2) to accommodate scores up to 10.00 with two decimal places. - Updated the 'store' method in 'KetQuaController' to validate that 'diemSo' does not exceed 10.
1 parent 06e920d commit ed204f7

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

app/Http/Controllers/DeThiController.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,9 @@ public function destroy($id)
145145
// API JSON: Lấy danh sách các đề thi theo ID ca thi
146146
public function apiIndex(Request $request)
147147
{
148-
$caThiId = $request->get('cathi_id'); // Lấy ID ca thi từ request
149148

150149
// Lấy tất cả các đề thi với thông tin môn học và ca thi liên quan
151-
$query = DeThi::with('monHoc', 'caThi')
152-
->where('cathi_id', $caThiId); // Lọc theo ID ca thi
150+
$query = DeThi::with('monHoc', 'caThi');
153151

154152
$deThis = $query->get(); // Thực hiện truy vấn và lấy dữ liệu
155153
// Trả về dữ liệu dưới dạng JSON với mã trạng thái 200

app/Http/Controllers/KetQuaController.php

+24-17
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,51 @@
2525

2626
use Illuminate\Http\Request;
2727
use App\Models\KetQua;
28-
use App\Models\SinhVien;
2928
use App\Models\DeThi;
29+
use Illuminate\Support\Facades\Auth;
3030

3131
class KetQuaController extends Controller
3232
{
3333
// Xem kết quả của sinh viên cho một đề thi qua API
3434
public function show($sinhvienId, $dethiId)
3535
{
36-
// Tìm kết quả của sinh viên với ID và đề thi được chỉ định
3736
$ketQua = KetQua::where('sinhvien_id', $sinhvienId)
3837
->where('dethi_id', $dethiId)
3938
->first();
4039

41-
// Nếu không tìm thấy kết quả, trả về thông báo lỗi 404
4240
if (!$ketQua) {
4341
return response()->json(['message' => 'Không tìm thấy kết quả.'], 404);
4442
}
4543

46-
// Nếu tìm thấy kết quả, trả về dữ liệu kết quả dưới dạng JSON với mã trạng thái 200
4744
return response()->json($ketQua, 200);
4845
}
4946

50-
// Views
51-
52-
// Hiển thị kết quả của sinh viên cho một đề thi qua giao diện
53-
public function view($sinhvienId, $dethiId)
47+
// Thêm kết quả qua API
48+
public function store(Request $request)
5449
{
55-
// Tìm kết quả của sinh viên với ID và đề thi được chỉ định
56-
$ketQua = KetQua::where('sinhvien_id', $sinhvienId)
57-
->where('dethi_id', $dethiId)
58-
->first();
50+
// Lấy sinh viên từ token đăng nhập (Sanctum)
51+
$sinhVien = Auth::guard('sanctum')->user();
5952

60-
// Nếu không tìm thấy kết quả, trả về view thông báo không tìm thấy kết quả
61-
if (!$ketQua) {
62-
return view('ketqua.notfound');
53+
if (!$sinhVien) {
54+
return response()->json(['message' => 'Không tìm thấy sinh viên đã đăng nhập.'], 401);
6355
}
6456

65-
// Nếu tìm thấy kết quả, trả về view hiển thị kết quả với dữ liệu kết quả
66-
return view('ketqua.show', compact('ketQua'));
57+
// Xác thực dữ liệu đầu vào từ request
58+
$request->validate([
59+
'diemSo' => 'required|numeric|min:0|max:10', // Kiểm tra điểm số là số và nằm trong khoảng 0-10
60+
'danhSachDapAn' => 'required|json', // Đáp án phải là JSON hợp lệ
61+
'dethi_id' => 'required|exists:tb_dethi,id', // Đề thi phải tồn tại
62+
]);
63+
64+
// Tạo mới kết quả và lưu vào cơ sở dữ liệu
65+
$ketQua = KetQua::create([
66+
'diemSo' => $request->diemSo,
67+
'danhSachDapAn' => $request->danhSachDapAn,
68+
'dethi_id' => $request->dethi_id,
69+
'sinhvien_id' => $sinhVien->id, // Chèn sinhvien_id từ sinh viên đã đăng nhập
70+
]);
71+
72+
// Trả về kết quả vừa được thêm với mã trạng thái 201
73+
return response()->json(['message' => 'Kết quả đã được lưu thành công.', 'ketQua' => $ketQua], 201);
6774
}
6875
}

database/migrations/2024_08_12_031153_create_tb_ket_qua_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function up(): void
1313
{
1414
Schema::create('tb_KetQua', function (Blueprint $table) {
1515
$table->id(); // Tự động tăng id
16-
$table->decimal('diemSo', 3, 2)->nullable(); // Điểm số, kiểu decimal với 2 chữ số thập phân
16+
$table->decimal('diemSo', 5, 2)->nullable(); // Điểm số, kiểu decimal với 2 chữ số thập phân
1717
$table->json('danhSachDapAn')->nullable(); // Danh sách đáp án
1818
$table->unsignedBigInteger('dethi_id'); // Khóa ngoại tham chiếu đến bảng 'tb_dethi'
1919
$table->unsignedBigInteger('sinhvien_id'); // Khóa ngoại tham chiếu đến bảng 'tb_sinhvien'

routes/api.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
// Route::delete('/{id}', [SinhVienController::class, 'apiDestroy']); // xoá sinh viên
3737
Route::get('/search', [SinhVienController::class, 'apiSearch']); // tìm kiếm sinh viên
3838

39-
// kết quả bài thi
40-
Route::get('/{sinhvienId}/ketqua/{dethiId}', [KetQuaController::class, 'show'])->name('api.ketqua.show');
39+
Route::get('/{sinhvienId}/ketqua/{dethiId}', [KetQuaController::class, 'show'])->name('api.ketqua.show'); // kết quả bài thi
4140
});
4241

42+
Route::post('/ketqua', [KetQuaController::class, 'store']); // API thêm kết quả (sinhvien_id sẽ được lấy từ phiên đăng nhập)
43+
4344
// khoa
4445
Route::prefix('khoa')->group(function () {
4546
Route::get('/', [KhoaController::class, 'apiIndex']); // danh sách khoa
@@ -67,7 +68,7 @@
6768
// Route::post('/', [LopController::class, 'apiStore']); // thêm lớp
6869
// Route::put('/{id}', [LopController::class, 'apiUpdate']); // sửa lớp
6970
// Route::delete('/{id}', [LopController::class, 'apiDestroy']); // xoá lớp
70-
Route::get('/timkiem', [LopController::class, 'apiSearch']); // tìm kiếm lớp
71+
Route::get('?search', [LopController::class, 'apiSearch']); // tìm kiếm lớp
7172
});
7273

7374
// Môn học

0 commit comments

Comments
 (0)