1. BannerList
func (s *GoodsServer) BannerList(ctx context.Context, req *emptypb.Empty) (*proto.BannerListResponse, error) {
bannerListResponse := proto.BannerListResponse{}
var banners []model.Banner
result := global.DB.Find(&banners)
bannerListResponse.Total = int32(result.RowsAffected)
var bannerReponses []*proto.BannerResponse
for _, banner := range banners {
bannerReponses = append(bannerReponses, &proto.BannerResponse{
Id: banner.ID,
Image: banner.Image,
Index: banner.Index,
Url: banner.Url,
})
}
bannerListResponse.Data = bannerReponses
return &bannerListResponse, nil
}
2. CreateBanner
func (s *GoodsServer) CreateBanner(ctx context.Context, req *proto.BannerRequest) (*proto.BannerResponse, error) {
banner := model.Banner{}
banner.Image = req.Image
banner.Index = req.Index
banner.Url = req.Url
global.DB.Save(&banner)
return &proto.BannerResponse{Id:banner.ID}, nil
}
3. DeleteBanner
func (s *GoodsServer) DeleteBanner(ctx context.Context, req *proto.BannerRequest) (*emptypb.Empty, error) {
if result := global.DB.Delete(&model.Banner{}, req.Id); result.RowsAffected == 0 {
return nil, status.Errorf(codes.NotFound, "轮播图不存在")
}
return &emptypb.Empty{}, nil
}
4. UpdateBanner
func (s *GoodsServer) UpdateBanner(ctx context.Context, req *proto.BannerRequest) (*emptypb.Empty, error) {
var banner model.Banner
if result := global.DB.First(&banner, req.Id); result.RowsAffected == 0 {
return nil, status.Errorf(codes.NotFound, "轮播图不存在")
}
if req.Url != "" {
banner.Url = req.Url
}
if req.Image != "" {
banner.Image = req.Image
}
if req.Index != 0 {
banner.Index = req.Index
}
global.DB.Save(&banner)
return &emptypb.Empty{}, nil
}