From 2adc431f7268425e720f594616a793810a0347c1 Mon Sep 17 00:00:00 2001 From: Farhan Ramadhana Date: Sun, 19 May 2024 16:50:58 +0700 Subject: [PATCH 1/2] change column name --- db/migrations/20240516162425_add_medical_records_table.up.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrations/20240516162425_add_medical_records_table.up.sql b/db/migrations/20240516162425_add_medical_records_table.up.sql index e17f679..7b68a73 100644 --- a/db/migrations/20240516162425_add_medical_records_table.up.sql +++ b/db/migrations/20240516162425_add_medical_records_table.up.sql @@ -1,5 +1,5 @@ CREATE TABLE medical_records ( - identityNumber NUMERIC(16) NOT NULL, + identity_number NUMERIC(16) NOT NULL, symptoms VARCHAR(2000) NOT NULL, medications VARCHAR(2000) NOT NULL, created_by VARCHAR(255), From 62e73f4502054deae4982931ab435c38f3033c35 Mon Sep 17 00:00:00 2001 From: Farhan Ramadhana Date: Sun, 19 May 2024 17:15:30 +0700 Subject: [PATCH 2/2] fix migrations --- ...516162425_add_medical_records_table.up.sql | 1 + src/repository/patient_repository.go | 28 ++++++++----------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/db/migrations/20240516162425_add_medical_records_table.up.sql b/db/migrations/20240516162425_add_medical_records_table.up.sql index 7b68a73..c09f10d 100644 --- a/db/migrations/20240516162425_add_medical_records_table.up.sql +++ b/db/migrations/20240516162425_add_medical_records_table.up.sql @@ -1,4 +1,5 @@ CREATE TABLE medical_records ( + id SERIAL PRIMARY KEY, identity_number NUMERIC(16) NOT NULL, symptoms VARCHAR(2000) NOT NULL, medications VARCHAR(2000) NOT NULL, diff --git a/src/repository/patient_repository.go b/src/repository/patient_repository.go index eec7ee0..0808451 100644 --- a/src/repository/patient_repository.go +++ b/src/repository/patient_repository.go @@ -112,11 +112,12 @@ func (ur *PatientRepository) GetPatients(ctx context.Context, param dto.RequestG func (ur *PatientRepository) GetRecords(ctx context.Context, request dto.RequestGetRecord) ([]dto.MedicalRecords, error) { var records []dto.MedicalRecords - query := "SELECT medical_records.identity_number, medical_records.symptoms, medical_records.medications, medical_records.created_by, medical_records.created_at FROM medical_records LEFT JOIN users ON medical_records.created_by = users.name WHERE 1=1" + query := "SELECT medical_records.symptoms, medical_records.medications, medical_records.created_at, users.name AS created_by_name, users.nip AS created_by_nip, users.user_id AS created_by_user_id, patients.phone_number, patients.identity_number, patients.name, patients.birth_date, patients.gender, patients.identity_card_scan_img FROM medical_records LEFT JOIN patients ON medical_records.identity_number = patients.identity_number LEFT JOIN users ON medical_records.created_by = users.user_id WHERE 1=1" + var params []interface{} if request.IdentityDetail.IdentityNumber != 0 { - query += " AND medical_records.identity_number = ?" + query += " AND medical_records.identity_number = CAST(? AS VARCHAR)" params = append(params, request.IdentityDetail.IdentityNumber) } @@ -134,15 +135,7 @@ func (ur *PatientRepository) GetRecords(ctx context.Context, request dto.Request query += " ORDER BY medical_records.created_at " + request.CreatedAt } - if request.Limit > 0 { - query += " LIMIT ?" - params = append(params, request.Limit) - } - - if request.Offset >= 0 { - query += " OFFSET ?" - params = append(params, request.Offset) - } + query += fmt.Sprintf(" LIMIT %d OFFSET %d", request.Limit, request.Offset) rows, err := ur.db.QueryContext(ctx, query, params...) if err != nil { @@ -153,18 +146,19 @@ func (ur *PatientRepository) GetRecords(ctx context.Context, request dto.Request for rows.Next() { var ptn dto.MedicalRecords if err := rows.Scan( + &ptn.Symptoms, + &ptn.Medications, + &ptn.CreatedAt, + &ptn.CreatedBy.Name, + &ptn.CreatedBy.Nip, + &ptn.CreatedBy.UserId, &ptn.IdentityDetail.IdentityNumber, &ptn.IdentityDetail.PhoneNumber, &ptn.IdentityDetail.Name, &ptn.IdentityDetail.BirthDate, &ptn.IdentityDetail.Gender, &ptn.IdentityDetail.IdentityCardScanImg, - &ptn.Symptoms, - &ptn.Medications, - &ptn.CreatedBy.Name, - &ptn.CreatedBy.Nip, - &ptn.CreatedBy.UserId, - &ptn.CreatedAt); err != nil { + ); err != nil { return nil, err }