add: db tx commit and rollback implementation
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/models"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/database"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// DBPatient represents a patient from the database
|
||||
@@ -61,9 +62,9 @@ func (r *PatientRepository) GetPatientDetailsByUserID(userID string) (*models.Pa
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreatePatient creates a new patient record
|
||||
func (r *PatientRepository) CreatePatient(patientRecord *models.PatientDetails, userID string) error {
|
||||
// Parse DOB to time.Time
|
||||
// CreatePatientTx creates a new patient record within a transaction
|
||||
func (r *PatientRepository) CreatePatientTx(tx *sqlx.Tx, patientRecord *models.PatientDetails, userID string) error {
|
||||
// Parse DOB to time.Time. 2006-1-02 = reference format YYYY-MM-DD in Go, not a default value
|
||||
dob, err := time.Parse("2006-01-02", patientRecord.DateOfBirth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid date of birth format: %w", err)
|
||||
@@ -72,7 +73,7 @@ func (r *PatientRepository) CreatePatient(patientRecord *models.PatientDetails,
|
||||
query := `INSERT INTO patient (Patient_UsersID, PatientMedrec, PatientName, PatientDoB, PatientCreatedAt, PatientUpdatedAt)
|
||||
VALUES (?, ?, ?, ?, NOW(), NOW())`
|
||||
|
||||
_, err = database.DB.Exec(query, userID, patientRecord.PatientID, patientRecord.PatientName, dob)
|
||||
_, err = tx.Exec(query, userID, patientRecord.PatientID, patientRecord.PatientName, dob)
|
||||
if err != nil {
|
||||
return fmt.Errorf("database error creating patient: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user