1. BrandList

  1. func (s *GoodsServer) BrandList(ctx context.Context, req *proto.BrandFilterRequest) (*proto.BrandListResponse, error){
  2. brandListResponse := proto.BrandListResponse{}
  3. var brands []model.Brands
  4. result := global.DB.Scopes(Paginate(int(req.Pages), int(req.PagePerNums))).Find(&brands)
  5. if result.Error != nil {
  6. return nil, result.Error
  7. }
  8. var total int64
  9. global.DB.Model(&model.Brands{}).Count(&total)
  10. brandListResponse.Total = int32(total)
  11. var brandResponses []*proto.BrandInfoResponse
  12. for _, brand := range brands {
  13. brandResponses = append(brandResponses, &proto.BrandInfoResponse{
  14. Id: brand.ID,
  15. Name: brand.Name,
  16. Logo: brand.Logo,
  17. })
  18. }
  19. brandListResponse.Data = brandResponses
  20. return &brandListResponse, nil
  21. }

2. CreateBrand

  1. func (s *GoodsServer) CreateBrand(ctx context.Context, req *proto.BrandRequest) (*proto.BrandInfoResponse, error){
  2. //新建品牌
  3. if result := global.DB.First(&model.Brands{}); result.RowsAffected == 1 {
  4. return nil, status.Errorf(codes.InvalidArgument, "品牌已存在")
  5. }
  6. brand := &model.Brands{
  7. Name: req.Name,
  8. Logo: req.Logo,
  9. }
  10. global.DB.Save(brand)
  11. return &proto.BrandInfoResponse{Id: brand.ID}, nil
  12. }

3. DeleteBrand

  1. func (s *GoodsServer) DeleteBrand(ctx context.Context, req *proto.BrandRequest) (*emptypb.Empty, error) {
  2. if result := global.DB.Delete(&model.Brands{}, req.Id); result.RowsAffected == 0{
  3. return nil, status.Errorf(codes.NotFound, "品牌不存在")
  4. }
  5. return &emptypb.Empty{}, nil
  6. }

4. UpdateBrand

  1. func (s *GoodsServer) UpdateBrand(ctx context.Context, req *proto.BrandRequest) (*emptypb.Empty, error){
  2. brands := model.Brands{}
  3. if result := global.DB.First(&brands); result.RowsAffected == 0 {
  4. return nil, status.Errorf(codes.InvalidArgument, "品牌不存在")
  5. }
  6. if req.Name != "" {
  7. brands.Name = req.Name
  8. }
  9. if req.Logo != "" {
  10. brands.Logo = req.Logo
  11. }
  12. global.DB.Save(&brands)
  13. return &emptypb.Empty{}, nil
  14. }