From 17c5a133f50afe16495f6291686a1d590593668d Mon Sep 17 00:00:00 2001 From: "achmad.nauval.0510" Date: Mon, 1 Dec 2025 11:38:25 +0700 Subject: [PATCH] fixing crud registrasi, penambahan lokasi dan display lokasi --- internal/handlers/registrasi/registrasih.go | 45 +++++-------- internal/models/registrasi/registrasi.go | 70 ++++++++++++--------- 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/internal/handlers/registrasi/registrasih.go b/internal/handlers/registrasi/registrasih.go index cc493bd9..a5834679 100644 --- a/internal/handlers/registrasi/registrasih.go +++ b/internal/handlers/registrasi/registrasih.go @@ -412,35 +412,10 @@ func (h *RegistrationCounterHandler) GetRegistrationCounterStats(c *gin.Context) }) } -// Database operations -// func (h *RegistrationCounterHandler) getRegistrationCounterByID(ctx context.Context, dbConn *sql.DB, id int64) (*registrasi.MsRegistrationCounter, error) { -// query := `SELECT id, name, code, icon, quota, active, fk_ref_healtcare_type_id, fk_ref_service_type_id, fk_sd_location_id, ds_sd_location -// FROM master.ms_registration_counter WHERE id = $1` -// row := dbConn.QueryRowContext(ctx, query, id) - -// var item registrasi.MsRegistrationCounter -// err := row.Scan( -// &item.ID, -// &item.Name, -// &item.Code, -// &item.Icon, -// &item.Quota, -// &item.Active, -// &item.FKRefHealtcareTypeID, -// &item.FKRefServiceTypeID, -// &item.FKSdLocationID, -// &item.DsSdLocation, -// ) -// if err != nil { -// return nil, err -// } - -// return &item, nil -// } func (h *RegistrationCounterHandler) getRegistrationCounterByID(ctx context.Context, dbConn *sql.DB, id int64) (*registrasi.MsRegistrationCounterDetail, error) { query := ` - SELECT rc.id, rc.name, rc.code, rc.icon, rc.quota, rc.active, + SELECT rc.id, rc.name, rc.code, rc.icon, rc.quota, rc.active, rc.fk_sd_location_id as location, rc.ds_sd_location as displaylocation, ht.name as healthcaretype, st.name as servicetype FROM master.ms_registration_counter rc @@ -457,6 +432,9 @@ func (h *RegistrationCounterHandler) getRegistrationCounterByID(ctx context.Cont &item.Icon, &item.Quota, &item.Active, + &item.Location, + &item.DisplayLocation, + &item.HealthCareType, &item.ServiceType, ) @@ -473,6 +451,12 @@ func (h *RegistrationCounterHandler) createRegistrationCounter(ctx context.Conte VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING id, name, code, icon, quota, active, fk_ref_healtcare_type_id, fk_ref_service_type_id, fk_sd_location_id, ds_sd_location` + var fkSdLocationID interface{} = req.FKSdLocationID + // Jika string kosong, ubah menjadi nil agar dikirim sebagai NULL ke database + if req.FKSdLocationID == "" { + fkSdLocationID = nil + } + row := dbConn.QueryRowContext( ctx, query, @@ -483,7 +467,7 @@ func (h *RegistrationCounterHandler) createRegistrationCounter(ctx context.Conte req.Active, req.FKRefHealtcareTypeID, req.FKRefServiceTypeID, - req.FKSdLocationID, + fkSdLocationID, req.DsSdLocation, ) @@ -513,6 +497,11 @@ func (h *RegistrationCounterHandler) updateRegistrationCounter(ctx context.Conte WHERE id = $1 RETURNING id, name, code, icon, quota, active, fk_ref_healtcare_type_id, fk_ref_service_type_id, fk_sd_location_id, ds_sd_location` + var fkSdLocationID interface{} = req.FKSdLocationID + if req.FKSdLocationID == "" { + fkSdLocationID = nil + } + row := dbConn.QueryRowContext( ctx, query, @@ -524,7 +513,7 @@ func (h *RegistrationCounterHandler) updateRegistrationCounter(ctx context.Conte req.Active, req.FKRefHealtcareTypeID, req.FKRefServiceTypeID, - req.FKSdLocationID, + fkSdLocationID, req.DsSdLocation, ) diff --git a/internal/models/registrasi/registrasi.go b/internal/models/registrasi/registrasi.go index 0f069263..ad721a76 100644 --- a/internal/models/registrasi/registrasi.go +++ b/internal/models/registrasi/registrasi.go @@ -21,23 +21,23 @@ type MsRegistrationCounter struct { DsSdLocation sql.NullString `json:"ds_sd_location,omitempty" db:"ds_sd_location"` } -type MsRegistrationCounterDetail struct { - ID int64 `json:"id" db:"id"` - Name sql.NullString `json:"name,omitempty" db:"name"` - Code sql.NullString `json:"code,omitempty" db:"code"` - Icon sql.NullString `json:"icon,omitempty" db:"icon"` - Quota sql.NullInt16 `json:"quota,omitempty" db:"quota"` - Active sql.NullBool `json:"active,omitempty" db:"active"` - HealthCareType sql.NullString `json:"healthcaretype,omitempty" db:"healthcaretype"` - ServiceType sql.NullString `json:"servicetype,omitempty" db:"fk_ref_service_type_id"` -} + type MsRegistrationCounterDetail struct { + ID int64 `json:"id" db:"id"` + Name sql.NullString `json:"name,omitempty" db:"name"` + Code sql.NullString `json:"code,omitempty" db:"code"` + Icon sql.NullString `json:"icon,omitempty" db:"icon"` + Quota sql.NullInt16 `json:"quota,omitempty" db:"quota"` + Active sql.NullBool `json:"active,omitempty" db:"active"` + HealthCareType sql.NullString `json:"healthcaretype,omitempty" db:"healthcaretype"` + ServiceType sql.NullString `json:"servicetype,omitempty" db:"servicetype"` + Location sql.NullString `json:"location" db:"location"` + DisplayLocation sql.NullString `json:"displaylocation" db:"displaylocation"` + } func (m MsRegistrationCounterDetail) MarshalJSON() ([]byte, error) { - // Buat alias untuk menghindari rekursi tak terbatas saat pemanggilan json.Marshal type Alias MsRegistrationCounterDetail - // Buat struct anonim dengan field pointer untuk menangani nilai NULL aux := &struct { Name *string `json:"name,omitempty"` Code *string `json:"code,omitempty"` @@ -46,13 +46,13 @@ func (m MsRegistrationCounterDetail) MarshalJSON() ([]byte, error) { Active *bool `json:"active,omitempty"` HealthCareType *string `json:"healthcaretype,omitempty"` ServiceType *string `json:"servicetype,omitempty"` - // Embed alias untuk menyertakan field non-nullable seperti 'id' + Location *string `json:"location"` + DisplayLocation *string `json:"displaylocation"` *Alias }{ Alias: (*Alias)(&m), } - // Jika field asli valid, isi pointer di struct anonim if m.Name.Valid { aux.Name = &m.Name.String } @@ -68,6 +68,21 @@ func (m MsRegistrationCounterDetail) MarshalJSON() ([]byte, error) { if m.Active.Valid { aux.Active = &m.Active.Bool } + + if m.Location.Valid { + aux.Location = &m.Location.String + } else { + emptyStr := "" + aux.Location = &emptyStr + } + + if m.DisplayLocation.Valid { + aux.DisplayLocation = &m.DisplayLocation.String + } else { + emptyStr := "" + aux.DisplayLocation = &emptyStr + } + if m.HealthCareType.Valid { aux.HealthCareType = &m.HealthCareType.String } @@ -75,7 +90,6 @@ func (m MsRegistrationCounterDetail) MarshalJSON() ([]byte, error) { aux.ServiceType = &m.ServiceType.String } - // Marshal struct anonim yang sudah "bersih" return json.Marshal(aux) } @@ -184,15 +198,15 @@ type MsRegistrationCounterGetResponse struct { } type MsRegistrationCounterCreateRequest struct { - Name string `json:"name" validate:"required,min=1,max=20"` - Code string `json:"code" validate:"required,min=1,max=3"` - Icon string `json:"icon" validate:"min=1,max=20"` + Name string `json:"name" validate:"required,min=1"` + Code string `json:"code"` + Icon string `json:"icon"` Quota int16 `json:"quota" validate:"min=0"` Active bool `json:"active"` - FKRefHealtcareTypeID int32 `json:"fk_ref_healtcare_type_id" validate:"min=1"` - FKRefServiceTypeID int32 `json:"fk_ref_service_type_id" validate:"min=1"` + FKRefHealtcareTypeID int32 `json:"fk_ref_healtcare_type_id"` + FKRefServiceTypeID int32 `json:"fk_ref_service_type_id"` FKSdLocationID string `json:"fk_sd_location_id"` - DsSdLocation string `json:"ds_sd_location" validate:"min=1,max=255"` + DsSdLocation string `json:"ds_sd_location"` } type MsRegistrationCounterCreateResponse struct { @@ -202,15 +216,15 @@ type MsRegistrationCounterCreateResponse struct { type MsRegistrationCounterUpdateRequest struct { ID int `json:"id" validate:"required,min=1"` - Name string `json:"name" validate:"required,min=1,max=20"` - Code string `json:"code" validate:"required,min=1,max=3"` - Icon string `json:"icon" validate:"min=1,max=20"` + Name string `json:"name" validate:"required,min=1"` + Code string `json:"code" validate:"required,min=1"` + Icon string `json:"icon" validate:"min=1"` Quota int16 `json:"quota" validate:"min=0"` Active bool `json:"active"` - FKRefHealtcareTypeID int32 `json:"fk_ref_healtcare_type_id" validate:"min=1"` - FKRefServiceTypeID int32 `json:"fk_ref_service_type_id" validate:"min=1"` - FKSdLocationID string `json:"fk_sd_location_id" validate:"required"` - DsSdLocation string `json:"ds_sd_location" validate:"required,min=1,max=255"` + FKRefHealtcareTypeID int32 `json:"fk_ref_healtcare_type_id"` + FKRefServiceTypeID int32 `json:"fk_ref_service_type_id"` + FKSdLocationID string `json:"fk_sd_location_id"` + DsSdLocation string `json:"ds_sd_location"` }