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

106 lines
2.6 KiB
Go
Raw Normal View History

2019-05-06 01:40:41 +03:00
package services
import (
"github.com/muety/wakapi/config"
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/repositories"
"github.com/muety/wakapi/utils"
"github.com/patrickmn/go-cache"
2020-05-24 17:34:32 +03:00
uuid "github.com/satori/go.uuid"
"time"
2019-05-06 01:40:41 +03:00
)
type UserService struct {
Config *config.Config
cache *cache.Cache
repository repositories.IUserRepository
2019-05-06 01:40:41 +03:00
}
func NewUserService(userRepo repositories.IUserRepository) *UserService {
return &UserService{
Config: config.Get(),
repository: userRepo,
cache: cache.New(1*time.Hour, 2*time.Hour),
}
}
2020-02-20 16:28:55 +03:00
2019-05-11 18:49:56 +03:00
func (srv *UserService) GetUserById(userId string) (*models.User, error) {
if u, ok := srv.cache.Get(userId); ok {
return u.(*models.User), nil
}
u, err := srv.repository.GetById(userId)
if err != nil {
return nil, err
}
srv.cache.Set(u.ID, u, cache.DefaultExpiration)
return u, nil
2019-05-06 01:40:41 +03:00
}
2019-05-11 18:49:56 +03:00
func (srv *UserService) GetUserByKey(key string) (*models.User, error) {
if u, ok := srv.cache.Get(key); ok {
return u.(*models.User), nil
}
u, err := srv.repository.GetByApiKey(key)
if err != nil {
return nil, err
}
srv.cache.Set(u.ID, u, cache.DefaultExpiration)
return u, nil
2019-05-06 01:40:41 +03:00
}
func (srv *UserService) GetAll() ([]*models.User, error) {
return srv.repository.GetAll()
}
2020-05-24 17:34:32 +03:00
func (srv *UserService) CreateOrGet(signup *models.Signup) (*models.User, bool, error) {
u := &models.User{
ID: signup.Username,
ApiKey: uuid.NewV4().String(),
Password: signup.Password,
}
2020-10-16 17:11:14 +03:00
if hash, err := utils.HashBcrypt(u.Password, srv.Config.Security.PasswordSalt); err != nil {
return nil, false, err
2020-10-16 17:11:14 +03:00
} else {
u.Password = hash
2020-05-24 17:34:32 +03:00
}
return srv.repository.InsertOrGet(u)
2020-05-24 17:34:32 +03:00
}
func (srv *UserService) Update(user *models.User) (*models.User, error) {
srv.cache.Flush()
return srv.repository.Update(user)
}
func (srv *UserService) ResetApiKey(user *models.User) (*models.User, error) {
srv.cache.Flush()
user.ApiKey = uuid.NewV4().String()
return srv.Update(user)
}
func (srv *UserService) ToggleBadges(user *models.User) (*models.User, error) {
srv.cache.Flush()
return srv.repository.UpdateField(user, "badges_enabled", !user.BadgesEnabled)
}
func (srv *UserService) SetWakatimeApiKey(user *models.User, apiKey string) (*models.User, error) {
srv.cache.Flush()
return srv.repository.UpdateField(user, "wakatime_api_key", apiKey)
}
func (srv *UserService) MigrateMd5Password(user *models.User, login *models.Login) (*models.User, error) {
srv.cache.Flush()
user.Password = login.Password
2020-10-16 17:11:14 +03:00
if hash, err := utils.HashBcrypt(user.Password, srv.Config.Security.PasswordSalt); err != nil {
return nil, err
2020-10-16 17:11:14 +03:00
} else {
user.Password = hash
}
return srv.repository.UpdateField(user, "password", user.Password)
}