Skip to content

Commit

Permalink
Refactoring to Repository pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak committed Jul 15, 2020
1 parent 0249977 commit ef82c2b
Show file tree
Hide file tree
Showing 20 changed files with 918 additions and 115 deletions.
8 changes: 7 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ CORS_ALLOWED_ORIGINS=http://localhost:8080

#SERVICE_ACCOUNT_FILE=/service-account-file.json
MOCK_AUTH=true
LOCAL_ENV=true
LOCAL_ENV=true

MYSQL_ADDR=localhost
MYSQL_DATABASE=db
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_RANDOM_ROOT_PASSWORD=true
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include .env

.PHONY: openapi
openapi: openapi_http openapi_js

Expand Down Expand Up @@ -39,3 +41,7 @@ lint:
@./scripts/lint.sh trainer
@./scripts/lint.sh trainings
@./scripts/lint.sh users

.PHONY: mycli
mycli:
mycli -u ${MYSQL_USER} -p ${MYSQL_PASSWORD} ${MYSQL_DATABASE}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ No application is perfect from the beginning. With over a dozen coming articles,
4. [**You should not build your own authentication. Let Firebase do it for you.**](https://threedots.tech/post/firebase-cloud-run-authentication/?utm_source=github.com)
5. [**Business Applications in Go: Things to know about DRY**](https://threedots.tech/post/things-to-know-about-dry/?utm_source=github.com)
6. [**When microservices in Go are not enough: introduction to DDD Lite**](https://threedots.tech/post/ddd-lite-in-go-introduction/?utm_source=github.com)
7. *More articles are on the way!*
7. [**Repository pattern: painless way to simplify your Go service logic**](https://threedots.tech/post/repository-pattern-in-go/?utm_source=github.com)
8. *More articles are on the way!*

### Directories

Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,13 @@ services:
- "127.0.0.1:8787:8787"
- "127.0.0.1:4000:4000"
restart: unless-stopped

mysql:
image: mysql:8
env_file:
- .env
volumes:
- ./sql/schema.sql:/docker-entrypoint-initdb.d/schema.sql
ports:
- "127.0.0.1:3306:3306"
restart: unless-stopped
2 changes: 1 addition & 1 deletion internal/common/logs/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"

"github.com/sirupsen/logrus"
"github.com/x-cray/logrus-prefixed-formatter"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

func Init() {
Expand Down
2 changes: 1 addition & 1 deletion internal/common/server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
Expand Down
33 changes: 28 additions & 5 deletions internal/trainer/domain/hour/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ package hour

import "github.com/pkg/errors"

var (
Available = Availability{"available"}
NotAvailable = Availability{"not_available"}
TrainingScheduled = Availability{"training_scheduled"}
)

var availabilityValues = []Availability{
Available,
NotAvailable,
TrainingScheduled,
}

// Availability is enum.
//
// Using struct instead of `type Availability string` for enums allows us to ensure,
Expand All @@ -11,24 +23,35 @@ type Availability struct {
a string
}

func NewAvailabilityFromString(availabilityStr string) (Availability, error) {
for _, availability := range availabilityValues {
if availability.String() == availabilityStr {
return availability, nil
}
}
return Availability{}, errors.Errorf("unknown '%s' availability", availabilityStr)
}

// Every type in Go have zero value. In that case it's `Availability{}`.
// It's always a good idea to check if provided value is not zero!
func (h Availability) IsZero() bool {
return h == Availability{}
}

var (
Available = Availability{"available"}
NotAvailable = Availability{"not_available"}
TrainingScheduled = Availability{"training_scheduled"}
)
func (h Availability) String() string {
return h.a
}

var (
ErrTrainingScheduled = errors.New("unable to modify hour, because scheduled training")
ErrNoTrainingScheduled = errors.New("training is not scheduled")
ErrHourNotAvailable = errors.New("hour is not available")
)

func (h Hour) Availability() Availability {
return h.availability
}

func (h Hour) IsAvailable() bool {
return h.availability == Available
}
Expand Down
37 changes: 31 additions & 6 deletions internal/trainer/domain/hour/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestHour_MakeNotAvailable(t *testing.T) {
h, err := hour.NewAvailableHour(validTrainingHour())
h, err := testHourFactory.NewAvailableHour(validTrainingHour())
require.NoError(t, err)

require.NoError(t, h.MakeNotAvailable())
Expand All @@ -23,7 +23,7 @@ func TestHour_MakeNotAvailable_with_scheduled_training(t *testing.T) {
}

func TestHour_MakeAvailable(t *testing.T) {
h, err := hour.NewAvailableHour(validTrainingHour())
h, err := testHourFactory.NewAvailableHour(validTrainingHour())
require.NoError(t, err)

require.NoError(t, h.MakeNotAvailable())
Expand All @@ -39,7 +39,7 @@ func TestHour_MakeAvailable_with_scheduled_training(t *testing.T) {
}

func TestHour_ScheduleTraining(t *testing.T) {
h, err := hour.NewAvailableHour(validTrainingHour())
h, err := testHourFactory.NewAvailableHour(validTrainingHour())
require.NoError(t, err)

require.NoError(t, h.ScheduleTraining())
Expand All @@ -63,14 +63,39 @@ func TestHour_CancelTraining(t *testing.T) {
}

func TestHour_CancelTraining_no_training_scheduled(t *testing.T) {
h, err := hour.NewAvailableHour(validTrainingHour())
h, err := testHourFactory.NewAvailableHour(validTrainingHour())
require.NoError(t, err)

assert.Equal(t, hour.ErrNoTrainingScheduled, h.CancelTraining())
}

func TestNewAvailabilityFromString(t *testing.T) {
testCases := []hour.Availability{
hour.Available,
hour.NotAvailable,
hour.TrainingScheduled,
}

for _, expectedAvailability := range testCases {
t.Run(expectedAvailability.String(), func(t *testing.T) {
availability, err := hour.NewAvailabilityFromString(expectedAvailability.String())
require.NoError(t, err)

assert.Equal(t, expectedAvailability, availability)
})
}
}

func TestNewAvailabilityFromString_invalid(t *testing.T) {
_, err := hour.NewAvailabilityFromString("invalid_value")
assert.Error(t, err)

_, err = hour.NewAvailabilityFromString("")
assert.Error(t, err)
}

func newHourWithScheduledTraining(t *testing.T) *hour.Hour {
h, err := hour.NewAvailableHour(validTrainingHour())
h, err := testHourFactory.NewAvailableHour(validTrainingHour())
require.NoError(t, err)

require.NoError(t, h.ScheduleTraining())
Expand All @@ -79,7 +104,7 @@ func newHourWithScheduledTraining(t *testing.T) *hour.Hour {
}

func newNotAvailableHour(t *testing.T) *hour.Hour {
h, err := hour.NewAvailableHour(validTrainingHour())
h, err := testHourFactory.NewAvailableHour(validTrainingHour())
require.NoError(t, err)

require.NoError(t, h.MakeNotAvailable())
Expand Down
Loading

0 comments on commit ef82c2b

Please sign in to comment.