migrate from github
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package comment_repo
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type CommentRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewCommentRepository(db *gorm.DB) *CommentRepository {
|
||||
return &CommentRepository{db: db}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package comment_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentCreateOperation struct {
|
||||
repo *CommentRepository
|
||||
item *model.DocumentComment
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *CommentRepository) Create(item *model.DocumentComment) *CommentCreateOperation {
|
||||
return &CommentCreateOperation{
|
||||
repo: r,
|
||||
item: item,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *CommentCreateOperation) WithTx(tx *gorm.DB) *CommentCreateOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *CommentCreateOperation) Exec() error {
|
||||
if op.item == nil {
|
||||
return nil
|
||||
}
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
return db.Create(op.item).Error
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package comment_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentDeleteOperation struct {
|
||||
repo *CommentRepository
|
||||
id int64
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *CommentRepository) Delete(id int64) *CommentDeleteOperation {
|
||||
return &CommentDeleteOperation{
|
||||
repo: r,
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *CommentDeleteOperation) WithTx(tx *gorm.DB) *CommentDeleteOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *CommentDeleteOperation) Exec() error {
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
return db.Delete(&model.DocumentComment{}, op.id).Error
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package comment_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentGetByExternalIDOperation struct {
|
||||
repo *CommentRepository
|
||||
externalID string
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *CommentRepository) GetByExternalID(externalID string) *CommentGetByExternalIDOperation {
|
||||
return &CommentGetByExternalIDOperation{
|
||||
repo: r,
|
||||
externalID: externalID,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *CommentGetByExternalIDOperation) WithTx(tx *gorm.DB) *CommentGetByExternalIDOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *CommentGetByExternalIDOperation) Exec() (*model.DocumentComment, error) {
|
||||
var item model.DocumentComment
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
if err := db.First(&item, "external_id = ?", op.externalID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package comment_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentListByDocumentOperation struct {
|
||||
repo *CommentRepository
|
||||
documentID int64
|
||||
page int
|
||||
pageSize int
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *CommentRepository) ListByDocument(documentID int64) *CommentListByDocumentOperation {
|
||||
return &CommentListByDocumentOperation{
|
||||
repo: r,
|
||||
documentID: documentID,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *CommentListByDocumentOperation) WithTx(tx *gorm.DB) *CommentListByDocumentOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *CommentListByDocumentOperation) WithPagination(page, pageSize int) *CommentListByDocumentOperation {
|
||||
op.page = page
|
||||
op.pageSize = pageSize
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *CommentListByDocumentOperation) ExecWithTotal() ([]model.DocumentComment, int64, error) {
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
|
||||
query := db.Model(&model.DocumentComment{}).
|
||||
Where("document_id = ?", op.documentID).
|
||||
Where("anchor_id IS NOT NULL AND anchor_id <> ''")
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
page := op.page
|
||||
pageSize := op.pageSize
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
if pageSize > 100 {
|
||||
pageSize = 100
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
var items []model.DocumentComment
|
||||
if err := query.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&items).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package comment_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentUpdateContentOperation struct {
|
||||
repo *CommentRepository
|
||||
id int64
|
||||
content string
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *CommentRepository) UpdateContentByID(id int64, content string) *CommentUpdateContentOperation {
|
||||
return &CommentUpdateContentOperation{
|
||||
repo: r,
|
||||
id: id,
|
||||
content: content,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *CommentUpdateContentOperation) WithTx(tx *gorm.DB) *CommentUpdateContentOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *CommentUpdateContentOperation) Exec() error {
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
return db.Model(&model.DocumentComment{}).Where("id = ?", op.id).Update("content", op.content).Error
|
||||
}
|
||||
Reference in New Issue
Block a user