2020-11-01 18:56:36 +03:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-01 22:14:10 +03:00
|
|
|
"gorm.io/gorm"
|
2021-02-13 01:06:48 +03:00
|
|
|
"time"
|
2020-11-01 18:56:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserRepository struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserRepository(db *gorm.DB) *UserRepository {
|
|
|
|
return &UserRepository{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *UserRepository) GetById(userId string) (*models.User, error) {
|
|
|
|
u := &models.User{}
|
|
|
|
if err := r.db.Where(&models.User{ID: userId}).First(u).Error; err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2021-02-13 13:23:58 +03:00
|
|
|
func (r *UserRepository) GetByIds(userIds []string) ([]*models.User, error) {
|
|
|
|
var users []*models.User
|
|
|
|
if err := r.db.
|
|
|
|
Model(&models.User{}).
|
|
|
|
Where("id in ?", userIds).
|
|
|
|
Find(&users).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *UserRepository) GetByApiKey(key string) (*models.User, error) {
|
2021-10-11 12:07:04 +03:00
|
|
|
if key == "" {
|
|
|
|
return nil, errors.New("invalid input")
|
|
|
|
}
|
2020-11-01 18:56:36 +03:00
|
|
|
u := &models.User{}
|
|
|
|
if err := r.db.Where(&models.User{ApiKey: key}).First(u).Error; err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 23:57:57 +03:00
|
|
|
func (r *UserRepository) GetByResetToken(resetToken string) (*models.User, error) {
|
|
|
|
if resetToken == "" {
|
|
|
|
return nil, errors.New("invalid input")
|
|
|
|
}
|
|
|
|
u := &models.User{}
|
|
|
|
if err := r.db.Where(&models.User{ResetToken: resetToken}).First(u).Error; err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *UserRepository) GetByEmail(email string) (*models.User, error) {
|
|
|
|
if email == "" {
|
|
|
|
return nil, errors.New("invalid input")
|
|
|
|
}
|
|
|
|
u := &models.User{}
|
|
|
|
if err := r.db.Where(&models.User{Email: email}).First(u).Error; err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *UserRepository) GetAll() ([]*models.User, error) {
|
|
|
|
var users []*models.User
|
|
|
|
if err := r.db.
|
2021-02-03 00:54:22 +03:00
|
|
|
Where(&models.User{}).
|
2020-11-01 18:56:36 +03:00
|
|
|
Find(&users).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2022-10-04 00:52:22 +03:00
|
|
|
func (r *UserRepository) GetMany(ids []string) ([]*models.User, error) {
|
|
|
|
var users []*models.User
|
|
|
|
if err := r.db.
|
|
|
|
Table("users").
|
|
|
|
Where("id in ?", ids).
|
|
|
|
Find(&users).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:07:14 +03:00
|
|
|
func (r *UserRepository) GetAllByReports(reportsEnabled bool) ([]*models.User, error) {
|
|
|
|
var users []*models.User
|
|
|
|
if err := r.db.Where(&models.User{ReportsWeekly: reportsEnabled}).Find(&users).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 01:01:39 +03:00
|
|
|
func (r *UserRepository) GetAllByLeaderboard(leaderboardEnabled bool) ([]*models.User, error) {
|
|
|
|
var users []*models.User
|
|
|
|
if err := r.db.Where(&models.User{PublicLeaderboard: leaderboardEnabled}).Find(&users).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:06:48 +03:00
|
|
|
func (r *UserRepository) GetByLoggedInAfter(t time.Time) ([]*models.User, error) {
|
|
|
|
var users []*models.User
|
|
|
|
if err := r.db.
|
2021-04-25 15:15:18 +03:00
|
|
|
Where("last_logged_in_at >= ?", t.Local()).
|
2021-02-13 01:06:48 +03:00
|
|
|
Find(&users).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2021-02-13 13:23:58 +03:00
|
|
|
// Returns a list of user ids, whose last heartbeat is not older than t
|
|
|
|
// NOTE: Only ID field will be populated
|
|
|
|
func (r *UserRepository) GetByLastActiveAfter(t time.Time) ([]*models.User, error) {
|
2022-03-19 12:30:32 +03:00
|
|
|
subQuery1 := r.db.Model(&models.Heartbeat{}).
|
|
|
|
Select("user_id as user, max(time) as time").
|
|
|
|
Group("user_id")
|
2021-02-13 13:23:58 +03:00
|
|
|
|
|
|
|
var userIds []string
|
|
|
|
if err := r.db.
|
|
|
|
Select("user as id").
|
|
|
|
Table("(?) as q", subQuery1).
|
2021-04-25 15:15:18 +03:00
|
|
|
Where("time >= ?", t.Local()).
|
2021-02-13 13:23:58 +03:00
|
|
|
Scan(&userIds).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.GetByIds(userIds)
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:49:47 +03:00
|
|
|
func (r *UserRepository) Count() (int64, error) {
|
|
|
|
var count int64
|
|
|
|
if err := r.db.
|
|
|
|
Model(&models.User{}).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *UserRepository) InsertOrGet(user *models.User) (*models.User, bool, error) {
|
2021-12-15 14:52:24 +03:00
|
|
|
if u, err := r.GetById(user.ID); err == nil && u != nil && u.ID != "" {
|
|
|
|
return u, false, nil
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
|
|
|
|
2021-12-15 14:52:24 +03:00
|
|
|
result := r.db.Create(user)
|
|
|
|
if err := result.Error; err != nil {
|
|
|
|
return nil, false, err
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
|
|
|
|
2021-12-15 14:52:24 +03:00
|
|
|
return user, true, nil
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *UserRepository) Update(user *models.User) (*models.User, error) {
|
2021-02-07 00:32:03 +03:00
|
|
|
updateMap := map[string]interface{}{
|
|
|
|
"api_key": user.ApiKey,
|
|
|
|
"password": user.Password,
|
2021-02-21 15:02:11 +03:00
|
|
|
"email": user.Email,
|
2021-02-07 00:32:03 +03:00
|
|
|
"last_logged_in_at": user.LastLoggedInAt,
|
|
|
|
"share_data_max_days": user.ShareDataMaxDays,
|
|
|
|
"share_editors": user.ShareEditors,
|
|
|
|
"share_languages": user.ShareLanguages,
|
|
|
|
"share_oss": user.ShareOSs,
|
|
|
|
"share_projects": user.ShareProjects,
|
|
|
|
"share_machines": user.ShareMachines,
|
2021-06-12 10:12:28 +03:00
|
|
|
"share_labels": user.ShareLabels,
|
2021-02-07 00:32:03 +03:00
|
|
|
"wakatime_api_key": user.WakatimeApiKey,
|
2022-01-21 14:35:05 +03:00
|
|
|
"wakatime_api_url": user.WakatimeApiUrl,
|
2021-02-13 14:59:59 +03:00
|
|
|
"has_data": user.HasData,
|
2021-04-10 01:07:13 +03:00
|
|
|
"reset_token": user.ResetToken,
|
2021-04-25 15:15:18 +03:00
|
|
|
"location": user.Location,
|
2021-04-30 15:07:14 +03:00
|
|
|
"reports_weekly": user.ReportsWeekly,
|
2022-10-06 15:47:22 +03:00
|
|
|
"public_leaderboard": user.PublicLeaderboard,
|
2021-02-07 00:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result := r.db.Model(user).Updates(updateMap)
|
2020-11-01 18:56:36 +03:00
|
|
|
if err := result.Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *UserRepository) UpdateField(user *models.User, key string, value interface{}) (*models.User, error) {
|
|
|
|
result := r.db.Model(user).Update(key, value)
|
|
|
|
if err := result.Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.RowsAffected != 1 {
|
|
|
|
return nil, errors.New("nothing updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
2021-02-03 00:54:22 +03:00
|
|
|
|
|
|
|
func (r *UserRepository) Delete(user *models.User) error {
|
|
|
|
return r.db.Delete(user).Error
|
|
|
|
}
|