feat: 初始提交云印享付费打印服务后端
This commit is contained in:
96
cloudprint-backend/internal/grpc/service/price_service.go
Normal file
96
cloudprint-backend/internal/grpc/service/price_service.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudprint/cloudprint-backend/internal/repository"
|
||||
pb "github.com/cloudprint/cloudprint-backend/proto/generated"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type PriceServiceServer struct {
|
||||
pb.UnimplementedPriceServiceServer
|
||||
priceRepo *repository.PriceRepository
|
||||
}
|
||||
|
||||
func NewPriceServiceServer(priceRepo *repository.PriceRepository) *PriceServiceServer {
|
||||
return &PriceServiceServer{
|
||||
priceRepo: priceRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PriceServiceServer) GetPriceList(ctx context.Context, req *pb.GetPriceListRequest) (*pb.PriceListResponse, error) {
|
||||
items, err := s.priceRepo.GetAllActive(ctx)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "get price list failed: %v", err)
|
||||
}
|
||||
|
||||
pbItems := make([]*pb.PriceItem, 0, len(items))
|
||||
for _, item := range items {
|
||||
pbItems = append(pbItems, &pb.PriceItem{
|
||||
Id: item.ID,
|
||||
Name: item.Name,
|
||||
Type: item.Type,
|
||||
Price: item.Price,
|
||||
Unit: item.Unit,
|
||||
ColorEnabled: item.ColorEnabled,
|
||||
IsActive: item.IsActive,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.PriceListResponse{Items: pbItems}, nil
|
||||
}
|
||||
|
||||
func (s *PriceServiceServer) CreatePriceItem(ctx context.Context, req *pb.CreatePriceItemRequest) (*pb.PriceItem, error) {
|
||||
item := &model.PriceList{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Price: req.Price,
|
||||
Unit: req.Unit,
|
||||
ColorEnabled: req.ColorEnabled,
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
if err := s.priceRepo.Create(ctx, item); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "create price item failed: %v", err)
|
||||
}
|
||||
|
||||
return &pb.PriceItem{
|
||||
Id: item.ID,
|
||||
Name: item.Name,
|
||||
Type: item.Type,
|
||||
Price: item.Price,
|
||||
Unit: item.Unit,
|
||||
ColorEnabled: item.ColorEnabled,
|
||||
IsActive: item.IsActive,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *PriceServiceServer) UpdatePriceItem(ctx context.Context, req *pb.UpdatePriceItemRequest) (*pb.PriceItem, error) {
|
||||
item, err := s.priceRepo.GetByID(ctx, req.Id)
|
||||
if err != nil || item == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "price item not found")
|
||||
}
|
||||
|
||||
item.Name = req.Name
|
||||
item.Type = req.Type
|
||||
item.Price = req.Price
|
||||
item.Unit = req.Unit
|
||||
item.ColorEnabled = req.ColorEnabled
|
||||
item.IsActive = req.IsActive
|
||||
|
||||
if err := s.priceRepo.Update(ctx, item); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "update price item failed: %v", err)
|
||||
}
|
||||
|
||||
return &pb.PriceItem{
|
||||
Id: item.ID,
|
||||
Name: item.Name,
|
||||
Type: item.Type,
|
||||
Price: item.Price,
|
||||
Unit: item.Unit,
|
||||
ColorEnabled: item.ColorEnabled,
|
||||
IsActive: item.IsActive,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user