Backend

Controllers

Documentation of all controllers in the Klinik Gunung Health Screening System backend

Controllers

This section documents all the controllers used in the Klinik Gunung Health Screening System. Controllers handle the business logic for API endpoints and manage data flow between models and views.

Controller Structure

The application follows Laravel's MVC pattern with the following controller structure:

app/Http/Controllers/
├── API/
│   ├── V1/
│   │   ├── AuthController.php
│   │   ├── PatientController.php
│   │   ├── ScreeningController.php
│   │   ├── QuestionnaireController.php
│   │   ├── PhysicalExaminationController.php
│   │   ├── PaymentController.php
│   │   └── UploadController.php
│   └── Controller.php
└── Controller.php

AuthController

Handles user authentication, registration, and profile management.

Methods:

login(Request $request)

Authenticates user credentials and returns JWT token.

public function login(LoginRequest $request): JsonResponse
{
    $credentials = $request->only('email', 'password');

    if (!$token = auth()->attempt($credentials)) {
        return response()->json([
            'success' => false,
            'message' => 'Invalid credentials'
        ], 401);
    }

    return response()->json([
        'success' => true,
        'data' => [
            'user' => auth()->user(),
            'token' => $token
        ]
    ]);
}

register(Request $request)

Creates new user account with validation.

logout()

Invalidates current user token.

me()

Returns authenticated user information.

PatientController

Manages patient records and related operations.

Methods:

index(Request $request)

Returns paginated list of patients with optional search.

public function index(Request $request): JsonResponse
{
    $query = Patient::query();

    if ($request->has('search')) {
        $search = $request->search;
        $query->where('name', 'like', "%{$search}%")
              ->orWhere('nik', 'like', "%{$search}%");
    }

    $patients = $query->paginate($request->per_page ?? 10);

    return response()->json([
        'success' => true,
        'data' => $patients
    ]);
}

store(PatientRequest $request)

Creates new patient record with validation.

show(Patient $patient)

Returns specific patient details.

update(PatientRequest $request, Patient $patient)

Updates patient information.

destroy(Patient $patient)

Archives patient record (soft delete).

ScreeningController

Handles health screening sessions and processes.

Methods:

index()

Returns list of screenings with patient information.

store(ScreeningRequest $request)

Creates new screening session.

public function store(ScreeningRequest $request): JsonResponse
{
    $screening = Screening::create([
        'patient_id' => $request->patient_id,
        'screening_type' => $request->screening_type,
        'scheduled_date' => $request->scheduled_date,
        'status' => 'scheduled',
        'notes' => $request->notes
    ]);

    return response()->json([
        'success' => true,
        'data' => $screening->load('patient')
    ], 201);
}

show(Screening $screening)

Returns screening details with questionnaire and answers.

update(ScreeningRequest $request, Screening $screening)

Updates screening information.

complete(Screening $screening)

Marks screening as completed and generates results.

QuestionnaireController

Manages screening questionnaires and questions.

Methods:

index()

Returns available questionnaires.

show(Questionnaire $questionnaire)

Returns questionnaire with all questions.

submitAnswers(Request $request, Screening $screening)

Processes and stores questionnaire answers.

public function submitAnswers(Request $request, Screening $screening): JsonResponse
{
    $answers = $request->answers;

    foreach ($answers as $answer) {
        ScreeningAnswer::create([
            'screening_id' => $screening->id,
            'question_id' => $answer['question_id'],
            'answer' => $answer['answer'],
            'notes' => $answer['notes'] ?? null
        ]);
    }

    return response()->json([
        'success' => true,
        'message' => 'Answers submitted successfully'
    ]);
}

PhysicalExaminationController

Handles physical examination data and medical measurements.

Methods:

index()

Returns physical examination records.

store(PhysicalExaminationRequest $request)

Creates new physical examination record.

public function store(PhysicalExaminationRequest $request): JsonResponse
{
    $examination = PhysicalExamination::create([
        'screening_id' => $request->screening_id,
        'blood_pressure' => $request->blood_pressure,
        'heart_rate' => $request->heart_rate,
        'temperature' => $request->temperature,
        'height' => $request->height,
        'weight' => $request->weight,
        'bmi' => $request->weight / (($request->height / 100) ** 2),
        'examination_notes' => $request->examination_notes
    ]);

    return response()->json([
        'success' => true,
        'data' => $examination
    ], 201);
}

show(PhysicalExamination $examination)

Returns examination details.

update(PhysicalExaminationRequest $request, PhysicalExamination $examination)

Updates examination data.

PaymentController

Manages payment records and transactions.

Methods:

index()

Returns payment records with filtering options.

store(PaymentRequest $request)

Creates new payment record.

public function store(PaymentRequest $request): JsonResponse
{
    $payment = Payment::create([
        'screening_id' => $request->screening_id,
        'amount' => $request->amount,
        'payment_method' => $request->payment_method,
        'status' => $request->status ?? 'pending',
        'transaction_id' => $request->transaction_id,
        'notes' => $request->notes
    ]);

    return response()->json([
        'success' => true,
        'data' => $payment->load('screening.patient')
    ], 201);
}

show(Payment $payment)

Returns payment details.

updateStatus(Request $request, Payment $payment)

Updates payment status (paid, failed, refunded).

UploadController

Handles file uploads for documents and images.

Methods:

uploadKtp(Request $request)

Uploads and validates KTP (ID card) images.

public function uploadKtp(Request $request): JsonResponse
{
    $request->validate([
        'file' => 'required|image|mimes:jpeg,png,jpg|max:2048',
        'patient_id' => 'required|exists:patients,id'
    ]);

    $file = $request->file('file');
    $filename = 'ktp_' . $request->patient_id . '_' . time() . '.' . $file->getClientOriginalExtension();

    $path = $file->storeAs('ktp_images', $filename, 'public');

    $patient = Patient::find($request->patient_id);
    $patient->update(['ktp_images' => $path]);

    return response()->json([
        'success' => true,
        'data' => [
            'path' => $path,
            'url' => asset('storage/' . $path)
        ]
    ]);
}

uploadAvatar(Request $request)

Uploads user avatar images.

Request Validation Classes

The application uses Form Request classes for validation:

  • LoginRequest
  • PatientRequest
  • ScreeningRequest
  • PhysicalExaminationRequest
  • PaymentRequest

Each request class contains validation rules and custom error messages.

Middleware

auth:api

Protects authenticated routes using JWT tokens.

role:admin|doctor

Checks user roles for specific permissions.

cors

Handles Cross-Origin Resource Sharing for API requests.

Error Handling

All controllers extend a base ApiController that provides consistent error responses:

protected function successResponse($data = null, string $message = 'Success', int $status = 200): JsonResponse
{
    return response()->json([
        'success' => true,
        'message' => $message,
        'data' => $data
    ], $status);
}

protected function errorResponse(string $message = 'Error', int $status = 400, $errors = null): JsonResponse
{
    $response = [
        'success' => false,
        'message' => $message
    ];

    if ($errors) {
        $response['errors'] = $errors;
    }

    return response()->json($response, $status);
}