1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package service
- import (
- "fmt"
- "github.com/pkg/errors"
- "gorm.io/gorm"
- "net/http"
- "sghgogs.com/micro/common"
- "sghgogs.com/micro/common/errorcode"
- req "sghgogs.com/micro/shopping-service/domain/model/request"
- pb "sghgogs.com/micro/shopping-service/proto"
- "sghgogs.com/micro/shopping-service/utils"
- "time"
- )
- func (svc *Service) CreateMultipleCategories(category *pb.CreateCategoryRequest) error {
- if err := svc.Repository.CreateMultipleCategories(svc.getCategoryReq(category)); err != nil {
- return errorcode.New(svc.Namespace, err.Error(), http.StatusBadRequest)
- }
- return nil
- }
- func (svc *Service) DeleteCategory(query *pb.DeleteCategoryRequest) error {
- if err := svc.Repository.DeleteCategory(query.CategoryId); err != nil {
- return errorcode.New(svc.Namespace, err.Error(), http.StatusBadRequest)
- }
- return nil
- }
- func (svc *Service) GetAllCategories() ([]*pb.Category, error) {
- categories := make([]*pb.Category, 0)
- list, err := svc.Repository.GetAllCategories()
- if err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return categories, nil // 记录不存在,角色不存在
- } else {
- return categories, err
- }
- }
- return svc.getCategoryRes(list, false), nil
- }
- func (svc *Service) GetCategoryList(query *pb.GetCategoryListRequest) ([]*pb.Category, int64, error) {
- categories := make([]*pb.Category, 0)
- list, count, err := svc.Repository.GetCategoryList(query)
- if err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return categories, 0, nil // 记录不存在,角色不存在
- } else {
- return categories, 0, err
- }
- }
- return svc.getCategoryRes(list, true), count, nil
- }
- func (svc *Service) getCategoryRes(items []req.Category, isTime bool) []*pb.Category {
- fmt.Println("isTime", isTime)
- categories := make([]*pb.Category, 0)
- for _, item := range items {
- var category *pb.Category
- common.SwapTo(item, &category)
- if isTime {
- category.CreatedAt = utils.ConvertTimeToInt64(item.CreatedAt)
- category.UpdatedAt = utils.ConvertTimeToInt64(*item.UpdatedAt)
- }
- categories = append(categories, category)
- }
- return categories
- }
- func (svc *Service) getCategoryReq(category *pb.CreateCategoryRequest) []req.Category {
- categories := make([]req.Category, 0)
- for _, item := range category.Items {
- categories = append(categories, req.Category{
- Name: item.Name,
- ParentCategoryID: item.ParentCategoryId,
- CreatedAt: time.Now(),
- Description: item.Description,
- UpdatedAt: nil,
- })
- }
- return categories
- }
|