add: db tx commit and rollback implementation

This commit is contained in:
mario
2025-05-15 15:42:33 +07:00
parent 264435f67e
commit d2ec8c0f07
11 changed files with 216 additions and 60 deletions

View File

@@ -6,6 +6,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"
)
// DBDoctor represents a doctor from the database
@@ -44,12 +45,12 @@ func (r *DoctorRepository) GetDoctorDetailsByUserID(userID string) (*DBDoctor, e
return &dbDoctor, nil
}
// CreateDoctor creates a new doctor record
func (r *DoctorRepository) CreateDoctor(doctorDetails *models.DoctorDetails, userID string) error {
// CreateDoctorTx creates a new doctor record within a transaction
func (r *DoctorRepository) CreateDoctorTx(tx *sqlx.Tx, doctorDetails *models.DoctorDetails, userID string) error {
query := `INSERT INTO doctor (Doctor_UsersID, DoctorID, DoctorName, DoctorCreatedAt, DoctorLastUpdatedAt)
VALUES (?, ?, ?, NOW(), NOW())`
_, err := database.DB.Exec(query, userID, doctorDetails.DoctorID, doctorDetails.DoctorName)
_, err := tx.Exec(query, userID, doctorDetails.DoctorID, doctorDetails.DoctorName)
if err != nil {
return fmt.Errorf("database error creating doctor: %w", err)
}

View File

@@ -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)
}

View File

@@ -8,6 +8,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"
)
// DBShortLink represents a shortlink from the database
@@ -69,8 +70,8 @@ func (r *ShortLinkRepository) GetShortLinkByToken(token string) (*models.ShortLi
return dbShortLink.ToShortLink(), nil
}
// CreateShortLink stores a new shortlink in the database
func (r *ShortLinkRepository) CreateShortLink(shortLink *models.ShortLink) error {
// CreateShortLinkTx stores a new shortlink in the database within a transaction
func (r *ShortLinkRepository) CreateShortLinkTx(tx *sqlx.Tx, shortLink *models.ShortLink) error {
query := `INSERT INTO shortlink (
ShortlinkCode,
Shortlink_PatientID,
@@ -93,7 +94,7 @@ func (r *ShortLinkRepository) CreateShortLink(shortLink *models.ShortLink) error
return fmt.Errorf("invalid expiration date: %w", err)
}
_, err = database.DB.Exec(
_, err = tx.Exec(
query,
shortLink.Token,
shortLink.PatientID,
@@ -112,8 +113,8 @@ func (r *ShortLinkRepository) CreateShortLink(shortLink *models.ShortLink) error
return nil
}
// UpdateShortLink updates an existing shortlink in the database
func (r *ShortLinkRepository) UpdateShortLink(shortLink *models.ShortLink) error {
// UpdateShortLinkTx updates an existing shortlink in the database within a transaction
func (r *ShortLinkRepository) UpdateShortLinkTx(tx *sqlx.Tx, shortLink *models.ShortLink) error {
query := `UPDATE shortlink SET
ShortlinkIsRevoked = ?,
ShortlinkRemainingTries = ?,
@@ -125,7 +126,7 @@ func (r *ShortLinkRepository) UpdateShortLink(shortLink *models.ShortLink) error
return fmt.Errorf("invalid expiration date: %w", err)
}
_, err = database.DB.Exec(
_, err = tx.Exec(
query,
shortLink.IsRevoked,
shortLink.RemainingTries,

View File

@@ -6,6 +6,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"
)
// DBStudy represents a study from the database
@@ -69,8 +70,8 @@ func (r *StudyRepository) GetStudyByUID(studyUID string) (*DBStudy, error) {
return &study, nil
}
// CreateStudy creates a new study record for a patient
func (r *StudyRepository) CreateStudy(patientID string, study models.Study) error {
// CreateStudyTx creates a new study record for a patient within a transaction
func (r *StudyRepository) CreateStudyTx(tx *sqlx.Tx, patientID string, study models.Study) error {
// Parse study date if provided
var studyDate *time.Time
if study.StudyDate != "" {
@@ -85,7 +86,7 @@ func (r *StudyRepository) CreateStudy(patientID string, study models.Study) erro
(Study_PatientID, StudyIUID, StudyAccessionNumber, StudyDate, StudyCreatedAt, StudyUpdatedAt)
VALUES (?, ?, ?, ?, NOW(), NOW())`
_, err := database.DB.Exec(query,
_, err := tx.Exec(query,
patientID,
study.StudyInstanceUID,
study.AccessionNumber,

View File

@@ -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"
"golang.org/x/crypto/bcrypt"
)
@@ -140,8 +141,8 @@ func (r *UserRepository) RevokeRefreshToken(token string) error {
return nil
}
// CreateUser creates a new user
func (r *UserRepository) CreateUser(user *models.User) error {
// CreateUserTx creates a new user within a transaction
func (r *UserRepository) CreateUserTx(tx *sqlx.Tx, user *models.User) error {
// Hash the password before storing
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
@@ -151,7 +152,7 @@ func (r *UserRepository) CreateUser(user *models.User) error {
query := `INSERT INTO user (UserEmail, UserPassword, UserRole, UserName, UserCreatedAt, UserUpdatedAt)
VALUES (?, ?, ?, ?, NOW(), NOW())`
result, err := database.DB.Exec(query, user.Email, string(hashedPassword), user.Role, user.Name)
result, err := tx.Exec(query, user.Email, string(hashedPassword), user.Role, user.Name)
if err != nil {
return fmt.Errorf("database error creating user: %w", err)
}