migrate from github
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package doc_yjs_snapshot_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type DocumentYjsSnapshotRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewDocumentYjsSnapshotRepository(db *gorm.DB) *DocumentYjsSnapshotRepository {
|
||||
return &DocumentYjsSnapshotRepository{db: db}
|
||||
}
|
||||
|
||||
type DocumentYjsSnapshotGetOperation struct {
|
||||
repo *DocumentYjsSnapshotRepository
|
||||
docID int64
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *DocumentYjsSnapshotRepository) GetByDocID(docID int64) *DocumentYjsSnapshotGetOperation {
|
||||
return &DocumentYjsSnapshotGetOperation{
|
||||
repo: r,
|
||||
docID: docID,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *DocumentYjsSnapshotGetOperation) WithTx(tx *gorm.DB) *DocumentYjsSnapshotGetOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *DocumentYjsSnapshotGetOperation) Exec() (*model.DocumentYjsSnapshot, error) {
|
||||
if op.tx == nil {
|
||||
op.tx = op.repo.db
|
||||
}
|
||||
|
||||
var snapshot model.DocumentYjsSnapshot
|
||||
if err := op.tx.First(&snapshot, "doc_id = ?", op.docID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &snapshot, nil
|
||||
}
|
||||
|
||||
type DocumentYjsSnapshotSaveOperation struct {
|
||||
repo *DocumentYjsSnapshotRepository
|
||||
docID int64
|
||||
state []byte
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *DocumentYjsSnapshotRepository) Save(docID int64, state []byte) *DocumentYjsSnapshotSaveOperation {
|
||||
return &DocumentYjsSnapshotSaveOperation{
|
||||
repo: r,
|
||||
docID: docID,
|
||||
state: state,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *DocumentYjsSnapshotSaveOperation) WithTx(tx *gorm.DB) *DocumentYjsSnapshotSaveOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *DocumentYjsSnapshotSaveOperation) Exec() error {
|
||||
if op.tx == nil {
|
||||
op.tx = op.repo.db
|
||||
}
|
||||
|
||||
snapshot := &model.DocumentYjsSnapshot{
|
||||
DocID: op.docID,
|
||||
YjsState: op.state,
|
||||
Version: 1,
|
||||
}
|
||||
|
||||
return op.tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "doc_id"}},
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||||
"yjs_state": op.state,
|
||||
"version": gorm.Expr("documents_yjs_snapshot.version + 1"),
|
||||
"updated_at": gorm.Expr("NOW()"),
|
||||
}),
|
||||
}).Create(snapshot).Error
|
||||
}
|
||||
Reference in New Issue
Block a user