76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package grpc
|
|
|
|
import (
|
|
"golang.org/x/net/context"
|
|
"google.golang.org/grpc/metadata"
|
|
"src.dualinventive.com/go/assets-service/internal/assets"
|
|
"src.dualinventive.com/go/assets-service/internal/storage"
|
|
"src.dualinventive.com/go/lib/dilog"
|
|
mtinfo "src.dualinventive.com/go/mtinfo-go"
|
|
)
|
|
|
|
//Server is a structure of server instance
|
|
type Server struct {
|
|
Name string
|
|
AssetService *assets.AssetService
|
|
}
|
|
|
|
//NewServer creates new instance of Server
|
|
func NewServer(
|
|
logger dilog.Logger,
|
|
assetRepository storage.AssetRepository,
|
|
mtinfoClient *mtinfo.Client) (*Server, error) {
|
|
server := &Server{
|
|
Name: "authentication-service-grpc",
|
|
AssetService: &assets.AssetService{
|
|
Logger: logger,
|
|
AssetRepository: assetRepository,
|
|
Mtinfo: mtinfoClient,
|
|
},
|
|
}
|
|
return server, nil
|
|
}
|
|
|
|
//GetAssetByID returns an asset found by the given ID
|
|
func (s *Server) GetAssetByID(ctx context.Context, req *GetAssetByIDRequest) (*GetAssetByIDResponse, error) {
|
|
asset, err := s.AssetService.GetAssetByID(getValue(ctx, "token"), req.AssetID)
|
|
if err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
|
|
return &GetAssetByIDResponse{Asset: mapAsset(asset)}, nil
|
|
}
|
|
|
|
//GetAssets returns a list of assets
|
|
func (s *Server) GetAssets(ctx context.Context, req *GetAssetsRequest) (*GetAssetsResponse, error) {
|
|
assets, count, err := s.AssetService.GetAssets(
|
|
getValue(ctx, "token"),
|
|
req.Page,
|
|
req.PerPage,
|
|
mapSortCol(req.Sort),
|
|
)
|
|
if err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
|
|
return &GetAssetsResponse{Assets: mapAssets(assets), Count: count}, nil
|
|
}
|
|
|
|
func getValue(ctx context.Context, s string) string { //nolint: unparam
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
|
|
raw := md.Get(s)
|
|
if raw == nil {
|
|
return ""
|
|
}
|
|
|
|
if len(raw) == 0 {
|
|
return ""
|
|
}
|
|
|
|
return raw[0]
|
|
}
|