alpha
This commit is contained in:
75
store/gorm.go
Normal file
75
store/gorm.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"git.gm6.ru/icewind/seadoc/models"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormStorage реализует интерфейс VersionStorage через GORM (SQLite)
|
||||
type GormStorage struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
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) GetDocument(id uuid.UUID) (*models.Document, error) {
|
||||
var d models.Document
|
||||
if err := s.db.First(&d, "id = ?", id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func (s *GormStorage) UpdateLatestVersion(docID, versionID uuid.UUID) error {
|
||||
return s.db.Model(&models.Document{}).
|
||||
Where("id = ?", docID).
|
||||
Update("latest_version", versionID).Error
|
||||
}
|
||||
|
||||
func (s *GormStorage) CreateVersion(v *models.Version) error {
|
||||
return s.db.Create(v).Error
|
||||
}
|
||||
|
||||
func (s *GormStorage) GetLatestVersion(docID uuid.UUID) (*models.Version, error) {
|
||||
var v models.Version
|
||||
if err := s.db.
|
||||
Where("document_id = ?", docID).
|
||||
Order("created_at desc").
|
||||
First(&v).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func (s *GormStorage) GetVersionByID(id uuid.UUID) (*models.Version, error) {
|
||||
var v models.Version
|
||||
if err := s.db.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.db.First(&parent, "id = ?", v.ParentID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &parent, nil
|
||||
}
|
||||
19
store/store.go
Normal file
19
store/store.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"git.gm6.ru/icewind/seadoc/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type IVersionStorage interface {
|
||||
// Document
|
||||
CreateDocument(name string) (uuid.UUID, error)
|
||||
GetDocument(id uuid.UUID) (*models.Document, error)
|
||||
UpdateLatestVersion(docID, versionID uuid.UUID) error
|
||||
|
||||
// Version
|
||||
CreateVersion(v *models.Version) error
|
||||
GetLatestVersion(docID uuid.UUID) (*models.Version, error)
|
||||
GetVersionByID(id uuid.UUID) (*models.Version, error)
|
||||
GetParentVersion(v *models.Version) (*models.Version, error)
|
||||
}
|
||||
Reference in New Issue
Block a user