Skip to content

Commit 0249977

Browse files
committed
Refactoring with DDD Lite
1 parent 3d2ce60 commit 0249977

File tree

20 files changed

+959
-247
lines changed

20 files changed

+959
-247
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Wild Workouts
22

3+
*The idea for this series, is to apply DDD by refactoring. This process is in progress! Please check articles, to know the current progress.*
4+
35
Wild Workouts is an example project that we created to show how to build Go applications that are **easy to develop, maintain, and fun to work with, especially in the long term!**
46

57
No application is perfect from the beginning. With over a dozen coming articles, we will uncover what issues you can find in the current implementation. We will also show how to fix these issues and achieve clean implementation by refactoring.
@@ -11,7 +13,8 @@ No application is perfect from the beginning. With over a dozen coming articles,
1113
3. [**Robust gRPC communication on Google Cloud Run (but not only!)**](https://threedots.tech/post/robust-grpc-google-cloud-run/?utm_source=github.com)
1214
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)
1315
5. [**Business Applications in Go: Things to know about DRY**](https://threedots.tech/post/things-to-know-about-dry/?utm_source=github.com)
14-
6. *More articles are on the way!*
16+
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)
17+
7. *More articles are on the way!*
1518

1619
### Directories
1720

api/protobuf/trainer.proto

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import "google/protobuf/timestamp.proto";
66

77
service TrainerService {
88
rpc IsHourAvailable(IsHourAvailableRequest) returns (IsHourAvailableResponse) {}
9-
rpc UpdateHour(UpdateHourRequest) returns (EmptyResponse) {}
9+
rpc ScheduleTraining(UpdateHourRequest) returns (EmptyResponse) {}
10+
rpc CancelTraining(UpdateHourRequest) returns (EmptyResponse) {}
11+
rpc MakeHourAvailable(UpdateHourRequest) returns (EmptyResponse) {}
1012
}
1113

1214
message IsHourAvailableRequest {
@@ -19,9 +21,6 @@ message IsHourAvailableResponse {
1921

2022
message UpdateHourRequest {
2123
google.protobuf.Timestamp time = 1;
22-
23-
bool has_training_scheduled = 2;
24-
bool available = 3;
2524
}
2625

2726
message EmptyResponse {}

docker/app/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
go run *.go
1+
go run .

internal/common/genproto/trainer/trainer.pb.go

Lines changed: 103 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package hour
2+
3+
import "github.com/pkg/errors"
4+
5+
// Availability is enum.
6+
//
7+
// Using struct instead of `type Availability string` for enums allows us to ensure,
8+
// that we have full control of what values are possible.
9+
// With `type Availability string` you are able to create `Availability("i_can_put_anything_here")`
10+
type Availability struct {
11+
a string
12+
}
13+
14+
// Every type in Go have zero value. In that case it's `Availability{}`.
15+
// It's always a good idea to check if provided value is not zero!
16+
func (h Availability) IsZero() bool {
17+
return h == Availability{}
18+
}
19+
20+
var (
21+
Available = Availability{"available"}
22+
NotAvailable = Availability{"not_available"}
23+
TrainingScheduled = Availability{"training_scheduled"}
24+
)
25+
26+
var (
27+
ErrTrainingScheduled = errors.New("unable to modify hour, because scheduled training")
28+
ErrNoTrainingScheduled = errors.New("training is not scheduled")
29+
ErrHourNotAvailable = errors.New("hour is not available")
30+
)
31+
32+
func (h Hour) IsAvailable() bool {
33+
return h.availability == Available
34+
}
35+
36+
func (h Hour) HasTrainingScheduled() bool {
37+
return h.availability == TrainingScheduled
38+
}
39+
40+
func (h *Hour) MakeNotAvailable() error {
41+
if h.HasTrainingScheduled() {
42+
return ErrTrainingScheduled
43+
}
44+
45+
h.availability = NotAvailable
46+
return nil
47+
}
48+
49+
func (h *Hour) MakeAvailable() error {
50+
if h.HasTrainingScheduled() {
51+
return ErrTrainingScheduled
52+
}
53+
54+
h.availability = Available
55+
return nil
56+
}
57+
58+
func (h *Hour) ScheduleTraining() error {
59+
if !h.IsAvailable() {
60+
return ErrHourNotAvailable
61+
}
62+
63+
h.availability = TrainingScheduled
64+
return nil
65+
}
66+
67+
func (h *Hour) CancelTraining() error {
68+
if !h.HasTrainingScheduled() {
69+
return ErrNoTrainingScheduled
70+
}
71+
72+
h.availability = Available
73+
return nil
74+
}

0 commit comments

Comments
 (0)