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