-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d2ce60
commit 0249977
Showing
20 changed files
with
959 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
go run *.go | ||
go run . |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package hour | ||
|
||
import "github.com/pkg/errors" | ||
|
||
// Availability is enum. | ||
// | ||
// Using struct instead of `type Availability string` for enums allows us to ensure, | ||
// that we have full control of what values are possible. | ||
// With `type Availability string` you are able to create `Availability("i_can_put_anything_here")` | ||
type Availability struct { | ||
a string | ||
} | ||
|
||
// 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"} | ||
) | ||
|
||
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) IsAvailable() bool { | ||
return h.availability == Available | ||
} | ||
|
||
func (h Hour) HasTrainingScheduled() bool { | ||
return h.availability == TrainingScheduled | ||
} | ||
|
||
func (h *Hour) MakeNotAvailable() error { | ||
if h.HasTrainingScheduled() { | ||
return ErrTrainingScheduled | ||
} | ||
|
||
h.availability = NotAvailable | ||
return nil | ||
} | ||
|
||
func (h *Hour) MakeAvailable() error { | ||
if h.HasTrainingScheduled() { | ||
return ErrTrainingScheduled | ||
} | ||
|
||
h.availability = Available | ||
return nil | ||
} | ||
|
||
func (h *Hour) ScheduleTraining() error { | ||
if !h.IsAvailable() { | ||
return ErrHourNotAvailable | ||
} | ||
|
||
h.availability = TrainingScheduled | ||
return nil | ||
} | ||
|
||
func (h *Hour) CancelTraining() error { | ||
if !h.HasTrainingScheduled() { | ||
return ErrNoTrainingScheduled | ||
} | ||
|
||
h.availability = Available | ||
return nil | ||
} |
Oops, something went wrong.