FrontInterfaceCenter/internal/data/repositoryimpl/activity.go

44 lines
1.1 KiB
Go

package repositoryimpl
import (
"center-api/internal/biz/do"
"center-api/internal/biz/repository"
"center-api/internal/data"
"center-api/internal/data/ent"
"center-api/internal/data/ent/activity"
"context"
)
type ActivityRepoImpl struct {
Base[ent.Activity, do.ActivityDo, ent.ActivityQuery]
db *data.Db
}
func NewActivityRepoImpl(db *data.Db) repository.ActivityRepo {
return &ActivityRepoImpl{db: db}
}
// ToEntity 转换成实体
func (p *ActivityRepoImpl) ToEntity(pp *ent.Activity) *do.ActivityDo {
return p.Base.ToEntity(pp)
}
// ToEntities 转换成实体
// 支持基本类型的值对象
func (p *ActivityRepoImpl) ToEntities(ps []*ent.Activity) []*do.ActivityDo {
if ps == nil {
return nil
}
// 使用循环,以免单个转换有特殊处理,要修改两个地方
entities := make([]*do.ActivityDo, len(ps))
for k, pp := range ps {
entities[k] = p.ToEntity(pp)
}
return entities
}
func (p *ActivityRepoImpl) GetById(ctx context.Context, id int) *do.ActivityDo {
row := p.db.GetDb(ctx).Activity.Query().Where(activity.ID(id)).FirstX(ctx)
return p.ToEntity(row)
}