From a318bfa8cd286252e1cff67efee8389ddb4ae859 Mon Sep 17 00:00:00 2001 From: hieunv Date: Thu, 24 Jul 2025 10:23:18 +0700 Subject: [PATCH] api destroy exercise --- .../Controllers/Api/ExerciseController.php | 30 ++++++++++--- app/Services/ExerciseService.php | 45 +++++++++++++++++++ app/Services/ExerciseSkillService.php | 3 ++ routes/api.php | 1 + 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/ExerciseController.php b/app/Http/Controllers/Api/ExerciseController.php index 1d4a14a..8fe9c69 100644 --- a/app/Http/Controllers/Api/ExerciseController.php +++ b/app/Http/Controllers/Api/ExerciseController.php @@ -10,6 +10,7 @@ use App\Services\QuestionService; use App\Services\SkillService; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Symfony\Component\HttpFoundation\Response; class ExerciseController extends Controller { @@ -35,14 +36,14 @@ class ExerciseController extends Controller 'status' => true, 'data' => $exercises, 'message' => 'Không có dữ liệu.', - ]); + ], Response::HTTP_NOT_FOUND); } return response()->json([ 'status' => true, 'data' => $exercises, 'message' => 'Lấy danh sách đề thi thành công.', - ]); + ], Response::HTTP_OK); } public function create(CreateExerciseRequest $request) @@ -65,14 +66,14 @@ class ExerciseController extends Controller 'success' => true, 'message' => 'Tạo đề thi thành công.', 'exercise_id' => $exerciseId - ], 201); + ], Response::HTTP_OK); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'success' => false, 'message' => 'Tạo đề thi thất bại.', 'error' => $e->getMessage() - ], 500); + ], Response::HTTP_INTERNAL_SERVER_ERROR); } } @@ -85,13 +86,30 @@ class ExerciseController extends Controller 'status' => true, 'data' => $exercise, 'message' => 'Không có dữ liệu.', - ]); + ], Response::HTTP_NOT_FOUND); } return response()->json([ 'status' => true, 'data' => $exercise, 'message' => 'Lấy danh sách đề thi thành công.', - ]); + ], Response::HTTP_OK); + } + + public function destroy($id) + { + $result = $this->exerciseService->destroy($id); + + if (!$result['status']) { + return response()->json([ + 'status' => false, + 'message' => $result['message'], + ], Response::HTTP_INTERNAL_SERVER_ERROR); + } + + return response()->json([ + 'status' => true, + 'message' => $result['message'], + ], Response::HTTP_OK); } } diff --git a/app/Services/ExerciseService.php b/app/Services/ExerciseService.php index 9c3f08c..20f5706 100644 --- a/app/Services/ExerciseService.php +++ b/app/Services/ExerciseService.php @@ -4,6 +4,13 @@ namespace App\Services; use App\Http\Resources\ExerciseResource; use App\Models\Exercise; +use App\Models\ExerciseSkill; +use App\Models\Question; +use App\Models\QuestionBlank; +use App\Models\QuestionChoice; +use App\Models\QuestionGroup; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Support\Facades\DB; class ExerciseService { @@ -71,4 +78,42 @@ class ExerciseService return new ExerciseResource($exercise); } + + public function destroy($id) + { + try { + $exercise = Exercise::findOrFail($id); + + DB::transaction(function () use ($exercise) { + + $groupIds = $exercise->questionGroups()->pluck('id'); + $questionIds = DB::table('se_questions') + ->whereIn('group_id', $groupIds) + ->pluck('id'); + + QuestionChoice::whereIn('question_id', $questionIds)->delete(); + QuestionBlank::whereIn('question_id', $questionIds)->delete(); + Question::whereIn('id', $questionIds)->delete(); + QuestionGroup::whereIn('id', $groupIds)->delete(); + ExerciseSkill::where('exercise_id', $exercise->id)->delete(); + + $exercise->delete(); + }); + + return [ + 'status' => true, + 'message' => 'Xoá đề thi thành công.' + ]; + } catch (ModelNotFoundException $e) { + return [ + 'status' => false, + 'message' => 'Không tìm thấy đề thi.' + ]; + } catch (\Exception $e) { + return [ + 'status' => false, + 'message' => 'Lỗi hệ thống: ' . $e->getMessage() + ]; + } + } } diff --git a/app/Services/ExerciseSkillService.php b/app/Services/ExerciseSkillService.php index 71895b9..ca8a08b 100644 --- a/app/Services/ExerciseSkillService.php +++ b/app/Services/ExerciseSkillService.php @@ -15,11 +15,14 @@ class ExerciseSkillService */ public function handleSkillsForExercise($skillIds, $exerciseId) { + $now = now(); $data = []; foreach ($skillIds as $skillId) { $data[] = [ 'exercise_id' => $exerciseId, 'skill_id' => $skillId, + 'created_at' => $now, + 'updated_at' => $now, ]; } diff --git a/routes/api.php b/routes/api.php index 495a596..8e87b84 100644 --- a/routes/api.php +++ b/routes/api.php @@ -16,4 +16,5 @@ Route::middleware('auth:api')->group(function () { Route::get('/exercises', [ExerciseController::class, 'index'])->name('exercise.index'); Route::post('/exercise/create', [ExerciseController::class, 'create'])->name('exercise.create'); Route::get('/exercise/{id}', [ExerciseController::class, 'detail'])->name('exercise.detail'); + Route::delete('/exercise/{id}', [ExerciseController::class, 'destroy']); });