mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
refactor: define interface types for all services and repositories
This commit is contained in:
@@ -16,12 +16,12 @@ const (
|
||||
|
||||
type AggregationService struct {
|
||||
config *config.Config
|
||||
userService *UserService
|
||||
summaryService *SummaryService
|
||||
heartbeatService *HeartbeatService
|
||||
userService IUserService
|
||||
summaryService ISummaryService
|
||||
heartbeatService IHeartbeatService
|
||||
}
|
||||
|
||||
func NewAggregationService(userService *UserService, summaryService *SummaryService, heartbeatService *HeartbeatService) *AggregationService {
|
||||
func NewAggregationService(userService IUserService, summaryService ISummaryService, heartbeatService IHeartbeatService) *AggregationService {
|
||||
return &AggregationService{
|
||||
config: config.Get(),
|
||||
userService: userService,
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
|
||||
type AliasService struct {
|
||||
config *config.Config
|
||||
repository *repositories.AliasRepository
|
||||
repository repositories.IAliasRepository
|
||||
}
|
||||
|
||||
func NewAliasService(aliasRepo *repositories.AliasRepository) *AliasService {
|
||||
func NewAliasService(aliasRepo repositories.IAliasRepository) *AliasService {
|
||||
return &AliasService{
|
||||
config: config.Get(),
|
||||
repository: aliasRepo,
|
||||
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
|
||||
type HeartbeatService struct {
|
||||
config *config.Config
|
||||
repository *repositories.HeartbeatRepository
|
||||
languageMappingSrvc *LanguageMappingService
|
||||
repository repositories.IHeartbeatRepository
|
||||
languageMappingSrvc ILanguageMappingService
|
||||
}
|
||||
|
||||
func NewHeartbeatService(heartbeatRepo *repositories.HeartbeatRepository, languageMappingService *LanguageMappingService) *HeartbeatService {
|
||||
func NewHeartbeatService(heartbeatRepo repositories.IHeartbeatRepository, languageMappingService ILanguageMappingService) *HeartbeatService {
|
||||
return &HeartbeatService{
|
||||
config: config.Get(),
|
||||
repository: heartbeatRepo,
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
|
||||
type KeyValueService struct {
|
||||
config *config.Config
|
||||
repository *repositories.KeyValueRepository
|
||||
repository repositories.IKeyValueRepository
|
||||
}
|
||||
|
||||
func NewKeyValueService(keyValueRepo *repositories.KeyValueRepository) *KeyValueService {
|
||||
func NewKeyValueService(keyValueRepo repositories.IKeyValueRepository) *KeyValueService {
|
||||
return &KeyValueService{
|
||||
config: config.Get(),
|
||||
repository: keyValueRepo,
|
||||
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
|
||||
type LanguageMappingService struct {
|
||||
config *config.Config
|
||||
repository *repositories.LanguageMappingRepository
|
||||
cache *cache.Cache
|
||||
repository repositories.ILanguageMappingRepository
|
||||
}
|
||||
|
||||
func NewLanguageMappingService(languageMappingsRepo *repositories.LanguageMappingRepository) *LanguageMappingService {
|
||||
func NewLanguageMappingService(languageMappingsRepo repositories.ILanguageMappingRepository) *LanguageMappingService {
|
||||
return &LanguageMappingService{
|
||||
config: config.Get(),
|
||||
repository: languageMappingsRepo,
|
||||
|
||||
58
services/services.go
Normal file
58
services/services.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/muety/wakapi/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IAggregationService interface {
|
||||
Schedule()
|
||||
Run(map[string]bool) error
|
||||
}
|
||||
|
||||
type IAliasService interface {
|
||||
LoadUserAliases(string) error
|
||||
GetAliasOrDefault(string, uint8, string) (string, error)
|
||||
IsInitialized(string) bool
|
||||
}
|
||||
|
||||
type IHeartbeatService interface {
|
||||
InsertBatch([]*models.Heartbeat) error
|
||||
GetAllWithin(time.Time, time.Time, *models.User) ([]*models.Heartbeat, error)
|
||||
GetFirstByUsers() ([]*models.TimeByUser, error)
|
||||
DeleteBefore(time.Time) error
|
||||
}
|
||||
|
||||
type IKeyValueService interface {
|
||||
GetString(string) (*models.KeyStringValue, error)
|
||||
PutString(*models.KeyStringValue) error
|
||||
DeleteString(string) error
|
||||
}
|
||||
|
||||
type ILanguageMappingService interface {
|
||||
GetById(uint) (*models.LanguageMapping, error)
|
||||
GetByUser(string) ([]*models.LanguageMapping, error)
|
||||
ResolveByUser(string) (map[string]string, error)
|
||||
Create(*models.LanguageMapping) (*models.LanguageMapping, error)
|
||||
Delete(mapping *models.LanguageMapping) error
|
||||
}
|
||||
|
||||
type ISummaryService interface {
|
||||
Aliased(time.Time, time.Time, *models.User, SummaryRetriever) (*models.Summary, error)
|
||||
Retrieve(time.Time, time.Time, *models.User) (*models.Summary, error)
|
||||
Summarize(time.Time, time.Time, *models.User) (*models.Summary, error)
|
||||
GetLatestByUser() ([]*models.TimeByUser, error)
|
||||
DeleteByUser(string) error
|
||||
Insert(*models.Summary) error
|
||||
}
|
||||
|
||||
type IUserService interface {
|
||||
GetUserById(string) (*models.User, error)
|
||||
GetUserByKey(string) (*models.User, error)
|
||||
GetAll() ([]*models.User, error)
|
||||
CreateOrGet(*models.Signup) (*models.User, bool, error)
|
||||
Update(*models.User) (*models.User, error)
|
||||
ResetApiKey(*models.User) (*models.User, error)
|
||||
ToggleBadges(*models.User) (*models.User, error)
|
||||
MigrateMd5Password(*models.User, *models.Login) (*models.User, error)
|
||||
}
|
||||
@@ -17,14 +17,14 @@ const HeartbeatDiffThreshold = 2 * time.Minute
|
||||
type SummaryService struct {
|
||||
config *config.Config
|
||||
cache *cache.Cache
|
||||
repository *repositories.SummaryRepository
|
||||
heartbeatService *HeartbeatService
|
||||
aliasService *AliasService
|
||||
repository repositories.ISummaryRepository
|
||||
heartbeatService IHeartbeatService
|
||||
aliasService IAliasService
|
||||
}
|
||||
|
||||
type SummaryRetriever func(f, t time.Time, u *models.User) (*models.Summary, error)
|
||||
|
||||
func NewSummaryService(summaryRepo *repositories.SummaryRepository, heartbeatService *HeartbeatService, aliasService *AliasService) *SummaryService {
|
||||
func NewSummaryService(summaryRepo repositories.ISummaryRepository, heartbeatService IHeartbeatService, aliasService IAliasService) *SummaryService {
|
||||
return &SummaryService{
|
||||
config: config.Get(),
|
||||
cache: cache.New(24*time.Hour, 24*time.Hour),
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
|
||||
type UserService struct {
|
||||
Config *config.Config
|
||||
repository *repositories.UserRepository
|
||||
repository repositories.IUserRepository
|
||||
}
|
||||
|
||||
func NewUserService(userRepo *repositories.UserRepository) *UserService {
|
||||
func NewUserService(userRepo repositories.IUserRepository) *UserService {
|
||||
return &UserService{
|
||||
Config: config.Get(),
|
||||
repository: userRepo,
|
||||
|
||||
Reference in New Issue
Block a user