2019-05-06 01:40:41 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2020-05-25 23:24:29 +03:00
|
|
|
"errors"
|
2019-05-11 18:49:56 +03:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-05-25 22:42:45 +03:00
|
|
|
"github.com/muety/wakapi/utils"
|
2020-05-24 17:34:32 +03:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-05-06 01:40:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserService struct {
|
2019-05-16 23:53:03 +03:00
|
|
|
Config *models.Config
|
|
|
|
Db *gorm.DB
|
2019-05-06 01:40:41 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 18:32:26 +03:00
|
|
|
func NewUserService(db *gorm.DB) *UserService {
|
|
|
|
return &UserService{
|
|
|
|
Config: models.GetConfig(),
|
|
|
|
Db: db,
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
u := &models.User{}
|
|
|
|
if err := srv.Db.Where(&models.User{ID: userId}).First(u).Error; err != nil {
|
2019-05-06 01:40:41 +03:00
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2019-05-11 18:49:56 +03:00
|
|
|
func (srv *UserService) GetUserByKey(key string) (*models.User, error) {
|
|
|
|
u := &models.User{}
|
|
|
|
if err := srv.Db.Where(&models.User{ApiKey: key}).First(u).Error; err != nil {
|
2019-05-06 01:40:41 +03:00
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
2019-10-10 00:26:28 +03:00
|
|
|
|
|
|
|
func (srv *UserService) GetAll() ([]*models.User, error) {
|
|
|
|
var users []*models.User
|
|
|
|
if err := srv.Db.
|
|
|
|
Table("users").
|
|
|
|
Find(&users).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
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,
|
2020-05-25 22:42:45 +03:00
|
|
|
ApiKey: uuid.NewV4().String(),
|
|
|
|
Password: signup.Password,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := utils.HashPassword(u, srv.Config.PasswordSalt); err != nil {
|
|
|
|
return nil, false, err
|
2020-05-24 17:34:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result := srv.Db.FirstOrCreate(u, &models.User{ID: u.ID})
|
|
|
|
if err := result.Error; err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.RowsAffected == 1 {
|
|
|
|
return u, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, false, nil
|
|
|
|
}
|
2020-05-25 23:24:29 +03:00
|
|
|
|
2020-05-30 23:19:05 +03:00
|
|
|
func (srv *UserService) Update(user *models.User) (*models.User, error) {
|
|
|
|
result := srv.Db.Model(&models.User{}).Updates(user)
|
|
|
|
if err := result.Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.RowsAffected != 1 {
|
|
|
|
return nil, errors.New("nothing updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:58:06 +03:00
|
|
|
func (srv *UserService) ResetApiKey(user *models.User) (*models.User, error) {
|
|
|
|
user.ApiKey = uuid.NewV4().String()
|
|
|
|
return srv.Update(user)
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:24:29 +03:00
|
|
|
func (srv *UserService) MigrateMd5Password(user *models.User, login *models.Login) (*models.User, error) {
|
|
|
|
user.Password = login.Password
|
|
|
|
if err := utils.HashPassword(user, srv.Config.PasswordSalt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := srv.Db.Model(user).Update("password", user.Password)
|
|
|
|
if err := result.Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if result.RowsAffected < 1 {
|
|
|
|
return nil, errors.New("nothing changes")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|