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

@@ -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,