go-first/internal/service/skill_service.go

107 lines
3.6 KiB
Go
Raw Normal View History

2026-10-07 15:21:47 +02:00
package service
import (
"context"
"sync"
"github.com/dnoulet/go-first/internal/model"
"github.com/google/uuid"
)
type SkillService struct {
mu sync.RWMutex
skills map[uuid.UUID]*model.Skill
}
func NewSkillService() *SkillService {
s := &SkillService{
skills: make(map[uuid.UUID]*model.Skill),
}
s.seedData()
return s
}
func (s *SkillService) seedData() {
skills := []*model.Skill{
{ID: uuid.New(), Name: "Go", Category: "Backend", Proficiency: 5, Icon: "go", Order: 1},
{ID: uuid.New(), Name: "Java", Category: "Backend", Proficiency: 5, Icon: "java", Order: 2},
{ID: uuid.New(), Name: "C#", Category: "Backend", Proficiency: 4, Icon: "csharp", Order: 3},
{ID: uuid.New(), Name: "Python", Category: "Backend", Proficiency: 4, Icon: "python", Order: 4},
{ID: uuid.New(), Name: "Ruby", Category: "Backend", Proficiency: 3, Icon: "ruby", Order: 5},
{ID: uuid.New(), Name: "Kotlin", Category: "Backend", Proficiency: 4, Icon: "kotlin", Order: 6},
{ID: uuid.New(), Name: "REST APIs", Category: "Architecture", Proficiency: 5, Icon: "api", Order: 7},
{ID: uuid.New(), Name: "Microservices", Category: "Architecture", Proficiency: 5, Icon: "microservices", Order: 8},
{ID: uuid.New(), Name: "System Design", Category: "Architecture", Proficiency: 5, Icon: "architecture", Order: 9},
{ID: uuid.New(), Name: "Infrastructure", Category: "DevOps", Proficiency: 4, Icon: "infrastructure", Order: 10},
{ID: uuid.New(), Name: "Docker", Category: "DevOps", Proficiency: 5, Icon: "docker", Order: 11},
{ID: uuid.New(), Name: "Kubernetes", Category: "DevOps", Proficiency: 4, Icon: "kubernetes", Order: 12},
{ID: uuid.New(), Name: "React", Category: "Frontend", Proficiency: 4, Icon: "react", Order: 13},
{ID: uuid.New(), Name: "TypeScript", Category: "Frontend", Proficiency: 4, Icon: "typescript", Order: 14},
{ID: uuid.New(), Name: "PostgreSQL", Category: "Database", Proficiency: 5, Icon: "postgresql", Order: 15},
{ID: uuid.New(), Name: "Redis", Category: "Database", Proficiency: 4, Icon: "redis", Order: 16},
{ID: uuid.New(), Name: "MongoDB", Category: "Database", Proficiency: 3, Icon: "mongodb", Order: 17},
{ID: uuid.New(), Name: "GraphQL", Category: "API", Proficiency: 4, Icon: "graphql", Order: 18},
{ID: uuid.New(), Name: "gRPC", Category: "API", Proficiency: 4, Icon: "grpc", Order: 19},
{ID: uuid.New(), Name: "CI/CD", Category: "DevOps", Proficiency: 5, Icon: "cicd", Order: 20},
}
for _, skill := range skills {
s.skills[skill.ID] = skill
}
}
func (s *SkillService) GetAll(ctx context.Context, category string) ([]*model.Skill, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var result []*model.Skill
for _, skill := range s.skills {
if category == "" || skill.Category == category {
result = append(result, skill)
}
}
return result, nil
}
func (s *SkillService) GetByID(ctx context.Context, id uuid.UUID) (*model.Skill, error) {
s.mu.RLock()
defer s.mu.RUnlock()
skill, ok := s.skills[id]
if !ok {
return nil, ErrNotFound
}
return skill, nil
}
func (s *SkillService) Create(ctx context.Context, skill *model.Skill) (*model.Skill, error) {
s.mu.Lock()
defer s.mu.Unlock()
skill.ID = uuid.New()
s.skills[skill.ID] = skill
return skill, nil
}
func (s *SkillService) Update(ctx context.Context, skill *model.Skill) (*model.Skill, error) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.skills[skill.ID]; !ok {
return nil, ErrNotFound
}
s.skills[skill.ID] = skill
return skill, nil
}
func (s *SkillService) Delete(ctx context.Context, id uuid.UUID) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.skills[id]; !ok {
return ErrNotFound
}
delete(s.skills, id)
return nil
}