You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package column
|
|
|
|
import (
|
|
"cls/internal/domain/column"
|
|
"cls/pkg/util/page"
|
|
"cls/pkg/xorm_engine"
|
|
"errors"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// ColumnRepository 专栏仓储实现
|
|
type ColumnRepositoryORM struct {
|
|
engine *xorm_engine.Engine
|
|
}
|
|
|
|
// NewColumnRepository 创建专栏仓储
|
|
func NewColumnRepositoryORM(engine *xorm_engine.Engine) column.ColumnRepository {
|
|
return &ColumnRepositoryORM{engine}
|
|
}
|
|
|
|
var _ column.ColumnRepository = (*ColumnRepositoryORM)(nil)
|
|
|
|
// Save 保存专栏
|
|
func (r *ColumnRepositoryORM) Save(col *column.Column) error {
|
|
_, err := r.engine.Insert(col)
|
|
return err
|
|
}
|
|
|
|
// FindByID 根据ID查找专栏
|
|
func (r *ColumnRepositoryORM) FindByID(id uint64) (*column.Column, error) {
|
|
var col column.Column
|
|
exists, err := r.engine.ID(id).Get(&col)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exists {
|
|
return nil, nil
|
|
}
|
|
return &col, nil
|
|
}
|
|
|
|
// FindByAuthorID 查找作者的所有专栏
|
|
func (r *ColumnRepositoryORM) FindByAuthorID(authorID uint64) ([]*column.Column, error) {
|
|
var cols []*column.Column
|
|
err := r.engine.Where("author_id = ?", authorID).
|
|
Desc("created_at").
|
|
Find(&cols)
|
|
return cols, err
|
|
}
|
|
|
|
// FindAll 查询专栏列表
|
|
func (r *ColumnRepositoryORM) FindAll(page *page.Page, conds []builder.Cond) error {
|
|
return r.engine.FindAll(page, &column.Column{}, builder.And(conds...))
|
|
}
|
|
|
|
// Update 更新专栏
|
|
func (r *ColumnRepositoryORM) Update(col *column.Column) error {
|
|
_, err := r.engine.ID(col.ID).Update(col)
|
|
return err
|
|
}
|
|
|
|
// Delete 删除专栏
|
|
func (r *ColumnRepositoryORM) Delete(id uint64) error {
|
|
_, err := r.engine.ID(id).Delete(&column.Column{})
|
|
return err
|
|
}
|
|
func (r *ColumnRepositoryORM) FindByName(name string) (*column.Column, error) {
|
|
var col column.Column
|
|
exists, err := r.engine.Where(builder.Eq{"title": name}).Get(&col)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exists {
|
|
return nil, errors.New("未找到专栏")
|
|
}
|
|
return &col, nil
|
|
}
|