wild-workouts-go-ddd-example
wild-workouts-go-ddd-example copied to clipboard
Not persisting hours after creating them with the in-memory repository
So, you're creating a new Hour
, but returning it immediately without storing it in the map.
https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/22c0a25b67c4669d612a2fa4a434ffae8e35e65a/internal/trainer/adapters/hour_memory_repository.go#L40
It looks like ok
will be always false
.
https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/22c0a25b67c4669d612a2fa4a434ffae8e35e65a/internal/trainer/adapters/hour_memory_repository.go#L38
Am I missing something here?
That should be documented better 😉.
In general, the idea is, that the repository should be independent of the database, but be rather some domain concept. By example it will be probably more clear:
In practice (not in the database, in reality) hour exists, even if it was not persisted. So for example, if we didn't persist 14:00 it exists anyway and is available for training. So that's why it is not persisted in GetHour
.
If we want to persist it, we need to explicitly call UpdateHour
. This is also how it works for all other implementations.
Does it make sense for you?
What I was saying, is that you're just returning a newly created hour without putting it to the map.
https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/22c0a25b67c4669d612a2fa4a434ffae8e35e65a/internal/trainer/adapters/hour_memory_repository.go#L40
This should be IMO:
currentHour, err := m.hourFactory.NewNotAvailableHour(hourTime)
// Handle error, skipped
m.hours[hourTime] = *currentHour
Otherwise your in-memory storage doesn't make much sense.
@msamoylov It's persisted in the UpdateHour
method: https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/22c0a25b67c4669d612a2fa4a434ffae8e35e65a/internal/trainer/adapters/hour_memory_repository.go#L66
The result of GetHour
is returned to the frontend application, which shows the hour as empty, if it's not filled in the database. The user can then click a button to fill it.