139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
package usecase
|
|
|
|
import (
|
|
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity"
|
|
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository"
|
|
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/usecase"
|
|
"code.30cm.net/digimon/library-go/errs"
|
|
"context"
|
|
"errors"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
|
)
|
|
|
|
type CategoryUseCaseParam struct {
|
|
CategoryRepo repository.CategoryRepository
|
|
}
|
|
|
|
type CategoryUseCase struct {
|
|
CategoryUseCaseParam
|
|
}
|
|
|
|
func MustCategoryUseCase(param CategoryUseCaseParam) usecase.CategoryUseCase {
|
|
return &CategoryUseCase{
|
|
param,
|
|
}
|
|
}
|
|
|
|
func (use *CategoryUseCase) Insert(ctx context.Context, data *entity.Category) error {
|
|
err := use.CategoryRepo.Insert(ctx, &entity.Category{
|
|
Name: data.Name,
|
|
})
|
|
if err != nil {
|
|
return errs.DBErrorL(
|
|
logx.WithContext(ctx),
|
|
[]logx.LogField{
|
|
{Key: "req", Value: data},
|
|
{Key: "func", Value: "CategoryRepo.Insert"},
|
|
{Key: "err", Value: err.Error()},
|
|
},
|
|
"failed to create category").Wrap(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (use *CategoryUseCase) FindOneByID(ctx context.Context, id string) (*entity.Category, error) {
|
|
res, err := use.CategoryRepo.FindOneByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, mon.ErrNotFound) {
|
|
e := errs.ResourceNotFound(
|
|
"failed to get category",
|
|
)
|
|
|
|
return nil, e
|
|
}
|
|
|
|
e := errs.DBErrorL(
|
|
logx.WithContext(ctx),
|
|
[]logx.LogField{
|
|
{Key: "req", Value: id},
|
|
{Key: "func", Value: "CategoryRepo.FindOneByID"},
|
|
{Key: "err", Value: err.Error()},
|
|
},
|
|
"failed to get category").Wrap(err)
|
|
|
|
return nil, e
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func (use *CategoryUseCase) Update(ctx context.Context, id string, data *entity.Category) error {
|
|
_, err := use.CategoryRepo.Update(ctx, id, &entity.Category{
|
|
Name: data.Name,
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, mon.ErrNotFound) {
|
|
e := errs.ResourceNotFound(
|
|
"failed to update category",
|
|
)
|
|
|
|
return e
|
|
}
|
|
|
|
e := errs.DBErrorL(
|
|
logx.WithContext(ctx),
|
|
[]logx.LogField{
|
|
{Key: "req", Value: id},
|
|
{Key: "func", Value: "CategoryRepo.Update"},
|
|
{Key: "err", Value: err.Error()},
|
|
},
|
|
"failed to update category").Wrap(err)
|
|
|
|
return e
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (use *CategoryUseCase) Delete(ctx context.Context, id string) error {
|
|
_, err := use.CategoryRepo.Delete(ctx, id)
|
|
if err != nil {
|
|
e := errs.DBErrorL(
|
|
logx.WithContext(ctx),
|
|
[]logx.LogField{
|
|
{Key: "req", Value: id},
|
|
{Key: "func", Value: "CategoryRepo.Delete"},
|
|
{Key: "err", Value: err.Error()},
|
|
},
|
|
"failed to delete category").Wrap(err)
|
|
|
|
return e
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (use *CategoryUseCase) ListCategory(ctx context.Context, params usecase.CategoryQueryParams) ([]*entity.Category, int64, error) {
|
|
category, i, err := use.CategoryRepo.ListCategory(ctx, &repository.CategoryQueryParams{
|
|
ID: params.ID,
|
|
PageIndex: params.PageIndex,
|
|
PageSize: params.PageSize,
|
|
})
|
|
if err != nil {
|
|
e := errs.DBErrorL(
|
|
logx.WithContext(ctx),
|
|
[]logx.LogField{
|
|
{Key: "req", Value: params},
|
|
{Key: "func", Value: "CategoryRepo.ListCategory"},
|
|
{Key: "err", Value: err.Error()},
|
|
},
|
|
"failed to list category").Wrap(err)
|
|
|
|
return nil, 0, e
|
|
}
|
|
|
|
return category, i, nil
|
|
}
|