64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"next-terminal/server/model"
|
|
)
|
|
|
|
var SnippetRepository = new(snippetRepository)
|
|
|
|
type snippetRepository struct {
|
|
baseRepository
|
|
}
|
|
|
|
func (r snippetRepository) Create(ctx context.Context, m *model.Snippet) error {
|
|
return r.GetDB(ctx).Create(m).Error
|
|
}
|
|
|
|
func (r snippetRepository) UpdateById(ctx context.Context, m *model.Snippet, id string) error {
|
|
return r.GetDB(ctx).Model(&model.Snippet{}).Where("id = ?", id).Updates(m).Error
|
|
}
|
|
|
|
func (r snippetRepository) DeleteById(ctx context.Context, id string) error {
|
|
return r.GetDB(ctx).Where("id = ?", id).Delete(&model.Snippet{}).Error
|
|
}
|
|
|
|
func (r snippetRepository) FindById(ctx context.Context, id string) (m model.Snippet, err error) {
|
|
err = r.GetDB(ctx).Where("id = ?", id).First(&m).Error
|
|
return
|
|
}
|
|
|
|
func (r snippetRepository) FindAll(ctx context.Context) (items []model.Snippet, err error) {
|
|
err = r.GetDB(ctx).Order("created_at desc").Find(&items).Error
|
|
return
|
|
}
|
|
|
|
func (r snippetRepository) FindPage(ctx context.Context, pageIndex, pageSize int, name string) (items []model.Snippet, total int64, err error) {
|
|
db := r.GetDB(ctx).Model(&model.Snippet{})
|
|
if name != "" {
|
|
db = db.Where("name like ?", "%"+name+"%")
|
|
}
|
|
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = db.Order("created_at desc").
|
|
Offset((pageIndex - 1) * pageSize).
|
|
Limit(pageSize).
|
|
Find(&items).Error
|
|
|
|
return
|
|
}
|
|
|
|
func (r snippetRepository) FindByVisibility(ctx context.Context, visibility string) (items []model.Snippet, err error) {
|
|
err = r.GetDB(ctx).Where("visibility = ?", visibility).Order("created_at desc").Find(&items).Error
|
|
return
|
|
}
|
|
|
|
func (r snippetRepository) FindByUserId(ctx context.Context, userId string) (items []model.Snippet, err error) {
|
|
err = r.GetDB(ctx).Where("created_by = ? OR visibility = 'public'", userId).Order("created_at desc").Find(&items).Error
|
|
return
|
|
}
|