package models import ( "api-service/internal/models" "database/sql" "encoding/json" ) // ================================================================================= // 1. MODEL DATABASE (Core Struct) // ================================================================================= // RefPaymentType mewakili tabel reference.ref_payment_type type RefPaymentType struct { ID int32 `json:"id" db:"id"` Name sql.NullString `json:"name,omitempty" db:"name"` Active sql.NullBool `json:"active,omitempty" db:"active"` } // MarshalJSON adalah custom JSON marshaller untuk menghasilkan output yang bersih. func (r RefPaymentType) MarshalJSON() ([]byte, error) { // Buat alias untuk menghindari rekursi tak terbatas saat pemanggilan json.Marshal type Alias RefPaymentType aux := &struct { Name *string `json:"name,omitempty"` Active *bool `json:"active,omitempty"` *Alias }{ Alias: (*Alias)(&r), } // Jika field asli valid, isi pointer di struct anonim if r.Name.Valid { aux.Name = &r.Name.String } if r.Active.Valid { aux.Active = &r.Active.Bool } return json.Marshal(aux) } // --- Helper Methods untuk Akses Nilai yang Aman --- func (r *RefPaymentType) GetName() string { if r.Name.Valid { return r.Name.String } return "" } func (r *RefPaymentType) GetActive() bool { if r.Active.Valid { return r.Active.Bool } return false } // ================================================================================= // 2. MODEL UNTUK RESPONSE API (Read-Only) // ================================================================================= // RefPaymentTypeGetResponse adalah model untuk response GET by ID type RefPaymentTypeGetResponse struct { Message string `json:"message"` Data *RefPaymentType `json:"data"` } // RefPaymentTypeGetListResponse adalah model untuk response GET all / dengan pagination type RefPaymentTypeGetListResponse struct { Message string `json:"message"` Data []RefPaymentType `json:"data"` Meta models.MetaResponse `json:"meta"` } // RefPaymentTypeFilter adalah model untuk filter pada endpoint GET list type RefPaymentTypeFilter struct { Active *bool `json:"active,omitempty" form:"active"` // Digunakan untuk query parameter ?active=true Search *string `json:"search,omitempty" form:"search"` // Digunakan untuk query parameter ?search=cash }