Skip to content

Commit

Permalink
Using grpc client info from common repository
Browse files Browse the repository at this point in the history
  • Loading branch information
walterjgsp committed Sep 4, 2023
1 parent dbac110 commit 4f08ec3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 38 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ require (
github.com/google/uuid v1.3.0
github.com/smartystreets/goconvey v1.8.1
github.com/wcodesoft/mosha-quote-service v0.1.0
github.com/wcodesoft/mosha-service-common v0.0.9
github.com/wcodesoft/mosha-service-common v0.0.10
go.mongodb.org/mongo-driver v1.12.1
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
)

Expand Down Expand Up @@ -44,4 +43,5 @@ require (
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/wcodesoft/mosha-quote-service v0.1.0 h1:aW1QSl0146+5AOLHOoCTpXVIJ3tGA4YSt7aVRq82Jq8=
github.com/wcodesoft/mosha-quote-service v0.1.0/go.mod h1:qhLjxWAncwwevbWFDc4WukQ2uYWnWCtIlyZQxWo6tQE=
github.com/wcodesoft/mosha-service-common v0.0.9 h1:nTQaSOEuDS+tAXYO3U8eCT8MVG00vkl8giDm/5V9DJg=
github.com/wcodesoft/mosha-service-common v0.0.9/go.mod h1:hPNn94cRwS0e2b8Kp134GTfx3+qTDQaT+mU66YnXAYE=
github.com/wcodesoft/mosha-service-common v0.0.10 h1:BegnBRqycaRqvIh5A4XiDBH5x6eEyMsPdmksRFh5nzk=
github.com/wcodesoft/mosha-service-common v0.0.10/go.mod h1:hPNn94cRwS0e2b8Kp134GTfx3+qTDQaT+mU66YnXAYE=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/wcodesoft/mosha-author-service/repository"
"github.com/wcodesoft/mosha-author-service/service"
mdb "github.com/wcodesoft/mosha-service-common/database"
mgrpc "github.com/wcodesoft/mosha-service-common/grpc"
mhttp "github.com/wcodesoft/mosha-service-common/http"
"os"
"sync"
Expand Down Expand Up @@ -33,9 +34,14 @@ func main() {
grpcPort := getEnv("GRPC_PORT", defaultGrpcPort)
quoteServiceAddress := getEnv("QUOTE_SERVICE_ADDRESS", quoteGrpcAddress)

clientsRepository := repository.NewClientRepository(repository.ClientsAddress{
QuoteServiceAddress: quoteServiceAddress,
})
quoteGrpcClientInfo := mgrpc.ClientInfo{
Name: "QuoteService",
Address: quoteServiceAddress,
}
clientsRepository, err := repository.NewClientRepository(quoteGrpcClientInfo)
if err != nil {
log.Fatal(err)
}

mongoClient, err := mdb.NewMongoClient(mongoHost)
if err != nil {
Expand Down
27 changes: 10 additions & 17 deletions repository/client_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,32 @@ package repository

import (
"context"
"github.com/charmbracelet/log"
mgrpc "github.com/wcodesoft/mosha-service-common/grpc"
qpb "github.com/wcodesoft/mosha-service-common/protos/quoteservice"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type ClientRepository interface {
DeleteAuthorQuotes(authorID string) (bool, error)
}

type ClientsAddress struct {
QuoteServiceAddress string
DeleteAuthorQuotes(authorID string) error
}

type clientRepository struct {
quoteClient qpb.QuoteServiceClient
}

func (c *clientRepository) DeleteAuthorQuotes(authorID string) (bool, error) {
response, err := c.quoteClient.DeleteAllQuotesByAuthor(context.Background(), &qpb.DeleteQuotesByAuthorRequest{AuthorId: authorID})
return response.Success, err
// DeleteAuthorQuotes deletes all quotes from an author.
func (c *clientRepository) DeleteAuthorQuotes(authorID string) error {
_, err := c.quoteClient.DeleteAllQuotesByAuthor(context.Background(), &qpb.DeleteQuotesByAuthorRequest{AuthorId: authorID})
return err
}

// NewClientRepository creates a new client repository.
func NewClientRepository(address ClientsAddress) ClientRepository {
conn, err := grpc.Dial(address.QuoteServiceAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
func NewClientRepository(clientInfo mgrpc.ClientInfo) (ClientRepository, error) {
conn, err := clientInfo.NewClientConnection()
if err != nil {
log.Errorf("Could not connect to AuthorService at: %s", address.QuoteServiceAddress)
panic(err)
return nil, err
}
client := qpb.NewQuoteServiceClient(conn)
log.Infof("Connected to AuthorService at: %s", address.QuoteServiceAddress)
return &clientRepository{
quoteClient: client,
}
}, nil
}
9 changes: 3 additions & 6 deletions repository/fake_client_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ import (

type FakeClientRepository struct {
quotes []data.Quote
retValue bool
retError error
ClientRepository
}

func (f *FakeClientRepository) DeleteAuthorQuotes(_ string) (bool, error) {
return f.retValue, f.retError
func (f *FakeClientRepository) DeleteAuthorQuotes(_ string) error {
return f.retError
}

func (f *FakeClientRepository) SetDeleteAuthorQuotesReturn(value bool, err error) {
f.retValue = value
func (f *FakeClientRepository) SetDeleteAuthorQuotesReturn(err error) {
f.retError = err
}

func NewFakeClientRepository() *FakeClientRepository {
return &FakeClientRepository{
quotes: []data.Quote{},
retValue: true,
retError: nil,
}
}
9 changes: 1 addition & 8 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,8 @@ func TestRepository(t *testing.T) {
Convey("When deleting an author with errors on quote service", func() {
authorID, _ := repo.AddAuthor(data.NewAuthorBuilder().WithName(name).Build())

Convey("When quotes service return failure, should not delete author", func() {
clientRepository.SetDeleteAuthorQuotesReturn(false, nil)
err := repo.DeleteAuthor(authorID)
So(err, ShouldNotBeNil)
So(len(repo.ListAll()), ShouldEqual, 1)
})

Convey("When quotes service throw error, should not delete author", func() {
clientRepository.SetDeleteAuthorQuotesReturn(false, fmt.Errorf("error"))
clientRepository.SetDeleteAuthorQuotesReturn(fmt.Errorf("error"))
err := repo.DeleteAuthor(authorID)
So(err, ShouldNotBeNil)
So(len(repo.ListAll()), ShouldEqual, 1)
Expand Down

0 comments on commit 4f08ec3

Please sign in to comment.