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:
commit
5f1fa2c1c1
|
|
@ -221,16 +221,29 @@ class TechnicalController extends Controller
|
||||||
'technicals.*.point' => 'required|integer|min:0|max:3', // Điểm trong khoảng [0-3]
|
'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
|
// Lấy danh sách technicals của user từ bảng TechnicalUser
|
||||||
TechnicalUser::where('user_id', $userInfo->id)->delete();
|
$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) {
|
foreach ($validatedData['technicals'] as $technical) {
|
||||||
TechnicalUser::create([
|
$existingTechnical = $existingTechnicals->get($technical['technical_id']);
|
||||||
'user_id' => $userInfo->id,
|
// if ($technical['technical_id'] == 7) {
|
||||||
'technical_id' => $technical['technical_id'],
|
// dd($existingTechnical,$userInfo);
|
||||||
'point' => $technical['point']
|
// }
|
||||||
]);
|
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.');
|
return AbstractController::ResultSuccess('Technicals for user updated successfully.');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue