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