migrate from github
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package template_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TemplateCreateOperation struct {
|
||||
repo *TemplateRepository
|
||||
template *model.Template
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *TemplateRepository) Create(template *model.Template) *TemplateCreateOperation {
|
||||
return &TemplateCreateOperation{
|
||||
repo: r,
|
||||
template: template,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *TemplateCreateOperation) WithTx(tx *gorm.DB) *TemplateCreateOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateCreateOperation) Exec() error {
|
||||
if op.tx != nil {
|
||||
return op.tx.Create(op.template).Error
|
||||
}
|
||||
return op.repo.db.Create(op.template).Error
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package template_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type CreateRecentTemplateOperation struct {
|
||||
repo *TemplateRepository
|
||||
m *model.RecentTemplate
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *TemplateRepository) CreateRecentTemplate(m *model.RecentTemplate) *CreateRecentTemplateOperation {
|
||||
return &CreateRecentTemplateOperation{
|
||||
repo: r,
|
||||
m: m,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *CreateRecentTemplateOperation) WithTx(tx *gorm.DB) {
|
||||
op.tx = tx
|
||||
}
|
||||
|
||||
func (op *CreateRecentTemplateOperation) Exec() error {
|
||||
if op.tx == nil {
|
||||
op.tx = op.repo.db
|
||||
}
|
||||
|
||||
return op.tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{
|
||||
{Name: "user_id"},
|
||||
{Name: "template_id"},
|
||||
},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"accessed_at"}),
|
||||
}).Create(op.m).Error
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package template_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TemplateGetByIDOperation struct {
|
||||
repo *TemplateRepository
|
||||
id int64
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *TemplateRepository) GetByID(id int64) (op *TemplateGetByIDOperation) {
|
||||
return &TemplateGetByIDOperation{
|
||||
repo: r,
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *TemplateGetByIDOperation) WithTx(tx *gorm.DB) *TemplateGetByIDOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateGetByIDOperation) Exec() (template *model.Template, err error) {
|
||||
if op.tx == nil {
|
||||
op.tx = op.repo.db
|
||||
}
|
||||
template = &model.Template{}
|
||||
err = op.tx.First(template, "id = ?", op.id).Error
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package template_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ListTemplateOperation struct {
|
||||
repo *TemplateRepository
|
||||
model *model.Template
|
||||
userID *int64
|
||||
scope *string
|
||||
knowledgeBaseID *int64
|
||||
nameKeyword *string
|
||||
documentType *model.DocumentType
|
||||
sortField string
|
||||
sortDesc bool
|
||||
page int
|
||||
pageSize int
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *TemplateRepository) List(m *model.Template) (op *ListTemplateOperation) {
|
||||
return &ListTemplateOperation{
|
||||
repo: r,
|
||||
model: m,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithTx(tx *gorm.DB) *ListTemplateOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithUserID(userID *int64) *ListTemplateOperation {
|
||||
op.userID = userID
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithScope(scope *string) *ListTemplateOperation {
|
||||
op.scope = scope
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithKnowledgeBaseID(knowledgeBaseID *int64) *ListTemplateOperation {
|
||||
op.knowledgeBaseID = knowledgeBaseID
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithNameKeyword(nameKeyword *string) *ListTemplateOperation {
|
||||
op.nameKeyword = nameKeyword
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithDocumentType(documentType *model.DocumentType) *ListTemplateOperation {
|
||||
op.documentType = documentType
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithSort(field string, desc bool) *ListTemplateOperation {
|
||||
op.sortField = field
|
||||
op.sortDesc = desc
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) WithPagination(page, pageSize int) *ListTemplateOperation {
|
||||
op.page = page
|
||||
op.pageSize = pageSize
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListTemplateOperation) ExecWithTotal() (templates []*model.Template, total int64, err error) {
|
||||
if op.tx == nil {
|
||||
op.tx = op.repo.db
|
||||
}
|
||||
|
||||
dbQuery := op.tx.Model(op.model)
|
||||
if op.model != nil {
|
||||
dbQuery = dbQuery.Where(op.model)
|
||||
}
|
||||
|
||||
if op.userID != nil {
|
||||
dbQuery = dbQuery.Where("user_id = ?", *op.userID)
|
||||
}
|
||||
if op.scope != nil {
|
||||
dbQuery = dbQuery.Where("scope = ?", *op.scope)
|
||||
}
|
||||
if op.knowledgeBaseID != nil {
|
||||
dbQuery = dbQuery.Where("knowledge_base_id = ?", *op.knowledgeBaseID)
|
||||
}
|
||||
if op.nameKeyword != nil && *op.nameKeyword != "" {
|
||||
dbQuery = dbQuery.Where("name ILIKE ?", "%"+*op.nameKeyword+"%")
|
||||
}
|
||||
if op.documentType != nil && *op.documentType != "" {
|
||||
dbQuery = dbQuery.Where("document_type = ?", *op.documentType)
|
||||
}
|
||||
|
||||
if err = dbQuery.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
orderStr := "created_at DESC"
|
||||
if op.sortField != "" {
|
||||
if op.sortDesc {
|
||||
orderStr = op.sortField + " DESC"
|
||||
} else {
|
||||
orderStr = op.sortField + " ASC"
|
||||
}
|
||||
}
|
||||
dbQuery = dbQuery.Order(orderStr)
|
||||
|
||||
if op.page > 0 && op.pageSize > 0 {
|
||||
offset := (op.page - 1) * op.pageSize
|
||||
dbQuery = dbQuery.Offset(offset).Limit(op.pageSize)
|
||||
}
|
||||
|
||||
err = dbQuery.Find(&templates).Error
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package template_repo
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ListRecentTemplatesOperation struct {
|
||||
repo *TemplateRepository
|
||||
userID int64
|
||||
startTime *time.Time
|
||||
endTime *time.Time
|
||||
page int
|
||||
pageSize int
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *TemplateRepository) ListRecentTemplates(userID int64) *ListRecentTemplatesOperation {
|
||||
return &ListRecentTemplatesOperation{
|
||||
repo: r,
|
||||
userID: userID,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *ListRecentTemplatesOperation) WithTimeRange(start, end *time.Time) *ListRecentTemplatesOperation {
|
||||
op.startTime = start
|
||||
op.endTime = end
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListRecentTemplatesOperation) WithPagination(page, pageSize int) *ListRecentTemplatesOperation {
|
||||
op.page = page
|
||||
op.pageSize = pageSize
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListRecentTemplatesOperation) WithTx(tx *gorm.DB) *ListRecentTemplatesOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *ListRecentTemplatesOperation) Exec() ([]*model.RecentTemplate, int64, error) {
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
|
||||
query := db.Model(&model.RecentTemplate{}).
|
||||
Where("user_id = ?", op.userID)
|
||||
if op.startTime != nil {
|
||||
query = query.Where("accessed_at >= ?", *op.startTime)
|
||||
}
|
||||
if op.endTime != nil {
|
||||
query = query.Where("accessed_at <= ?", *op.endTime)
|
||||
}
|
||||
|
||||
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 total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var records []*model.RecentTemplate
|
||||
if err := query.Order("accessed_at DESC").Offset(offset).Limit(pageSize).Find(&records).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return records, total, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package template_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TemplateMGetByIDsOperation struct {
|
||||
repo *TemplateRepository
|
||||
ids []int64
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *TemplateRepository) MGetByIDs(ids []int64) *TemplateMGetByIDsOperation {
|
||||
return &TemplateMGetByIDsOperation{
|
||||
repo: r,
|
||||
ids: ids,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *TemplateMGetByIDsOperation) WithTx(tx *gorm.DB) *TemplateMGetByIDsOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateMGetByIDsOperation) Exec() ([]*model.Template, error) {
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
var templates []*model.Template
|
||||
if len(op.ids) == 0 {
|
||||
return templates, nil
|
||||
}
|
||||
if err := db.Where("id IN ?", op.ids).Find(&templates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return templates, nil
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package template_repo
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type TemplateRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewTemplateRepository(db *gorm.DB) *TemplateRepository {
|
||||
return &TemplateRepository{db: db}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package template_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TemplateUpdateOperation struct {
|
||||
repo *TemplateRepository
|
||||
template *model.Template
|
||||
updateFields map[string]bool
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *TemplateRepository) Update(template *model.Template) *TemplateUpdateOperation {
|
||||
return &TemplateUpdateOperation{
|
||||
repo: r,
|
||||
template: template,
|
||||
updateFields: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (op *TemplateUpdateOperation) UpdateName() *TemplateUpdateOperation {
|
||||
op.updateFields["name"] = true
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateUpdateOperation) UpdateDescription() *TemplateUpdateOperation {
|
||||
op.updateFields["description"] = true
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateUpdateOperation) UpdateScope() *TemplateUpdateOperation {
|
||||
op.updateFields["scope"] = true
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateUpdateOperation) UpdateIsPublic() *TemplateUpdateOperation {
|
||||
op.updateFields["is_public"] = true
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateUpdateOperation) WithTx(tx *gorm.DB) *TemplateUpdateOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *TemplateUpdateOperation) Exec() error {
|
||||
if op.tx == nil {
|
||||
op.tx = op.repo.db
|
||||
}
|
||||
|
||||
query := op.tx.Model(op.template)
|
||||
if len(op.updateFields) > 0 {
|
||||
fields := make([]string, 0, len(op.updateFields))
|
||||
for field := range op.updateFields {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
query = query.Select(fields)
|
||||
}
|
||||
|
||||
return query.Updates(*op.template).Error
|
||||
}
|
||||
Reference in New Issue
Block a user