package biz import ( "context" "time" "gitlab.guxuan.icu/jinshan_community/internal/mods/common/dal" "gitlab.guxuan.icu/jinshan_community/internal/mods/common/schema" "gitlab.guxuan.icu/jinshan_community/pkg/errors" "gitlab.guxuan.icu/jinshan_community/pkg/util" ) // Defining the `SurroundingService` business logic. type SurroundingService struct { Trans *util.Trans SurroundingServiceDAL *dal.SurroundingService } // Query surrounding services from the data access object based on the provided parameters and options. func (a *SurroundingService) Query(ctx context.Context, params schema.SurroundingServiceQueryParam) (*schema.SurroundingServiceQueryResult, error) { params.Pagination = true result, err := a.SurroundingServiceDAL.Query(ctx, params, schema.SurroundingServiceQueryOptions{ QueryOptions: util.QueryOptions{ OrderFields: []util.OrderByParam{ {Field: "created_at", Direction: util.DESC}, }, }, }) if err != nil { return nil, err } return result, nil } // Get the specified surrounding service from the data access object. func (a *SurroundingService) Get(ctx context.Context, id string) (*schema.SurroundingService, error) { surroundingService, err := a.SurroundingServiceDAL.Get(ctx, id) if err != nil { return nil, err } else if surroundingService == nil { return nil, errors.NotFound("", "Surrounding service not found") } return surroundingService, nil } // Create a new surrounding service in the data access object. func (a *SurroundingService) Create(ctx context.Context, formItem *schema.SurroundingServiceForm) (*schema.SurroundingService, error) { surroundingService := &schema.SurroundingService{} if err := formItem.FillTo(surroundingService); err != nil { return nil, err } err := a.Trans.Exec(ctx, func(ctx context.Context) error { if err := a.SurroundingServiceDAL.Create(ctx, surroundingService); err != nil { return err } return nil }) if err != nil { return nil, err } return surroundingService, nil } // Update the specified surrounding service in the data access object. func (a *SurroundingService) Update(ctx context.Context, id string, formItem *schema.SurroundingServiceForm) error { surroundingService, err := a.SurroundingServiceDAL.Get(ctx, id) if err != nil { return err } else if surroundingService == nil { return errors.NotFound("", "Surrounding service not found") } if err := formItem.FillTo(surroundingService); err != nil { return err } surroundingService.UpdatedAt = time.Now() return a.Trans.Exec(ctx, func(ctx context.Context) error { if err := a.SurroundingServiceDAL.Update(ctx, surroundingService); err != nil { return err } return nil }) } // Delete the specified surrounding service from the data access object. func (a *SurroundingService) Delete(ctx context.Context, id string) error { exists, err := a.SurroundingServiceDAL.Exists(ctx, id) if err != nil { return err } else if !exists { return errors.NotFound("", "Surrounding service not found") } return a.Trans.Exec(ctx, func(ctx context.Context) error { if err := a.SurroundingServiceDAL.Delete(ctx, id); err != nil { return err } return nil }) }