84 lines
2.8 KiB
Go
84 lines
2.8 KiB
Go
package dal
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/activity/schema"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/errors"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/util"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Get activity category storage instance
|
|
func GetActivityCategoryDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
|
|
return util.GetDB(ctx, defDB).Model(new(schema.ActivityCategory))
|
|
}
|
|
|
|
// Defining the `ActivityCategory` data access object.
|
|
type ActivityCategory struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
// Query activity categories from the database based on the provided parameters and options.
|
|
func (a *ActivityCategory) Query(ctx context.Context, params schema.ActivityCategoryQueryParam, opts ...schema.ActivityCategoryQueryOptions) (*schema.ActivityCategoryQueryResult, error) {
|
|
var opt schema.ActivityCategoryQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
db := GetActivityCategoryDB(ctx, a.DB)
|
|
|
|
var list schema.ActivityCategories
|
|
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
queryResult := &schema.ActivityCategoryQueryResult{
|
|
PageResult: pageResult,
|
|
Data: list,
|
|
}
|
|
return queryResult, nil
|
|
}
|
|
|
|
// Get the specified activity category from the database.
|
|
func (a *ActivityCategory) Get(ctx context.Context, id string, opts ...schema.ActivityCategoryQueryOptions) (*schema.ActivityCategory, error) {
|
|
var opt schema.ActivityCategoryQueryOptions
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
|
|
item := new(schema.ActivityCategory)
|
|
ok, err := util.FindOne(ctx, GetActivityCategoryDB(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
|
|
}
|
|
|
|
// Exists checks if the specified activity category exists in the database.
|
|
func (a *ActivityCategory) Exists(ctx context.Context, id string) (bool, error) {
|
|
ok, err := util.Exists(ctx, GetActivityCategoryDB(ctx, a.DB).Where("id=?", id))
|
|
return ok, errors.WithStack(err)
|
|
}
|
|
|
|
// Create a new activity category.
|
|
func (a *ActivityCategory) Create(ctx context.Context, item *schema.ActivityCategory) error {
|
|
result := GetActivityCategoryDB(ctx, a.DB).Create(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
// Update the specified activity category in the database.
|
|
func (a *ActivityCategory) Update(ctx context.Context, item *schema.ActivityCategory) error {
|
|
result := GetActivityCategoryDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
// Delete the specified activity category from the database.
|
|
func (a *ActivityCategory) Delete(ctx context.Context, id string) error {
|
|
result := GetActivityCategoryDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.ActivityCategory))
|
|
return errors.WithStack(result.Error)
|
|
}
|