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