Files
go-ohif-proxy/internal/api/repository/shortlink.go

167 lines
4.6 KiB
Go

package repository
import (
"database/sql"
"fmt"
"strconv"
"time"
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/models"
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/database"
)
// DBShortLink represents a shortlink from the database
type DBShortLink struct {
ShortlinkID int `db:"ShortlinkID"`
ShortlinkCode string `db:"ShortlinkCode"`
Shortlink_PatientID string `db:"Shortlink_PatientID"`
Shortlink_Study_IUID string `db:"Shortlink_Study_IUID"`
ShortlinkHashDoB string `db:"ShortlinkHashDoB"`
ShortlinkExpiredAt time.Time `db:"ShortlinkExpiredAt"`
ShortlinkIsRevoked bool `db:"ShortlinkIsRevoked"`
ShortlinkRemainingTries int `db:"ShortlinkRemainingTries"`
ShortlinkCreatedAt time.Time `db:"ShortlinkCreatedAt"`
ShortlinkCreate_UserID int `db:"ShortlinkCreate_UserID"`
}
// ShortLinkRepository handles database operations related to shortlinks
type ShortLinkRepository struct {
*Repository
}
// NewShortLinkRepository creates a new shortlink repository
func NewShortLinkRepository() *ShortLinkRepository {
return &ShortLinkRepository{
Repository: NewRepository(),
}
}
// ToShortLink converts a DBShortLink to a ShortLink model
func (s *DBShortLink) ToShortLink() *models.ShortLink {
return &models.ShortLink{
ID: fmt.Sprintf("%d", s.ShortlinkID),
Token: s.ShortlinkCode,
PatientID: s.Shortlink_PatientID,
StudyUID: s.Shortlink_Study_IUID,
HashedDOB: s.ShortlinkHashDoB,
ExpiresAt: s.ShortlinkExpiredAt.Format(time.RFC3339),
IsRevoked: s.ShortlinkIsRevoked,
RemainingTries: s.ShortlinkRemainingTries,
CreatedAt: s.ShortlinkCreatedAt.Format(time.RFC3339),
CreatedByID: fmt.Sprintf("%d", s.ShortlinkCreate_UserID),
}
}
// GetShortLinkByToken retrieves a shortlink by token
func (r *ShortLinkRepository) GetShortLinkByToken(token string) (*models.ShortLink, error) {
var dbShortLink DBShortLink
query := `SELECT * FROM shortlink WHERE ShortlinkCode = ?`
err := database.DB.Get(&dbShortLink, query, token)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, fmt.Errorf("database error getting shortlink: %w", err)
}
return dbShortLink.ToShortLink(), nil
}
// CreateShortLink stores a new shortlink in the database
func (r *ShortLinkRepository) CreateShortLink(shortLink *models.ShortLink) error {
query := `INSERT INTO shortlink (
ShortlinkCode,
Shortlink_PatientID,
Shortlink_Study_IUID,
ShortlinkHashDoB,
ShortlinkExpiredAt,
ShortlinkIsRevoked,
ShortlinkRemainingTries,
ShortlinkCreatedAt,
ShortlinkCreate_UserID)
VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), ?)`
createdByID, err := strconv.Atoi(shortLink.CreatedByID)
if err != nil {
return fmt.Errorf("invalid created by ID: %w", err)
}
expiresAt, err := time.Parse(time.RFC3339, shortLink.ExpiresAt)
if err != nil {
return fmt.Errorf("invalid expiration date: %w", err)
}
_, err = database.DB.Exec(
query,
shortLink.Token,
shortLink.PatientID,
shortLink.StudyUID,
shortLink.HashedDOB,
expiresAt,
shortLink.IsRevoked,
shortLink.RemainingTries,
createdByID,
)
if err != nil {
return fmt.Errorf("database error creating shortlink: %w", err)
}
return nil
}
// UpdateShortLink updates an existing shortlink in the database
func (r *ShortLinkRepository) UpdateShortLink(shortLink *models.ShortLink) error {
query := `UPDATE shortlink SET
ShortlinkIsRevoked = ?,
ShortlinkRemainingTries = ?,
ShortlinkExpiredAt = ?
WHERE ShortlinkCode = ?`
expiresAt, err := time.Parse(time.RFC3339, shortLink.ExpiresAt)
if err != nil {
return fmt.Errorf("invalid expiration date: %w", err)
}
_, err = database.DB.Exec(
query,
shortLink.IsRevoked,
shortLink.RemainingTries,
expiresAt,
shortLink.Token,
)
if err != nil {
return fmt.Errorf("database error updating shortlink: %w", err)
}
return nil
}
// GetActiveShortLinkByPatientAndStudy retrieves an active (unexpired, not revoked) shortlink
// for the given patient ID and study UID
func (r *ShortLinkRepository) GetActiveShortLinkByPatientAndStudy(patientID string, studyUID string) (*models.ShortLink, error) {
var dbShortLink DBShortLink
query := `SELECT * FROM shortlink
WHERE Shortlink_PatientID = ?
AND Shortlink_Study_IUID = ?
AND ShortlinkExpiredAt > NOW()
AND ShortlinkIsRevoked = FALSE
ORDER BY ShortlinkExpiredAt DESC
LIMIT 1`
err := database.DB.Get(&dbShortLink, query, patientID, studyUID)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, fmt.Errorf("database error getting active shortlink: %w", err)
}
return dbShortLink.ToShortLink(), nil
}