migrate from github
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package document_version_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DocumentVersionCreateOperation struct {
|
||||
repo *DocumentVersionRepository
|
||||
docVersion *model.DocumentVersion
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (repo *DocumentVersionRepository) Create(docVersion *model.DocumentVersion) *DocumentVersionCreateOperation {
|
||||
return &DocumentVersionCreateOperation{
|
||||
repo: repo,
|
||||
docVersion: docVersion,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *DocumentVersionCreateOperation) WithTx(tx *gorm.DB) *DocumentVersionCreateOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *DocumentVersionCreateOperation) Exec() error {
|
||||
if op.tx == nil {
|
||||
op.tx = op.repo.db
|
||||
}
|
||||
|
||||
var maxVersion int
|
||||
err := op.tx.Model(&model.DocumentVersion{}).
|
||||
Where("document_id = ?", op.docVersion.DocumentID).
|
||||
Select("COALESCE(MAX(version), 0)").
|
||||
Scan(&maxVersion).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
op.docVersion.Version = maxVersion + 1
|
||||
|
||||
return op.tx.Create(op.docVersion).Error
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package document_version_repo
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type DocumentVersionRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewDocumentVersionRepository(db *gorm.DB) *DocumentVersionRepository {
|
||||
return &DocumentVersionRepository{db: db}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package document_version_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
)
|
||||
|
||||
func (r *DocumentVersionRepository) GetByDocumentIDAndVersion(documentID int64, version int) (*model.DocumentVersion, error) {
|
||||
item := &model.DocumentVersion{}
|
||||
if err := r.db.Where("document_id = ? AND version = ?", documentID, version).First(item).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package document_version_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
)
|
||||
|
||||
func (r *DocumentVersionRepository) ListByDocumentID(documentID int64, offset, limit int) ([]*model.DocumentVersion, int64, error) {
|
||||
var total int64
|
||||
query := r.db.Model(&model.DocumentVersion{}).Where("document_id = ?", documentID)
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
items := make([]*model.DocumentVersion, 0)
|
||||
if err := query.Order("version DESC").Offset(offset).Limit(limit).Find(&items).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
Reference in New Issue
Block a user