2021-06-11 21:59:34 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-08-06 18:07:36 +03:00
|
|
|
"github.com/leandro-lugaresi/hub"
|
2021-06-11 21:59:34 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/repositories"
|
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectLabelService struct {
|
|
|
|
config *config.Config
|
|
|
|
cache *cache.Cache
|
2021-08-06 18:07:36 +03:00
|
|
|
eventBus *hub.Hub
|
2021-06-11 21:59:34 +03:00
|
|
|
repository repositories.IProjectLabelRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectLabelService(projectLabelRepository repositories.IProjectLabelRepository) *ProjectLabelService {
|
|
|
|
return &ProjectLabelService{
|
|
|
|
config: config.Get(),
|
2021-08-06 18:07:36 +03:00
|
|
|
eventBus: config.EventBus(),
|
2021-06-11 21:59:34 +03:00
|
|
|
repository: projectLabelRepository,
|
|
|
|
cache: cache.New(24*time.Hour, 24*time.Hour),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *ProjectLabelService) GetById(id uint) (*models.ProjectLabel, error) {
|
|
|
|
return srv.repository.GetById(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *ProjectLabelService) GetByUser(userId string) ([]*models.ProjectLabel, error) {
|
|
|
|
if labels, found := srv.cache.Get(userId); found {
|
|
|
|
return labels.([]*models.ProjectLabel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
labels, err := srv.repository.GetByUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv.cache.Set(userId, labels, cache.DefaultExpiration)
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
2021-12-26 19:23:37 +03:00
|
|
|
// GetByUserGrouped returns lists of project labels, grouped by their project key
|
2021-06-12 11:44:19 +03:00
|
|
|
func (srv *ProjectLabelService) GetByUserGrouped(userId string) (map[string][]*models.ProjectLabel, error) {
|
2021-10-13 18:12:55 +03:00
|
|
|
labelsByProject := make(map[string][]*models.ProjectLabel)
|
2021-06-11 21:59:34 +03:00
|
|
|
userLabels, err := srv.GetByUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-12 11:44:19 +03:00
|
|
|
for _, l := range userLabels {
|
2021-10-13 18:12:55 +03:00
|
|
|
if _, ok := labelsByProject[l.ProjectKey]; !ok {
|
|
|
|
labelsByProject[l.ProjectKey] = []*models.ProjectLabel{l}
|
2021-06-12 11:44:19 +03:00
|
|
|
} else {
|
2021-10-13 18:12:55 +03:00
|
|
|
labelsByProject[l.ProjectKey] = append(labelsByProject[l.ProjectKey], l)
|
2021-06-12 11:44:19 +03:00
|
|
|
}
|
2021-06-11 21:59:34 +03:00
|
|
|
}
|
2021-10-13 18:12:55 +03:00
|
|
|
return labelsByProject, nil
|
|
|
|
}
|
2021-06-12 11:44:19 +03:00
|
|
|
|
2021-12-26 19:23:37 +03:00
|
|
|
// GetByUserGroupedInverted returns lists of project labels, grouped by their label key
|
2021-10-13 18:12:55 +03:00
|
|
|
func (srv *ProjectLabelService) GetByUserGroupedInverted(userId string) (map[string][]*models.ProjectLabel, error) {
|
|
|
|
projectsByLabel := make(map[string][]*models.ProjectLabel)
|
|
|
|
userLabels, err := srv.GetByUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range userLabels {
|
|
|
|
if _, ok := projectsByLabel[l.Label]; !ok {
|
|
|
|
projectsByLabel[l.Label] = []*models.ProjectLabel{l}
|
|
|
|
} else {
|
|
|
|
projectsByLabel[l.Label] = append(projectsByLabel[l.Label], l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return projectsByLabel, nil
|
2021-06-11 21:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *ProjectLabelService) Create(label *models.ProjectLabel) (*models.ProjectLabel, error) {
|
|
|
|
result, err := srv.repository.Insert(label)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.cache.Delete(result.UserID)
|
2021-08-06 18:07:36 +03:00
|
|
|
srv.notifyUpdate(label, false)
|
2021-06-11 21:59:34 +03:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *ProjectLabelService) Delete(label *models.ProjectLabel) error {
|
|
|
|
if label.UserID == "" {
|
|
|
|
return errors.New("no user id specified")
|
|
|
|
}
|
|
|
|
err := srv.repository.Delete(label.ID)
|
|
|
|
srv.cache.Delete(label.UserID)
|
2021-08-06 18:07:36 +03:00
|
|
|
srv.notifyUpdate(label, true)
|
2021-06-11 21:59:34 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-08-06 18:07:36 +03:00
|
|
|
|
|
|
|
func (srv *ProjectLabelService) notifyUpdate(label *models.ProjectLabel, isDelete bool) {
|
|
|
|
name := config.EventProjectLabelCreate
|
|
|
|
if isDelete {
|
|
|
|
name = config.EventProjectLabelDelete
|
|
|
|
}
|
|
|
|
srv.eventBus.Publish(hub.Message{
|
|
|
|
Name: name,
|
|
|
|
Fields: map[string]interface{}{config.FieldPayload: label, config.FieldUserId: label.UserID},
|
|
|
|
})
|
|
|
|
}
|