1. BannerList

  1. func (s *GoodsServer) BannerList(ctx context.Context, req *emptypb.Empty) (*proto.BannerListResponse, error) {
  2. bannerListResponse := proto.BannerListResponse{}
  3. var banners []model.Banner
  4. result := global.DB.Find(&banners)
  5. bannerListResponse.Total = int32(result.RowsAffected)
  6. var bannerReponses []*proto.BannerResponse
  7. for _, banner := range banners {
  8. bannerReponses = append(bannerReponses, &proto.BannerResponse{
  9. Id: banner.ID,
  10. Image: banner.Image,
  11. Index: banner.Index,
  12. Url: banner.Url,
  13. })
  14. }
  15. bannerListResponse.Data = bannerReponses
  16. return &bannerListResponse, nil
  17. }

2. CreateBanner

  1. func (s *GoodsServer) CreateBanner(ctx context.Context, req *proto.BannerRequest) (*proto.BannerResponse, error) {
  2. banner := model.Banner{}
  3. banner.Image = req.Image
  4. banner.Index = req.Index
  5. banner.Url = req.Url
  6. global.DB.Save(&banner)
  7. return &proto.BannerResponse{Id:banner.ID}, nil
  8. }

3. DeleteBanner

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

4. UpdateBanner

  1. func (s *GoodsServer) UpdateBanner(ctx context.Context, req *proto.BannerRequest) (*emptypb.Empty, error) {
  2. var banner model.Banner
  3. if result := global.DB.First(&banner, req.Id); result.RowsAffected == 0 {
  4. return nil, status.Errorf(codes.NotFound, "轮播图不存在")
  5. }
  6. if req.Url != "" {
  7. banner.Url = req.Url
  8. }
  9. if req.Image != "" {
  10. banner.Image = req.Image
  11. }
  12. if req.Index != 0 {
  13. banner.Index = req.Index
  14. }
  15. global.DB.Save(&banner)
  16. return &emptypb.Empty{}, nil
  17. }