Skip to content

Commit c557ce7

Browse files
committed
updated api and store
1 parent cf5dcc5 commit c557ce7

20 files changed

+79
-212
lines changed

api/client.go api/connector.go

File renamed without changes.

cmd/example/main.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package main
33
import (
44
"example/generated"
55
"example/internal/connectors"
6-
"example/internal/grpc"
7-
"example/internal/healthcheck"
8-
"example/internal/http"
9-
"example/internal/service"
6+
"example/internal/servers/grpc"
7+
"example/internal/servers/healthcheck"
8+
"example/internal/servers/http"
9+
"example/internal/services"
1010
"example/internal/store"
1111
"github.com/sirupsen/logrus"
1212
googleGrpc "google.golang.org/grpc"
@@ -22,15 +22,13 @@ func connectToMongo() connectors.MongoConnector {
2222
return mongoConnection
2323
}
2424

25-
func setupStores(conn connectors.MongoConnector) store.Store {
26-
s := store.NewStore()
27-
s.Set(store.PersonStoreName, store.NewPersonStore(conn.GetDatabase()))
28-
return s
25+
func setupStores(conn connectors.MongoConnector) {
26+
store.Instance().Person = store.NewPersonStore(conn.GetDatabase())
2927
}
3028

31-
func startGrpcServer(shutdown <-chan bool, stores store.Store) grpc.Server {
32-
var personService = service.NewPersonService(stores)
33-
var httpService = service.NewHttpService(personService)
29+
func startGrpcServer(shutdown <-chan bool) grpc.Server {
30+
var personService = services.NewPersonService()
31+
var httpService = services.NewHttpService(personService)
3432

3533
var opts = grpc.Options{
3634
ServiceRegistration: func(s *googleGrpc.Server) {
@@ -75,8 +73,8 @@ func main() {
7573
shutdown := make(chan bool)
7674

7775
conn := connectToMongo()
78-
stores := setupStores(conn)
79-
grpcServer := startGrpcServer(shutdown, stores)
76+
setupStores(conn)
77+
grpcServer := startGrpcServer(shutdown)
8078
startHealthCheckServer(shutdown, grpcServer)
8179
startHttpServer(shutdown)
8280

generated/mocks/HttpServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/mocks/HttpServiceServer.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/mocks/PersonServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/mocks/PersonServiceServer.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/models/person.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package models
2+
3+
import (
4+
"go.mongodb.org/mongo-driver/bson/primitive"
5+
"time"
6+
)
7+
8+
type Person struct {
9+
Id *primitive.ObjectID `bson:"_id,omitempty"`
10+
Name string `bson:"name"`
11+
Email string `bson:"email"`
12+
Status int32 `bson:"status"`
13+
CreatedAt *time.Time `bson:"created_at,omitempty"`
14+
UpdatedAt *time.Time `bson:"updated_at,omitempty"`
15+
}

internal/grpc/server.go internal/servers/grpc/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package grpc
33
import (
44
"errors"
55
"example/api"
6-
"example/internal/grpc/interceptors"
6+
"example/internal/servers/grpc/interceptors"
77
"example/util/check"
88
"fmt"
99
"github.com/grpc-ecosystem/go-grpc-prometheus"
File renamed without changes.
File renamed without changes.

internal/prometheus/server.go internal/servers/prometheus/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package prometheus
22

33
import (
4-
"example/internal/grpc"
4+
"example/internal/servers/grpc"
55
"fmt"
66
"github.com/grpc-ecosystem/go-grpc-prometheus"
77
"github.com/prometheus/client_golang/prometheus"

internal/service/http.go internal/services/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package service
1+
package services
22

33
import (
44
"context"

internal/service/person.go internal/services/person.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package service
1+
package services
22

33
import (
44
"context"
55
"example/generated"
6+
"example/internal/models"
67
"example/internal/store"
78
"example/util/transform"
89
"github.com/golang/protobuf/ptypes/empty"
@@ -15,23 +16,18 @@ type personService struct {
1516
Store store.Store
1617
}
1718

18-
func NewPersonService(store store.Store) example.PersonServiceServer {
19-
return &personService{
20-
Store: store,
21-
}
22-
}
23-
24-
func (p *personService) PersonStore() store.PersonStore {
25-
return p.Store.Get(store.PersonStoreName).(store.PersonStore)
19+
func NewPersonService() example.PersonServiceServer {
20+
return &personService{}
2621
}
2722

2823
func (p *personService) CreatePerson(_ context.Context, req *example.CreatePersonRequest) (*example.Person, error) {
29-
var person = store.Person{
24+
var personStore = store.Instance().Person
25+
var person = models.Person{
3026
Status: int32(req.Status),
3127
Name: req.Name,
3228
Email: req.Email,
3329
}
34-
err := p.PersonStore().CreatePerson(&person)
30+
err := personStore.CreatePerson(&person)
3531
if err != nil {
3632
return nil, err
3733
}
@@ -47,7 +43,8 @@ func (p *personService) CreatePerson(_ context.Context, req *example.CreatePerso
4743
}
4844

4945
func (p *personService) GetPerson(_ context.Context, req *example.GetPersonRequest) (*example.Person, error) {
50-
person, err := p.PersonStore().GetPerson(req.Id)
46+
var personStore = store.Instance().Person
47+
person, err := personStore.GetPerson(req.Id)
5148
if err != nil {
5249
return nil, status.Errorf(codes.NotFound, "Could not find Person with Object Id %s: %v", req.Id, err)
5350
}
@@ -62,13 +59,14 @@ func (p *personService) GetPerson(_ context.Context, req *example.GetPersonReque
6259
}
6360

6461
func (p *personService) GetPersons(_ context.Context, req *example.GetPersonsRequest) (*example.Persons, error) {
62+
var personStore = store.Instance().Person
6563
var filters = store.PersonFilters{
6664
Status: int32(req.Status),
6765
Skip: req.Skip,
6866
Limit: req.Limit,
6967
}
7068

71-
persons, err := p.PersonStore().GetPersons(&filters)
69+
persons, err := personStore.GetPersons(&filters)
7270
if err != nil {
7371
return nil, status.Errorf(codes.Internal, "Error retrieving persons: %s", err)
7472
}
@@ -89,20 +87,22 @@ func (p *personService) GetPersons(_ context.Context, req *example.GetPersonsReq
8987
}
9088

9189
func (p *personService) UpdatePerson(_ context.Context, req *example.UpdatePersonRequest) (*empty.Empty, error) {
92-
var person = store.Person{
90+
var personStore = store.Instance().Person
91+
var person = models.Person{
9392
Status: int32(req.Status),
9493
Name: req.Name,
9594
Email: req.Email,
9695
}
97-
var err = p.PersonStore().UpdatePerson(req.Id, &person)
96+
var err = personStore.UpdatePerson(req.Id, &person)
9897
if err != nil {
9998
return nil, status.Errorf(codes.NotFound, "Error updated person with id {%s}: %v", req.Id, err)
10099
}
101100
return &empty.Empty{}, nil
102101
}
103102

104103
func (p *personService) DeletePerson(_ context.Context, req *example.DeletePersonRequest) (*empty.Empty, error) {
105-
var err = p.PersonStore().DeleteRequest(req.Id)
104+
var personStore = store.Instance().Person
105+
var err = personStore.DeleteRequest(req.Id)
106106
if err != nil {
107107
return nil, status.Errorf(codes.NotFound, "Error deleting person with id {%s}: %v", req.Id, err)
108108
}

internal/service/person_test.go internal/services/person_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package service
1+
package services
22

33
import (
44
"context"
55
"example/generated"
6+
"example/internal/models"
67
"example/internal/store"
78
"example/internal/store/mocks"
89
"example/util/transform"
@@ -14,7 +15,7 @@ import (
1415
)
1516

1617
var now = time.Now()
17-
var personItem = store.Person{
18+
var personItem = models.Person{
1819
Id: transform.ToObjectId("5f35832b7913cffd5a329af7"),
1920
Status: int32(example.Status_ACTIVE),
2021
Name: "John Smith",
@@ -26,7 +27,7 @@ var personItem = store.Person{
2627
func newPersonStore() store.PersonStore {
2728
var mockStore = mocks.PersonStore{}
2829
//CreatePerson(person *Person) error
29-
mockStore.On("CreatePerson", mock.Anything).Return(func(person *store.Person) error {
30+
mockStore.On("CreatePerson", mock.Anything).Return(func(person *models.Person) error {
3031
person.Id = personItem.Id
3132
person.CreatedAt = personItem.CreatedAt
3233
person.UpdatedAt = personItem.UpdatedAt
@@ -35,8 +36,8 @@ func newPersonStore() store.PersonStore {
3536
//GetPerson(id string) (*Person, error)
3637
mockStore.On("GetPerson", mock.Anything).Return(&personItem, nil)
3738
//GetPersons(filters *PersonFilters) ([]Person, error)
38-
mockStore.On("GetPersons", mock.Anything).Return(func(filters *store.PersonFilters) []store.Person {
39-
var persons = make([]store.Person, 0)
39+
mockStore.On("GetPersons", mock.Anything).Return(func(filters *store.PersonFilters) []models.Person {
40+
var persons = make([]models.Person, 0)
4041
persons = append(persons, personItem)
4142
return persons
4243
}, nil)
@@ -53,9 +54,8 @@ type TestServiceSuite struct {
5354
}
5455

5556
func (p *TestServiceSuite) SetupTest() {
56-
var personStore = store.NewStore()
57-
personStore.Set(store.PersonStoreName, newPersonStore())
58-
p.server = NewPersonService(personStore)
57+
store.Instance().Person = newPersonStore()
58+
p.server = NewPersonService()
5959
}
6060

6161
func (p *TestServiceSuite) TestCreatePerson() {

internal/service/services.go internal/services/services.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package service
1+
package services
22

33
//import "example/generated"
44
//

internal/store/mocks/PersonStore.go

-99
This file was deleted.

0 commit comments

Comments
 (0)