From 9d324eff1e316a144f8351de39020110454df16b Mon Sep 17 00:00:00 2001 From: Alireza Josheghani Date: Wed, 26 Aug 2020 23:43:18 +0430 Subject: [PATCH] Adding Get Single Connection in user service --- go.mod | 2 +- go.sum | 2 ++ services/user/connections.go | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a962ed7..ff464e1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/CastyLab/grpc.server go 1.14 require ( - github.com/CastyLab/grpc.proto v0.0.0-20200826181838-2f9360028306 + github.com/CastyLab/grpc.proto v0.0.0-20200826191006-9099557119fd github.com/MrJoshLab/go-respond v0.0.0-20191125172458-25d4d18aaa1b github.com/asticode/go-astisub v0.2.0 github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/go.sum b/go.sum index 3214aba..e96eb7c 100644 --- a/go.sum +++ b/go.sum @@ -152,6 +152,8 @@ github.com/CastyLab/grpc.proto v0.0.0-20200826180959-ee61a995f35f h1:+gsV1RgXrjP github.com/CastyLab/grpc.proto v0.0.0-20200826180959-ee61a995f35f/go.mod h1:5ypgijUv7rWXMsRbUZXEgTucAA+4BpHtXgGIvOyUGRU= github.com/CastyLab/grpc.proto v0.0.0-20200826181838-2f9360028306 h1:rxE+/s9/T0FQGEDD2J+FtgwVAZuQWSRJzr0Yt6hXa9g= github.com/CastyLab/grpc.proto v0.0.0-20200826181838-2f9360028306/go.mod h1:5ypgijUv7rWXMsRbUZXEgTucAA+4BpHtXgGIvOyUGRU= +github.com/CastyLab/grpc.proto v0.0.0-20200826191006-9099557119fd h1:FBfJeHIVSQ8mCDTg1WP3FQRjpFpC0d67Zvc1uq+0UxQ= +github.com/CastyLab/grpc.proto v0.0.0-20200826191006-9099557119fd/go.mod h1:5ypgijUv7rWXMsRbUZXEgTucAA+4BpHtXgGIvOyUGRU= github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw= github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= diff --git a/services/user/connections.go b/services/user/connections.go index c8028ca..3df19a5 100644 --- a/services/user/connections.go +++ b/services/user/connections.go @@ -13,6 +13,48 @@ import ( "net/http" ) +func (s *Service) GetConnection(ctx context.Context, req *proto.GetConnectionRequest) (*proto.ConnectionsResponse, error) { + + var ( + connections = make([]*proto.Connection, 0) + collection = db.Connection.Collection("connections") + ) + + user, err := auth.Authenticate(req.AuthRequest) + if err != nil { + return nil, err + } + + filter := bson.M{ + "type": req.Connection.Type, + "user_id": user.ID, + } + + cursor, err := collection.Find(ctx, filter) + if err != nil { + return nil, status.Error(codes.NotFound, "Could not find connections!") + } + + for cursor.Next(ctx) { + connection := new(models.Connection) + if err := cursor.Decode(connection); err != nil { + continue + } + protoConnection, err := helpers.NewProtoConnection(connection) + if err != nil { + continue + } + connections = append(connections, protoConnection) + } + + return &proto.ConnectionsResponse{ + Status: "success", + Code: http.StatusOK, + Result: connections, + }, nil + +} + func (s *Service) GetConnections(ctx context.Context, req *proto.AuthenticateRequest) (*proto.ConnectionsResponse, error) { var (