hailin/internal/mods/rbac/dal/video.dal.go
2025-06-19 10:30:46 +08:00

89 lines
2.5 KiB
Go

package dal
import (
"context"
"github.com/guxuan/hailin_service/internal/mods/rbac/schema"
"github.com/guxuan/hailin_service/pkg/errors"
"github.com/guxuan/hailin_service/pkg/util"
"gorm.io/gorm"
)
func GetVideoDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.Video))
}
type Video struct {
DB *gorm.DB
}
// Query roles from the database based on the provided parameters and options.
func (a *Video) Query(ctx context.Context, params schema.VideoQueryParam, opts ...schema.VideoQueryOptions) (*schema.VideoQueryResult, error) {
var opt schema.VideoQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
db := GetVideoDB(ctx, a.DB)
if v := params.LikeTitle; len(v) > 0 {
db = db.Where("title LIKE ?", "%"+v+"%")
}
if v := params.Status; len(v) > 0 {
db = db.Where("status = ?", v)
}
var list schema.Videos
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
if err != nil {
return nil, errors.WithStack(err)
}
queryResult := &schema.VideoQueryResult{
PageResult: pageResult,
Data: list,
}
return queryResult, nil
}
// Get the specified role from the database.
func (a *Video) Get(ctx context.Context, id string, opts ...schema.VideoQueryOptions) (*schema.Video, error) {
var opt schema.VideoQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
item := new(schema.Video)
ok, err := util.FindOne(ctx, GetVideoDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
if err != nil {
return nil, errors.WithStack(err)
} else if !ok {
return nil, nil
}
return item, nil
}
// Exist checks if the specified role exists in the database.
func (a *Video) Exists(ctx context.Context, id string) (bool, error) {
ok, err := util.Exists(ctx, GetVideoDB(ctx, a.DB).Where("id=?", id))
return ok, errors.WithStack(err)
}
// Create a new role.
func (a *Video) Create(ctx context.Context, item *schema.Video) error {
result := GetVideoDB(ctx, a.DB).Create(item)
return errors.WithStack(result.Error)
}
// Update the specified role in the database.
func (a *Video) Update(ctx context.Context, item *schema.Video) error {
result := GetVideoDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
return errors.WithStack(result.Error)
}
// Delete the specified role from the database.
func (a *Video) Delete(ctx context.Context, id string) error {
result := GetVideoDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.Video))
return errors.WithStack(result.Error)
}