87 lines
2.5 KiB
Go
87 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 GetMemorabiliaDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
|
|
return util.GetDB(ctx, defDB).Model(new(schema.Memorabilia))
|
|
}
|
|
|
|
type Memorabilia struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func (a *Memorabilia) Query(ctx context.Context, params schema.MemorabiliaQueryParam, opts ...schema.MemorabiliaQueryOptions) (*schema.MemorabiliaQueryResult, error) {
|
|
var opt schema.MemorabiliaQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
db := GetMemorabiliaDB(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)
|
|
}
|
|
if v := params.Year; v > 0 {
|
|
db = db.Where("year = ?", v)
|
|
}
|
|
if v := params.Month; v > 0 {
|
|
db = db.Where("month = ?", v)
|
|
}
|
|
var list schema.Memorabilias
|
|
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
queryResult := &schema.MemorabiliaQueryResult{
|
|
PageResult: pageResult,
|
|
Data: list,
|
|
}
|
|
return queryResult, nil
|
|
}
|
|
|
|
func (a *Memorabilia) Get(ctx context.Context, id string, opts ...schema.MemorabiliaQueryOptions) (*schema.Memorabilia, error) {
|
|
var opt schema.MemorabiliaQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
item := new(schema.Memorabilia)
|
|
ok, err := util.FindOne(ctx, GetMemorabiliaDB(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
|
|
}
|
|
|
|
func (a *Memorabilia) Exists(ctx context.Context, id string) (bool, error) {
|
|
ok, err := util.Exists(ctx, GetMemorabiliaDB(ctx, a.DB).Where("id=?", id))
|
|
return ok, errors.WithStack(err)
|
|
}
|
|
|
|
func (a *Memorabilia) Create(ctx context.Context, item *schema.Memorabilia) error {
|
|
result := GetMemorabiliaDB(ctx, a.DB).Create(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
func (a *Memorabilia) Update(ctx context.Context, item *schema.Memorabilia) error {
|
|
result := GetMemorabiliaDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
func (a *Memorabilia) Delete(ctx context.Context, id string) error {
|
|
result := GetMemorabiliaDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.Memorabilia))
|
|
return errors.WithStack(result.Error)
|
|
}
|