-
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
Showing
32 changed files
with
1,124 additions
and
519 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package errors | ||
|
||
type ErrorType struct { | ||
t string | ||
} | ||
|
||
var ( | ||
ErrorTypeUnknown = ErrorType{"unknown"} | ||
ErrorTypeAuthorization = ErrorType{"authorization"} | ||
ErrorTypeIncorrectInput = ErrorType{"incorrect-input"} | ||
) | ||
|
||
type SlugError struct { | ||
error string | ||
slug string | ||
errorType ErrorType | ||
} | ||
|
||
func (s SlugError) Error() string { | ||
return s.error | ||
} | ||
|
||
func (s SlugError) Slug() string { | ||
return s.slug | ||
} | ||
|
||
func (s SlugError) ErrorType() ErrorType { | ||
return s.errorType | ||
} | ||
|
||
func NewSlugError(error string, slug string) SlugError { | ||
return SlugError{ | ||
error: error, | ||
slug: slug, | ||
errorType: ErrorTypeUnknown, | ||
} | ||
} | ||
|
||
func NewAuthorizationError(error string, slug string) SlugError { | ||
return SlugError{ | ||
error: error, | ||
slug: slug, | ||
errorType: ErrorTypeAuthorization, | ||
} | ||
} | ||
|
||
func NewIncorrectInputError(error string, slug string) SlugError { | ||
return SlugError{ | ||
error: error, | ||
slug: slug, | ||
errorType: ErrorTypeIncorrectInput, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package adapters | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/app" | ||
|
||
"cloud.google.com/go/firestore" | ||
"google.golang.org/api/iterator" | ||
) | ||
|
||
type DateModel struct { | ||
Date time.Time `firestore:"Date"` | ||
HasFreeHours bool `firestore:"HasFreeHours"` | ||
Hours []HourModel `firestore:"Hours"` | ||
} | ||
|
||
type HourModel struct { | ||
Available bool `firestore:"Available"` | ||
HasTrainingScheduled bool `firestore:"HasTrainingScheduled"` | ||
Hour time.Time `firestore:"Hour"` | ||
} | ||
|
||
type DatesFirestoreRepository struct { | ||
firestoreClient *firestore.Client | ||
} | ||
|
||
func NewDatesFirestoreRepository(firestoreClient *firestore.Client) DatesFirestoreRepository { | ||
if firestoreClient == nil { | ||
panic("missing firestoreClient") | ||
} | ||
|
||
return DatesFirestoreRepository{ | ||
firestoreClient: firestoreClient, | ||
} | ||
} | ||
|
||
func (d DatesFirestoreRepository) trainerHoursCollection() *firestore.CollectionRef { | ||
return d.firestoreClient.Collection("trainer-hours") | ||
} | ||
|
||
func (d DatesFirestoreRepository) DocumentRef(dateTimeToUpdate time.Time) *firestore.DocumentRef { | ||
return d.trainerHoursCollection().Doc(dateTimeToUpdate.Format("2006-01-02")) | ||
} | ||
|
||
func (d DatesFirestoreRepository) GetDates(ctx context.Context, from time.Time, to time.Time) ([]app.Date, error) { | ||
iter := d. | ||
trainerHoursCollection(). | ||
Where("Date", ">=", from). | ||
Where("Date", "<=", to). | ||
Documents(ctx) | ||
|
||
var dates []app.Date | ||
|
||
for { | ||
doc, err := iter.Next() | ||
if err == iterator.Done { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
date := DateModel{} | ||
if err := doc.DataTo(&date); err != nil { | ||
return nil, err | ||
} | ||
dates = append(dates, dateModelToApp(date)) | ||
} | ||
|
||
return dates, nil | ||
} | ||
|
||
func dateModelToApp(dm DateModel) app.Date { | ||
var hours []app.Hour | ||
for _, h := range dm.Hours { | ||
hours = append(hours, app.Hour{ | ||
Available: h.Available, | ||
HasTrainingScheduled: h.HasTrainingScheduled, | ||
Hour: h.Hour, | ||
}) | ||
} | ||
|
||
return app.Date{ | ||
Date: dm.Date, | ||
HasFreeHours: dm.HasFreeHours, | ||
Hours: hours, | ||
} | ||
} | ||
|
||
func (d DatesFirestoreRepository) CanLoadFixtures(ctx context.Context, daysToSet int) (bool, error) { | ||
documents, err := d.trainerHoursCollection().Limit(daysToSet).Documents(ctx).GetAll() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return len(documents) < daysToSet, nil | ||
} |
2 changes: 1 addition & 1 deletion
2
...rnal/trainer/hour_firestore_repository.go → ...ner/adapters/hour_firestore_repository.go
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,4 +1,4 @@ | ||
package main | ||
package adapters | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
internal/trainer/hour_memory_repository.go → ...rainer/adapters/hour_memory_repository.go
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,4 +1,4 @@ | ||
package main | ||
package adapters | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
internal/trainer/hour_mysql_repository.go → ...trainer/adapters/hour_mysql_repository.go
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,4 +1,4 @@ | ||
package main | ||
package adapters | ||
|
||
import ( | ||
"context" | ||
|
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
Oops, something went wrong.