Files
backend/internal/repository/document_version_repo/get_latest.go
T
2026-07-15 22:42:31 +08:00

42 lines
873 B
Go

package document_version_repo
import (
"github.com/XingfenD/yoresee_doc/internal/model"
"gorm.io/gorm"
)
type DocumentVersionGetLastedOperation struct {
repo *DocumentVersionRepository
docID int64
tx *gorm.DB
}
func (r *DocumentVersionRepository) GetLasted(docID int64) *DocumentVersionGetLastedOperation {
return &DocumentVersionGetLastedOperation{
repo: r,
docID: docID,
}
}
func (op *DocumentVersionGetLastedOperation) WithTx(tx *gorm.DB) *DocumentVersionGetLastedOperation {
op.tx = tx
return op
}
func (op *DocumentVersionGetLastedOperation) Exec() (int64, error) {
if op.tx == nil {
op.tx = op.repo.db
}
var maxVersion int64
err := op.tx.Model(&model.DocumentVersion{}).
Where("document_id = ?", op.docID).
Select("COALESCE(MAX(version), 0)").
Scan(&maxVersion).Error
if err != nil {
return 0, err
}
return maxVersion, nil
}