2025-06-19 10:35:26 +08:00

105 lines
2.5 KiB
Go

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 `Help` business logic.
type Help struct {
Trans *util.Trans
HelpDAL *dal.Help
}
// Query helps from the data access object based on the provided parameters and options.
func (a *Help) Query(ctx context.Context, params schema.HelpQueryParam) (*schema.HelpQueryResult, error) {
params.Pagination = true
result, err := a.HelpDAL.Query(ctx, params, schema.HelpQueryOptions{
QueryOptions: util.QueryOptions{
OrderFields: []util.OrderByParam{
{Field: "created_at", Direction: util.DESC},
},
},
})
if err != nil {
return nil, err
}
return result, nil
}
// Get the specified help from the data access object.
func (a *Help) Get(ctx context.Context, id string) (*schema.Help, error) {
help, err := a.HelpDAL.Get(ctx, id)
if err != nil {
return nil, err
} else if help == nil {
return nil, errors.NotFound("", "Help not found")
}
return help, nil
}
// Create a new help in the data access object.
func (a *Help) Create(ctx context.Context, formItem *schema.HelpForm) (*schema.Help, error) {
help := &schema.Help{}
if err := formItem.FillTo(help); err != nil {
return nil, err
}
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.HelpDAL.Create(ctx, help); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return help, nil
}
// Update the specified help in the data access object.
func (a *Help) Update(ctx context.Context, id string, formItem *schema.HelpForm) error {
help, err := a.HelpDAL.Get(ctx, id)
if err != nil {
return err
} else if help == nil {
return errors.NotFound("", "Help not found")
}
if err := formItem.FillTo(help); err != nil {
return err
}
help.UpdatedAt = time.Now()
return a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.HelpDAL.Update(ctx, help); err != nil {
return err
}
return nil
})
}
// Delete the specified help from the data access object.
func (a *Help) Delete(ctx context.Context, id string) error {
exists, err := a.HelpDAL.Exists(ctx, id)
if err != nil {
return err
} else if !exists {
return errors.NotFound("", "Help not found")
}
return a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.HelpDAL.Delete(ctx, id); err != nil {
return err
}
return nil
})
}