serverctl/services/entry/pkg/application/projects/service.go

62 lines
1.4 KiB
Go
Raw Normal View History

2022-02-14 01:25:36 +01:00
package projects
import (
"context"
"fmt"
"github.com/eko/gocache/cache"
"go.uber.org/zap"
)
type Service struct {
projectsRepository Repository
logger *zap.Logger
2022-02-14 01:42:18 +01:00
cache *cache.MetricCache
2022-02-14 01:25:36 +01:00
}
2022-02-14 01:42:18 +01:00
func NewService(logger *zap.Logger, projectsRepository Repository, cache *cache.MetricCache) *Service {
2022-02-14 01:25:36 +01:00
return &Service{
logger: logger,
projectsRepository: projectsRepository,
cache: cache}
}
func (s *Service) CreateProject(ctx context.Context, userId int, name string) (int, error) {
s.logger.Debug("creating project",
zap.String("name", name),
zap.Int("creatorId", userId))
projectId, err := s.projectsRepository.Create(ctx, NewCreateProject(name, userId))
if err != nil {
s.logger.Warn(err.Error())
return -1, err
}
_ = s.cache.Delete(fmt.Sprintf("projects_userId_%d", userId))
return projectId, nil
}
func (s *Service) Get(ctx context.Context, userId int) ([]*Project, error) {
s.logger.Debug("getting projects",
zap.Int("userId", userId))
loadFunc := func(key interface{}) (interface{}, error) {
s.logger.Debug("getting projects from repository",
zap.Int("userId", userId))
return s.projectsRepository.GetForMemberId(ctx, userId)
}
cacheEntry := cache.NewLoadable(
loadFunc,
s.cache)
entry, err := cacheEntry.Get(fmt.Sprintf("projects_userId_%d", userId))
if err != nil {
return nil, err
}
2022-02-14 22:19:44 +01:00
projects := entry.([]*Project)
return projects, nil
2022-02-14 01:25:36 +01:00
}