Files
seadoc/store/gorm.go
Vladimir V Maksimov 1619c1d9b1 gorm store update
2025-11-06 17:27:54 +03:00

106 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package store
import (
"git.gm6.ru/icewind/seadoc/models"
"github.com/google/uuid"
"gorm.io/gorm"
)
// GormStorage реализует интерфейс VersionStorage через GORM
type GormStorage struct {
open func() *gorm.DB
}
func NewGormStorage(open func() *gorm.DB) *GormStorage {
return &GormStorage{open: open}
}
func (s *GormStorage) CreateDocument(doc *models.Document) error {
err := s.open().Create(&doc).Error
return err
}
func (s *GormStorage) GetDocument(id uuid.UUID) (*models.Document, error) {
var d models.Document
if err := s.open().First(&d, "id = ?", id).Error; err != nil {
return nil, err
}
return &d, nil
}
func (s *GormStorage) UpdateLatestVersion(docID, versionID uuid.UUID) error {
return s.open().Model(&models.Document{}).
Where("id = ?", docID).
Update("latest_version", versionID).Error
}
func (s *GormStorage) CreateVersion(v *models.Version) error {
return s.open().Create(v).Error
}
func (s *GormStorage) GetLatestVersion(docID uuid.UUID) (*models.Version, error) {
var v models.Version
err := s.open().
Where("document_id = ?", docID).
Order("created_at desc").
First(&v).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &v, err
}
func (s *GormStorage) GetVersionByID(id uuid.UUID) (*models.Version, error) {
var v models.Version
if err := s.open().First(&v, "id = ?", id).Error; err != nil {
return nil, err
}
return &v, nil
}
func (s *GormStorage) GetParentVersion(v *models.Version) (*models.Version, error) {
if v.ParentID == nil {
return nil, nil
}
var parent models.Version
if err := s.open().First(&parent, "id = ?", v.ParentID).Error; err != nil {
return nil, err
}
return &parent, nil
}
// CountSinceLastSnapshot возвращает количество версий (включая текущую)
// с момента последнего snapshot
func (s *GormStorage) CountSinceLastSnapshot(docID uuid.UUID) (int, error) {
// Получаем последнюю версию
v, err := s.GetLatestVersion(docID)
if err != nil {
return 0, err
}
if v == nil {
return 0, nil
}
count := 0
for v != nil {
count++
// Если это снапшот, считать больше не нужно
if v.IsSnapshot {
break
}
// Переходим к родителю
if v.ParentID == nil {
break
}
v, err = s.GetVersionByID(*v.ParentID)
if err != nil {
return count, err
}
}
return count, nil
}