155 lines
4.4 KiB
Go
155 lines
4.4 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
type mappingServer struct {
|
|
pb.UnimplementedMappingServiceServer
|
|
*server
|
|
}
|
|
|
|
func (s *server) mapping() (storecontract.MappingStore, error) {
|
|
if s.deps.Mappings == nil {
|
|
return nil, errMappingUnavailable
|
|
}
|
|
return s.deps.Mappings, nil
|
|
}
|
|
|
|
func (s *mappingServer) InsertMapping(ctx context.Context, r *pb.InsertMappingRequest) (*pb.InsertMappingResponse, error) {
|
|
ms, err := s.mapping()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
value := mappingValue(r.Mapping)
|
|
if err := value.Validate(); err != nil {
|
|
return nil, validationFailure(err)
|
|
}
|
|
id, err := ms.InsertMapping(ctx, value)
|
|
if err != nil {
|
|
return nil, s.logErr("InsertMapping", err)
|
|
}
|
|
return &pb.InsertMappingResponse{Id: id}, nil
|
|
}
|
|
|
|
func (s *mappingServer) GetMapping(ctx context.Context, r *pb.GetMappingRequest) (*pb.GetMappingResponse, error) {
|
|
ms, err := s.mapping()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m, err := ms.GetMapping(ctx, r.Id)
|
|
if err != nil {
|
|
return nil, s.logErr("GetMapping", err)
|
|
}
|
|
return &pb.GetMappingResponse{Mapping: mappingProtoPtr(m), Found: m != nil}, nil
|
|
}
|
|
|
|
func (s *mappingServer) ListMappings(ctx context.Context, _ *pb.ListMappingsRequest) (*pb.ListMappingsResponse, error) {
|
|
ms, err := s.mapping()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mappings, err := ms.ListMappings(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("ListMappings", err)
|
|
}
|
|
return &pb.ListMappingsResponse{Mappings: mapValues(mappings, mappingProto)}, nil
|
|
}
|
|
|
|
func (s *mappingServer) UpdateMapping(ctx context.Context, r *pb.UpdateMappingRequest) (*pb.UpdateMappingResponse, error) {
|
|
ms, err := s.mapping()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
value := mappingValue(r.Mapping)
|
|
if err := value.Validate(); err != nil {
|
|
return nil, validationFailure(err)
|
|
}
|
|
if err := ms.UpdateMapping(ctx, value); err != nil {
|
|
return nil, s.logErr("UpdateMapping", err)
|
|
}
|
|
return &pb.UpdateMappingResponse{}, nil
|
|
}
|
|
|
|
func (s *mappingServer) DeleteMapping(ctx context.Context, r *pb.DeleteMappingRequest) (*pb.DeleteMappingResponse, error) {
|
|
ms, err := s.mapping()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := ms.DeleteMapping(ctx, r.Id); err != nil {
|
|
return nil, s.logErr("DeleteMapping", err)
|
|
}
|
|
return &pb.DeleteMappingResponse{}, nil
|
|
}
|
|
|
|
func (s *mappingServer) RecordMappingMatch(ctx context.Context, r *pb.RecordMappingMatchRequest) (*pb.RecordMappingMatchResponse, error) {
|
|
if s.deps.MappingMatches == nil {
|
|
return nil, status.Error(codes.Unavailable, "mapping match recorder unavailable")
|
|
}
|
|
if err := s.deps.MappingMatches.RecordMappingMatch(ctx, r.MappingId, r.CaptureId); err != nil {
|
|
return nil, s.logErr("RecordMappingMatch", err)
|
|
}
|
|
return &pb.RecordMappingMatchResponse{}, nil
|
|
}
|
|
|
|
// Binding
|
|
|
|
func mappingProtoPtr(m *mapping.Mapping) *pb.Mapping {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
return mappingProto(*m)
|
|
}
|
|
|
|
func (c *Client) InsertMapping(ctx context.Context, m mapping.Mapping) (string, error) {
|
|
r, err := c.mapping.InsertMapping(ctx, &pb.InsertMappingRequest{Mapping: mappingProto(m)})
|
|
if err != nil {
|
|
return "", unmapError(err)
|
|
}
|
|
return r.Id, nil
|
|
}
|
|
|
|
func (c *Client) GetMapping(ctx context.Context, id string) (*mapping.Mapping, error) {
|
|
r, err := c.mapping.GetMapping(ctx, &pb.GetMappingRequest{Id: id})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
if !r.Found {
|
|
return nil, nil
|
|
}
|
|
v := mappingValue(r.Mapping)
|
|
return &v, nil
|
|
}
|
|
|
|
func (c *Client) ListMappings(ctx context.Context) ([]mapping.Mapping, error) {
|
|
r, err := c.mapping.ListMappings(ctx, &pb.ListMappingsRequest{})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
return mapValues(r.Mappings, mappingValue), nil
|
|
}
|
|
|
|
func (c *Client) UpdateMapping(ctx context.Context, m mapping.Mapping) error {
|
|
_, err := c.mapping.UpdateMapping(ctx, &pb.UpdateMappingRequest{Mapping: mappingProto(m)})
|
|
return unmapError(err)
|
|
}
|
|
|
|
func (c *Client) DeleteMapping(ctx context.Context, id string) error {
|
|
_, err := c.mapping.DeleteMapping(ctx, &pb.DeleteMappingRequest{Id: id})
|
|
return unmapError(err)
|
|
}
|
|
|
|
func (c *Client) RecordMappingMatch(ctx context.Context, mappingID, captureID string) error {
|
|
_, err := c.mapping.RecordMappingMatch(ctx, &pb.RecordMappingMatchRequest{MappingId: mappingID, CaptureId: captureID})
|
|
return unmapError(err)
|
|
}
|
|
|
|
// Binding
|