upd store
This commit is contained in:
@@ -15,15 +15,9 @@ func NewGormStorage(db *gorm.DB) *GormStorage {
|
||||
return &GormStorage{db: db}
|
||||
}
|
||||
|
||||
func (s *GormStorage) CreateDocument(name string) (uuid.UUID, error) {
|
||||
doc := models.Document{
|
||||
ID: uuid.New(),
|
||||
Name: name,
|
||||
}
|
||||
if err := s.db.Create(&doc).Error; err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
return doc.ID, nil
|
||||
func (s *GormStorage) CreateDocument(doc *models.Document) error {
|
||||
err := s.db.Create(&doc).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *GormStorage) GetDocument(id uuid.UUID) (*models.Document, error) {
|
||||
@@ -46,13 +40,14 @@ func (s *GormStorage) CreateVersion(v *models.Version) error {
|
||||
|
||||
func (s *GormStorage) GetLatestVersion(docID uuid.UUID) (*models.Version, error) {
|
||||
var v models.Version
|
||||
if err := s.db.
|
||||
err := s.db.
|
||||
Where("document_id = ?", docID).
|
||||
Order("created_at desc").
|
||||
First(&v).Error; err != nil {
|
||||
return nil, err
|
||||
First(&v).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return &v, nil
|
||||
return &v, err
|
||||
}
|
||||
|
||||
func (s *GormStorage) GetVersionByID(id uuid.UUID) (*models.Version, error) {
|
||||
@@ -73,3 +68,38 @@ func (s *GormStorage) GetParentVersion(v *models.Version) (*models.Version, erro
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -7,9 +7,10 @@ import (
|
||||
|
||||
type IVersionStorage interface {
|
||||
// Document
|
||||
CreateDocument(name string) (uuid.UUID, error)
|
||||
CreateDocument(doc *models.Document) error
|
||||
GetDocument(id uuid.UUID) (*models.Document, error)
|
||||
UpdateLatestVersion(docID, versionID uuid.UUID) error
|
||||
CountSinceLastSnapshot(docID uuid.UUID) (int, error)
|
||||
|
||||
// Version
|
||||
CreateVersion(v *models.Version) error
|
||||
|
||||
Reference in New Issue
Block a user