105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.guxuan/haibei/internal/mods/activity/dal"
|
|
"github.guxuan/haibei/internal/mods/activity/schema"
|
|
"github.guxuan/haibei/pkg/errors"
|
|
"github.guxuan/haibei/pkg/util"
|
|
)
|
|
|
|
// Defining the `Questionnaire` business logic.
|
|
type Questionnaire struct {
|
|
Trans *util.Trans
|
|
QuestionnaireDAL *dal.Questionnaire
|
|
}
|
|
|
|
// Query questionnaires from the data access object based on the provided parameters and options.
|
|
func (a *Questionnaire) Query(ctx context.Context, params schema.QuestionnaireQueryParam) (*schema.QuestionnaireQueryResult, error) {
|
|
params.Pagination = true
|
|
|
|
result, err := a.QuestionnaireDAL.Query(ctx, params, schema.QuestionnaireQueryOptions{
|
|
QueryOptions: util.QueryOptions{
|
|
OrderFields: []util.OrderByParam{
|
|
{Field: "created_at", Direction: util.DESC},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Get the specified questionnaire from the data access object.
|
|
func (a *Questionnaire) Get(ctx context.Context, id uint) (*schema.Questionnaire, error) {
|
|
questionnaire, err := a.QuestionnaireDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if questionnaire == nil {
|
|
return nil, errors.NotFound("", "Questionnaire not found")
|
|
}
|
|
return questionnaire, nil
|
|
}
|
|
|
|
// Create a new questionnaire in the data access object.
|
|
func (a *Questionnaire) Create(ctx context.Context, formItem *schema.QuestionnaireForm) (*schema.Questionnaire, error) {
|
|
questionnaire := &schema.Questionnaire{}
|
|
|
|
if err := formItem.FillTo(questionnaire); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.QuestionnaireDAL.Create(ctx, questionnaire); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return questionnaire, nil
|
|
}
|
|
|
|
// Update the specified questionnaire in the data access object.
|
|
func (a *Questionnaire) Update(ctx context.Context, id uint, formItem *schema.QuestionnaireForm) error {
|
|
questionnaire, err := a.QuestionnaireDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if questionnaire == nil {
|
|
return errors.NotFound("", "Questionnaire not found")
|
|
}
|
|
|
|
if err := formItem.FillTo(questionnaire); err != nil {
|
|
return err
|
|
}
|
|
questionnaire.UpdatedAt = time.Now()
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.QuestionnaireDAL.Update(ctx, questionnaire); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Delete the specified questionnaire from the data access object.
|
|
func (a *Questionnaire) Delete(ctx context.Context, id uint) error {
|
|
exists, err := a.QuestionnaireDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Questionnaire not found")
|
|
}
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.QuestionnaireDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|