1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/services/heartbeat.go

57 lines
1.5 KiB
Go
Raw Normal View History

2019-05-05 23:36:49 +03:00
package services
import (
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/repositories"
"time"
2019-05-05 23:36:49 +03:00
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/models"
2019-05-05 23:36:49 +03:00
)
type HeartbeatService struct {
config *config.Config
repository repositories.IHeartbeatRepository
languageMappingSrvc ILanguageMappingService
2019-05-05 23:36:49 +03:00
}
func NewHeartbeatService(heartbeatRepo repositories.IHeartbeatRepository, languageMappingService ILanguageMappingService) *HeartbeatService {
return &HeartbeatService{
config: config.Get(),
repository: heartbeatRepo,
languageMappingSrvc: languageMappingService,
}
}
2020-02-20 16:28:55 +03:00
func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error {
return srv.repository.InsertBatch(heartbeats)
2019-05-05 23:36:49 +03:00
}
func (srv *HeartbeatService) GetAllWithin(from, to time.Time, user *models.User) ([]*models.Heartbeat, error) {
heartbeats, err := srv.repository.GetAllWithin(from, to, user)
if err != nil {
return nil, err
}
return srv.augmented(heartbeats, user.ID)
}
func (srv *HeartbeatService) GetFirstByUsers() ([]*models.TimeByUser, error) {
return srv.repository.GetFirstByUsers()
}
2020-03-09 19:30:23 +03:00
func (srv *HeartbeatService) DeleteBefore(t time.Time) error {
return srv.repository.DeleteBefore(t)
2020-03-09 19:30:23 +03:00
}
func (srv *HeartbeatService) augmented(heartbeats []*models.Heartbeat, userId string) ([]*models.Heartbeat, error) {
languageMapping, err := srv.languageMappingSrvc.ResolveByUser(userId)
if err != nil {
return nil, err
}
for i := range heartbeats {
heartbeats[i].Augment(languageMapping)
}
return heartbeats, nil
}