1
0
Fork 0

feat: add basic app

This commit is contained in:
Vojtěch Mareš 2024-10-10 21:29:08 +02:00
parent d4c1af4831
commit c94098afef
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D
13 changed files with 1850 additions and 0 deletions

View file

@ -0,0 +1,105 @@
package training
import (
"context"
"sync"
)
type InMemoryRepository struct {
m sync.Mutex
nextID ID
trainings map[ID]*Training
}
type InMemoryPricingRepository struct{}
func NewInMemoryRepository() *InMemoryRepository {
return &InMemoryRepository{
m: sync.Mutex{},
nextID: 1,
trainings: make(map[ID]*Training),
}
}
func (r *InMemoryRepository) FindAll(_ context.Context) ([]Training, error) {
r.m.Lock()
defer r.m.Unlock()
var trainings []Training
for _, t := range r.trainings {
trainings = append(trainings, *t)
}
return trainings, nil
}
func (r *InMemoryRepository) FindByID(_ context.Context, id ID) (*Training, error) {
r.m.Lock()
defer r.m.Unlock()
t, ok := r.trainings[id]
if !ok {
return nil, &ErrNotFound{}
}
return t, nil
}
func (r *InMemoryRepository) Create(_ context.Context, training *Training) error {
r.m.Lock()
defer r.m.Unlock()
training.id = r.nextID
r.nextID++
r.trainings[training.id] = training
return nil
}
func (r *InMemoryRepository) Update(_ context.Context, training *Training) error {
r.m.Lock()
defer r.m.Unlock()
if _, ok := r.trainings[training.id]; !ok {
return &ErrNotFound{}
}
r.trainings[training.id] = training
return nil
}
func (r *InMemoryRepository) Publish(_ context.Context, id ID) error {
r.m.Lock()
defer r.m.Unlock()
t, ok := r.trainings[id]
if !ok {
return &ErrNotFound{}
}
t.published = true
return nil
}
func (r *InMemoryRepository) Unpublish(_ context.Context, id ID) error {
r.m.Lock()
defer r.m.Unlock()
t, ok := r.trainings[id]
if !ok {
return &ErrNotFound{}
}
t.published = false
return nil
}
func (r *InMemoryRepository) Retire(_ context.Context, id ID) error {
r.m.Lock()
defer r.m.Unlock()
t, ok := r.trainings[id]
if !ok {
return &ErrNotFound{}
}
t.retired = true
return nil
}