From c3a005e03effc678a906621db9a84d9a4769cee0 Mon Sep 17 00:00:00 2001 From: Farisa Damayanti Date: Fri, 11 Sep 2026 09:16:34 +0700 Subject: [PATCH 1/2] get all user & students sync --- app/Contracts/Interfaces/StudentInterface.php | 3 ++ app/Contracts/Interfaces/UserInterface.php | 1 + .../Repositories/StudentRepository.php | 40 +++++++++++++++++++ app/Contracts/Repositories/UserRepository.php | 12 ++++++ app/Http/Controllers/Api/UserController.php | 8 ++++ app/Http/Controllers/StudentController.php | 40 +++++++++++++++++++ app/Providers/GlobalViewLoaderProvider.php | 8 ++-- ..._080915_add_is_synced_to_student_table.php | 28 +++++++++++++ routes/api.php | 5 +++ 9 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2026_09_11_080915_add_is_synced_to_student_table.php diff --git a/app/Contracts/Interfaces/StudentInterface.php b/app/Contracts/Interfaces/StudentInterface.php index 56fb5b53..626e03ab 100644 --- a/app/Contracts/Interfaces/StudentInterface.php +++ b/app/Contracts/Interfaces/StudentInterface.php @@ -177,4 +177,7 @@ public function StudentFinish(): mixed; public function countAlumni(): mixed; public function getStudentBySession(int $session): mixed; + + public function getSynced(Request $request): mixed; + public function sync(mixed $id): mixed; } diff --git a/app/Contracts/Interfaces/UserInterface.php b/app/Contracts/Interfaces/UserInterface.php index cbf4a851..1c04e457 100644 --- a/app/Contracts/Interfaces/UserInterface.php +++ b/app/Contracts/Interfaces/UserInterface.php @@ -29,5 +29,6 @@ public function addCourseToSubcribedUser(int $course): void; public function addSubCourseToSubcribedUser(int $courseId, mixed $subCourseId): void; public function where(string $string, mixed $id): mixed; public function get(Request $request): mixed; + public function getUser(Request $request): mixed; } diff --git a/app/Contracts/Repositories/StudentRepository.php b/app/Contracts/Repositories/StudentRepository.php index 4e914442..f6b30b7c 100644 --- a/app/Contracts/Repositories/StudentRepository.php +++ b/app/Contracts/Repositories/StudentRepository.php @@ -732,4 +732,44 @@ public function getStudentByMentorDevision(Request $request): mixed } return collect(); } + + public function sync(mixed $id): mixed + { + $student = $this->model->query() + ->where('id', $id) + ->first(); + + if (!$student) { + return 'not_found'; + } + + if ($student->status !== StudentStatusEnum::ACCEPTED->value) { + return 'not_accepted'; + } + + if ($student->is_synced) { + return 'already_synced'; + } + + $student->update([ + 'is_synced' => true, + ]); + + return $student; + } + + public function getSynced(Request $request): mixed + { + return $this->model->query() + ->where('status', StudentStatusEnum::ACCEPTED->value) + ->where('is_synced', true) + ->when($request->name, function ($query) use ($request) { + $query->where('name', 'LIKE', '%' . $request->name . '%'); + }) + ->when($request->school, function ($query) use ($request) { + $query->where('school', 'LIKE', '%' . $request->school . '%'); + }) + ->paginate($request->limit ?? 10); + } + } diff --git a/app/Contracts/Repositories/UserRepository.php b/app/Contracts/Repositories/UserRepository.php index 3e66ac50..d9c8ebf8 100644 --- a/app/Contracts/Repositories/UserRepository.php +++ b/app/Contracts/Repositories/UserRepository.php @@ -30,6 +30,18 @@ public function get(Request $request): mixed ->get(); } + public function getUser(Request $request): mixed + { + return $this->model->query() + ->when($request->email, function ($query) use ($request) { + $query->where(function ($subQuery) use ($request) { + $subQuery->where('name', 'LIKE', '%' . $request->email . '%') + ->orWhere('email', 'LIKE', '%' . $request->email . '%'); + }); + }) + ->paginate(10); + } + public function store(array $data): mixed { return $this->model->query()->create($data); diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index ad09f3fd..00722ad2 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -19,6 +19,14 @@ public function __construct(UserInterface $user ,StudentInterface $student) $this->user = $user; $this->student = $student; } + //get user with paginate + public function getUser(Request $request) + { + $users = $this->user->getUser($request); + + return ResponseHelper::success(UserResource::collection($users) + ); + } public function index() { diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php index fd7085b5..470f0ec5 100644 --- a/app/Http/Controllers/StudentController.php +++ b/app/Http/Controllers/StudentController.php @@ -57,6 +57,46 @@ public function getStudents(): JsonResponse return response()->json($response, 200); } + //sinkron data siswa aktif + public function getSyncedStudents(Request $request) + { + $students = $this->student->getSynced($request); + + return response()->json([ + 'total' => $students->total(), + 'result' => StudentResource::collection($students), + ], 200); + } + + //post sinkron + public function sync(Request $request) + { + $student = $this->student->sync($request->student_id); + + if ($student === 'not_found') { + return response()->json([ + 'message' => 'Student tidak ditemukan' + ], 404); + } + + if ($student === 'not_accepted') { + return response()->json([ + 'message' => 'Student belum accepted' + ], 400); + } + + if ($student === 'already_synced') { + return response()->json([ + 'message' => 'Student sudah disinkronkan' + ], 400); + } + + return response()->json([ + 'message' => 'Student berhasil disinkronkan', + 'result' => new StudentResource($student) + ], 200); + } + public function changeSessionStudent(Request $request,int $session) { $studentIds = $request->input('students', '[]'); diff --git a/app/Providers/GlobalViewLoaderProvider.php b/app/Providers/GlobalViewLoaderProvider.php index 33176d0f..afc9b926 100644 --- a/app/Providers/GlobalViewLoaderProvider.php +++ b/app/Providers/GlobalViewLoaderProvider.php @@ -23,9 +23,9 @@ public function register(): void */ public function boot(): void { - $divisions = Division::all(); - $institutions = Institution::all(); - view()->share('divisions', $divisions); - view()->share('institutions', $institutions); + // $divisions = Division::all(); + // $institutions = Institution::all(); + // view()->share('divisions', $divisions); + // view()->share('institutions', $institutions); } } diff --git a/database/migrations/2026_09_11_080915_add_is_synced_to_student_table.php b/database/migrations/2026_09_11_080915_add_is_synced_to_student_table.php new file mode 100644 index 00000000..39e7acb8 --- /dev/null +++ b/database/migrations/2026_09_11_080915_add_is_synced_to_student_table.php @@ -0,0 +1,28 @@ +boolean('is_synced')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('students', function (Blueprint $table) { + $table->dropColumn('is_synced'); + }); + } +}; diff --git a/routes/api.php b/routes/api.php index 62eba566..72c67776 100644 --- a/routes/api.php +++ b/routes/api.php @@ -31,6 +31,11 @@ Route::middleware('auth:sanctum')->group(function () { Route::put('journal/update/{journal}', [JournalController::class, 'update']); Route::get('users', [UserController::class, 'index']); + //get all user with paginate + Route::get('user', [UserController::class, 'getUser']); + //data siswa sinkron + Route::get('student/sync', [StudentController::class, 'getSyncedStudents']); + Route::post('student/sync', [StudentController::class, 'sync']); Route::post('permision', [PermissionController::class, 'store']); Route::get('profile', [ProfileController::class, 'index']); Route::get('journals', [JournalController::class, 'index']); From cfa8bbb607eb8493da71dc141a6752ac5edf82e6 Mon Sep 17 00:00:00 2001 From: Farisa Damayanti Date: Mon, 14 Sep 2026 11:20:07 +0700 Subject: [PATCH 2/2] rebuild sync --- .../Repositories/StudentRepository.php | 28 +++++----- app/Helpers/SearchHelper.php | 19 +++++++ app/Http/Controllers/StudentController.php | 55 +++++++++++-------- 3 files changed, 65 insertions(+), 37 deletions(-) create mode 100644 app/Helpers/SearchHelper.php diff --git a/app/Contracts/Repositories/StudentRepository.php b/app/Contracts/Repositories/StudentRepository.php index 7f643788..589990bd 100644 --- a/app/Contracts/Repositories/StudentRepository.php +++ b/app/Contracts/Repositories/StudentRepository.php @@ -2,12 +2,13 @@ namespace App\Contracts\Repositories; -use Carbon\Carbon; +use App\Contracts\Interfaces\StudentInterface; +use App\Enum\InternshipTypeEnum; +use App\Enum\StudentStatusEnum; +use App\Helpers\SearchHelper; use App\Models\Student; +use Carbon\Carbon; use Illuminate\Http\Request; -use App\Enum\StudentStatusEnum; -use App\Enum\InternshipTypeEnum; -use App\Contracts\Interfaces\StudentInterface; use Illuminate\Support\Facades\Auth; class StudentRepository extends BaseRepository implements StudentInterface @@ -771,16 +772,17 @@ public function sync(mixed $id): mixed public function getSynced(Request $request): mixed { - return $this->model->query() + $query = $this->model->query() ->where('status', StudentStatusEnum::ACCEPTED->value) - ->where('is_synced', true) - ->when($request->name, function ($query) use ($request) { - $query->where('name', 'LIKE', '%' . $request->name . '%'); - }) - ->when($request->school, function ($query) use ($request) { - $query->where('school', 'LIKE', '%' . $request->school . '%'); - }) - ->paginate($request->limit ?? 10); + ->where('is_synced', false); + + SearchHelper::apply($query, $request->search, [ + 'name', + 'email', + 'school' + ]); + + return $query->paginate($request->limit ?? 10); } } diff --git a/app/Helpers/SearchHelper.php b/app/Helpers/SearchHelper.php new file mode 100644 index 00000000..3b182e0f --- /dev/null +++ b/app/Helpers/SearchHelper.php @@ -0,0 +1,19 @@ +where(function ($query) use ($search, $columns) { + foreach ($columns as $column) { + $query->orWhere($column, 'LIKE', "%{$search}%"); + } + }); + } + + return $query->limit($limit); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php index 470f0ec5..a5f79d39 100644 --- a/app/Http/Controllers/StudentController.php +++ b/app/Http/Controllers/StudentController.php @@ -7,10 +7,11 @@ use App\Contracts\Interfaces\StudentInterface; use App\Contracts\Interfaces\StudentSessionInterface; use App\Contracts\Interfaces\UserInterface; -use App\Models\Student; +use App\Helpers\ResponseHelper; use App\Http\Requests\StoreStudentRequest; use App\Http\Requests\UpdateStudentRequest; use App\Http\Resources\StudentResource; +use App\Models\Student; use App\Models\User; use App\Services\StudentService; use Illuminate\Http\JsonResponse; @@ -60,41 +61,47 @@ public function getStudents(): JsonResponse //sinkron data siswa aktif public function getSyncedStudents(Request $request) { + try { $students = $this->student->getSynced($request); - return response()->json([ + return ResponseHelper::success([ 'total' => $students->total(), 'result' => StudentResource::collection($students), - ], 200); + ]); + + } catch (\Exception $e ) { + return ResponseHelper::error( + null, $e->getMessage() + ); + } } //post sinkron - public function sync(Request $request) + public function sync(Request $request) { - $student = $this->student->sync($request->student_id); + try { + $student = $this->student->sync($request->student_id); - if ($student === 'not_found') { - return response()->json([ - 'message' => 'Student tidak ditemukan' - ], 404); - } + if ($student === 'not_found') { + return ResponseHelper::error(null, 'Student tidak ditemukan'); + } - if ($student === 'not_accepted') { - return response()->json([ - 'message' => 'Student belum accepted' - ], 400); - } + if ($student === 'not_accepted') { + return ResponseHelper::error(null, 'Student belum accepted'); + } - if ($student === 'already_synced') { - return response()->json([ - 'message' => 'Student sudah disinkronkan' - ], 400); - } + if ($student === 'already_synced') { + return ResponseHelper::error(null, 'Student sudah disinkronkan'); + } - return response()->json([ - 'message' => 'Student berhasil disinkronkan', - 'result' => new StudentResource($student) - ], 200); + return ResponseHelper::success( + ['result' => new StudentResource($student)], + 'Student berhasil disinkronkan' + ); + + } catch (\Exception $e) { + return ResponseHelper::error(null, $e->getMessage()); + } } public function changeSessionStudent(Request $request,int $session)