244 lines
5.1 KiB
Go
244 lines
5.1 KiB
Go
package training
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/shopspring/decimal"
|
|
"gitlab.mareshq.com/hq/yggdrasil/internal/currency"
|
|
)
|
|
|
|
type TrainingRepository interface {
|
|
Create(training *Training) error
|
|
FindByID(id TrainingID) (*Training, error)
|
|
FindAll() ([]Training, error)
|
|
Update(training *Training) error
|
|
Delete(id TrainingID) error
|
|
}
|
|
|
|
type InMemoryTrainingRepository struct {
|
|
trainings map[TrainingID]Training
|
|
lock sync.RWMutex
|
|
}
|
|
|
|
func NewInMemoryTrainingRepository() *InMemoryTrainingRepository {
|
|
repo := &InMemoryTrainingRepository{
|
|
trainings: make(map[TrainingID]Training),
|
|
}
|
|
|
|
_ = repo.Create(&Training{
|
|
Name: "Kubernetes",
|
|
Days: 2,
|
|
Description: "Kubernetes",
|
|
Pricing: []TrainingPrice{
|
|
{
|
|
Amount: decimal.NewFromInt(450),
|
|
Currency: currency.USD,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(2000),
|
|
Currency: currency.USD,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(9900),
|
|
Currency: currency.CZK,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(49000),
|
|
Currency: currency.CZK,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
},
|
|
})
|
|
_ = repo.Create(&Training{
|
|
Name: "Terraform",
|
|
Days: 1,
|
|
Description: "Terraform",
|
|
Pricing: []TrainingPrice{
|
|
{
|
|
Amount: decimal.NewFromInt(200),
|
|
Currency: currency.USD,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(1000),
|
|
Currency: currency.USD,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(4900),
|
|
Currency: currency.CZK,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(24000),
|
|
Currency: currency.CZK,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
},
|
|
})
|
|
_ = repo.Create(&Training{
|
|
Name: "RKE2",
|
|
Days: 1,
|
|
Description: "RKE2",
|
|
Pricing: []TrainingPrice{
|
|
{
|
|
Amount: decimal.NewFromInt(200),
|
|
Currency: currency.USD,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(1000),
|
|
Currency: currency.USD,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(4900),
|
|
Currency: currency.CZK,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(24000),
|
|
Currency: currency.CZK,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
},
|
|
})
|
|
_ = repo.Create(&Training{
|
|
Name: "GitHub Actions",
|
|
Days: 1,
|
|
Description: "GitHub Actions",
|
|
Pricing: []TrainingPrice{
|
|
{
|
|
Amount: decimal.NewFromInt(200),
|
|
Currency: currency.USD,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(1000),
|
|
Currency: currency.USD,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(4900),
|
|
Currency: currency.CZK,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(24000),
|
|
Currency: currency.CZK,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
},
|
|
})
|
|
_ = repo.Create(&Training{
|
|
Name: "GitLab CI",
|
|
Days: 1,
|
|
Description: "GitLab CI",
|
|
Pricing: []TrainingPrice{
|
|
{
|
|
Amount: decimal.NewFromInt(200),
|
|
Currency: currency.USD,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(1000),
|
|
Currency: currency.USD,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(4900),
|
|
Currency: currency.CZK,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(24000),
|
|
Currency: currency.CZK,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
},
|
|
})
|
|
_ = repo.Create(&Training{
|
|
Name: "Prometheus",
|
|
Days: 2,
|
|
Description: "Prometheus",
|
|
Pricing: []TrainingPrice{
|
|
{
|
|
Amount: decimal.NewFromInt(450),
|
|
Currency: currency.USD,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(2000),
|
|
Currency: currency.USD,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(9900),
|
|
Currency: currency.CZK,
|
|
Type: OpenTrainingPrice,
|
|
},
|
|
{
|
|
Amount: decimal.NewFromInt(49000),
|
|
Currency: currency.CZK,
|
|
Type: CorporateTrainingPrice,
|
|
},
|
|
},
|
|
})
|
|
|
|
return repo
|
|
}
|
|
|
|
func (r *InMemoryTrainingRepository) Create(training *Training) error {
|
|
r.lock.Lock()
|
|
defer r.lock.Unlock()
|
|
|
|
training.ID = NewTrainingID()
|
|
r.trainings[training.ID] = *training
|
|
return nil
|
|
}
|
|
|
|
func (r *InMemoryTrainingRepository) FindByID(id TrainingID) (*Training, error) {
|
|
r.lock.RLock()
|
|
defer r.lock.RUnlock()
|
|
|
|
training, ok := r.trainings[id]
|
|
if !ok {
|
|
return nil, ErrTrainingNotFound
|
|
}
|
|
return &training, nil
|
|
}
|
|
|
|
func (r *InMemoryTrainingRepository) FindAll() ([]Training, error) {
|
|
r.lock.RLock()
|
|
defer r.lock.RUnlock()
|
|
|
|
trainings := make([]Training, 0, len(r.trainings))
|
|
for _, training := range r.trainings {
|
|
trainings = append(trainings, training)
|
|
}
|
|
return trainings, nil
|
|
}
|
|
|
|
func (r *InMemoryTrainingRepository) Update(training *Training) error {
|
|
r.lock.Lock()
|
|
defer r.lock.Unlock()
|
|
|
|
r.trainings[training.ID] = *training
|
|
return nil
|
|
}
|
|
|
|
func (r *InMemoryTrainingRepository) Delete(id TrainingID) error {
|
|
r.lock.Lock()
|
|
defer r.lock.Unlock()
|
|
|
|
_, ok := r.trainings[id]
|
|
if !ok {
|
|
return ErrTrainingNotFound
|
|
}
|
|
|
|
delete(r.trainings, id)
|
|
return nil
|
|
}
|