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

84 lines
2.9 KiB
Go

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