Merge pull request 'Hiệu chỉnh api update technical' (#88) from Sprint-4/MS-37-FE-Evaluation into master

Reviewed-on: #88
This commit is contained in:
vincent.vo 2024-09-24 19:18:27 +10:00
commit 5f1fa2c1c1
1 changed files with 21 additions and 8 deletions

View File

@ -221,17 +221,30 @@ class TechnicalController extends Controller
'technicals.*.point' => 'required|integer|min:0|max:3', // Điểm trong khoảng [0-3]
]);
// Xóa hết các bản ghi hiện có của user trong bảng technical_users
TechnicalUser::where('user_id', $userInfo->id)->delete();
// Lấy danh sách technicals của user từ bảng TechnicalUser
$existingTechnicals = TechnicalUser::where('user_id', $userInfo->id)->get()->keyBy('technical_id');
// Duyệt qua mảng technicals và thêm mới dữ liệu
foreach ($validatedData['technicals'] as $technical) {
$existingTechnical = $existingTechnicals->get($technical['technical_id']);
// if ($technical['technical_id'] == 7) {
// dd($existingTechnical,$userInfo);
// }
if ($existingTechnical) {
// Nếu technical_id đã tồn tại và point khác, thì update
if ($existingTechnical->point !== $technical['point']) {
$existingTechnical->update([
'point' => $technical['point']
]);
}
} else {
// Nếu technical_id chưa tồn tại, tạo mới
TechnicalUser::create([
'user_id' => $userInfo->id,
'technical_id' => $technical['technical_id'],
'point' => $technical['point']
]);
}
}
return AbstractController::ResultSuccess('Technicals for user updated successfully.');
}