feat(training): add InMemoryRepository for mocking
This commit is contained in:
parent
2ca890c66a
commit
adb793034a
2 changed files with 86 additions and 0 deletions
7
pkg/training/error.go
Normal file
7
pkg/training/error.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package training
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrTrainingNotFound = errors.New("training not found")
|
||||
)
|
||||
79
pkg/training/repository.go
Normal file
79
pkg/training/repository.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package training
|
||||
|
||||
type TrainingRepository interface {
|
||||
Create(training *Training) error
|
||||
FindByID(id TrainingID) (*Training, error)
|
||||
FindAll() ([]Training, error)
|
||||
Update(id TrainingID, training *Training) error
|
||||
Delete(id TrainingID) error
|
||||
}
|
||||
|
||||
type InMemoryTrainingRepository struct {
|
||||
trainings map[TrainingID]Training
|
||||
}
|
||||
|
||||
func NewInMemoryTrainingRepository() *InMemoryTrainingRepository {
|
||||
repo := &InMemoryTrainingRepository{
|
||||
trainings: make(map[TrainingID]Training),
|
||||
}
|
||||
|
||||
repo.Create(&Training{
|
||||
Name: "Kubernetes",
|
||||
Days: 2,
|
||||
})
|
||||
repo.Create(&Training{
|
||||
Name: "Terraform",
|
||||
Days: 1,
|
||||
})
|
||||
repo.Create(&Training{
|
||||
Name: "RKE2",
|
||||
Days: 1,
|
||||
})
|
||||
repo.Create(&Training{
|
||||
Name: "GitHub Actions",
|
||||
Days: 1,
|
||||
})
|
||||
repo.Create(&Training{
|
||||
Name: "GitLab CI",
|
||||
Days: 1,
|
||||
})
|
||||
repo.Create(&Training{
|
||||
Name: "Prometheus",
|
||||
Days: 2,
|
||||
})
|
||||
|
||||
return repo
|
||||
}
|
||||
|
||||
func (r *InMemoryTrainingRepository) Create(training *Training) error {
|
||||
training.ID = NewTrainingID()
|
||||
r.trainings[training.ID] = *training
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *InMemoryTrainingRepository) FindByID(id TrainingID) (*Training, error) {
|
||||
training, ok := r.trainings[id]
|
||||
if !ok {
|
||||
return nil, ErrTrainingNotFound
|
||||
}
|
||||
return &training, nil
|
||||
}
|
||||
|
||||
func (r *InMemoryTrainingRepository) FindAll() ([]Training, error) {
|
||||
|
||||
trainings := make([]Training, 0, len(r.trainings))
|
||||
for _, training := range r.trainings {
|
||||
trainings = append(trainings, training)
|
||||
}
|
||||
return trainings, nil
|
||||
}
|
||||
|
||||
func (r *InMemoryTrainingRepository) Update(id TrainingID, training *Training) error {
|
||||
r.trainings[id] = *training
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *InMemoryTrainingRepository) Delete(id TrainingID) error {
|
||||
delete(r.trainings, id)
|
||||
return nil
|
||||
}
|
||||
Reference in a new issue