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