jinshan/internal/mods/activity/dal/reciprocity.dal.go
2025-06-19 10:35:26 +08:00

84 lines
2.6 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 reciprocity storage instance
func GetReciprocityDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.Reciprocity))
}
// Defining the `Reciprocity` data access object.
type Reciprocity struct {
DB *gorm.DB
}
// Query reciprocities from the database based on the provided parameters and options.
func (a *Reciprocity) Query(ctx context.Context, params schema.ReciprocityQueryParam, opts ...schema.ReciprocityQueryOptions) (*schema.ReciprocityQueryResult, error) {
var opt schema.ReciprocityQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
db := GetReciprocityDB(ctx, a.DB)
var list schema.Reciprocities
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
if err != nil {
return nil, errors.WithStack(err)
}
queryResult := &schema.ReciprocityQueryResult{
PageResult: pageResult,
Data: list,
}
return queryResult, nil
}
// Get the specified reciprocity from the database.
func (a *Reciprocity) Get(ctx context.Context, id string, opts ...schema.ReciprocityQueryOptions) (*schema.Reciprocity, error) {
var opt schema.ReciprocityQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
item := new(schema.Reciprocity)
ok, err := util.FindOne(ctx, GetReciprocityDB(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 reciprocity exists in the database.
func (a *Reciprocity) Exists(ctx context.Context, id string) (bool, error) {
ok, err := util.Exists(ctx, GetReciprocityDB(ctx, a.DB).Where("id=?", id))
return ok, errors.WithStack(err)
}
// Create a new reciprocity.
func (a *Reciprocity) Create(ctx context.Context, item *schema.Reciprocity) error {
result := GetReciprocityDB(ctx, a.DB).Create(item)
return errors.WithStack(result.Error)
}
// Update the specified reciprocity in the database.
func (a *Reciprocity) Update(ctx context.Context, item *schema.Reciprocity) error {
result := GetReciprocityDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
return errors.WithStack(result.Error)
}
// Delete the specified reciprocity from the database.
func (a *Reciprocity) Delete(ctx context.Context, id string) error {
result := GetReciprocityDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.Reciprocity))
return errors.WithStack(result.Error)
}