2022-10-02 01:01:39 +03:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LeaderboardRepository struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLeaderboardRepository(db *gorm.DB) *LeaderboardRepository {
|
|
|
|
return &LeaderboardRepository{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LeaderboardRepository) InsertBatch(items []*models.LeaderboardItem) error {
|
|
|
|
if err := r.db.
|
|
|
|
Clauses(clause.OnConflict{DoNothing: true}).
|
|
|
|
Create(&items).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:13:39 +03:00
|
|
|
func (r *LeaderboardRepository) CountAllByUser(userId string) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
err := r.db.
|
|
|
|
Table("leaderboard_items").
|
|
|
|
Where("user_id = ?", userId).
|
|
|
|
Count(&count).Error
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2022-10-19 19:28:30 +03:00
|
|
|
func (r *LeaderboardRepository) CountUsers() (int64, error) {
|
|
|
|
var count int64
|
|
|
|
err := r.db.
|
|
|
|
Table("leaderboard_items").
|
|
|
|
Distinct("user_id").
|
|
|
|
Count(&count).Error
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2022-10-20 09:33:12 +03:00
|
|
|
func (r *LeaderboardRepository) GetAllAggregatedByInterval(key *models.IntervalKey, by *uint8, limit, skip int) ([]*models.LeaderboardItemRanked, error) {
|
2022-10-02 01:01:39 +03:00
|
|
|
// TODO: distinct by (user, key) to filter out potential duplicates ?
|
2022-10-20 09:33:12 +03:00
|
|
|
|
|
|
|
var items []*models.LeaderboardItemRanked
|
2022-10-16 19:59:19 +03:00
|
|
|
subq := r.db.
|
|
|
|
Table("leaderboard_items").
|
2022-10-06 16:23:59 +03:00
|
|
|
Select("*, rank() over (partition by \"key\" order by total desc) as \"rank\"").
|
|
|
|
Where("\"interval\" in ?", *key)
|
2022-10-16 19:59:19 +03:00
|
|
|
subq = utils.WhereNullable(subq, "\"by\"", by)
|
|
|
|
|
|
|
|
q := r.db.Table("(?) as ranked", subq)
|
|
|
|
q = r.withPaging(q, limit, skip)
|
2022-10-02 01:01:39 +03:00
|
|
|
|
|
|
|
if err := q.Find(&items).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 09:33:12 +03:00
|
|
|
func (r *LeaderboardRepository) GetAggregatedByUserAndInterval(userId string, key *models.IntervalKey, by *uint8, limit, skip int) ([]*models.LeaderboardItemRanked, error) {
|
|
|
|
var items []*models.LeaderboardItemRanked
|
2022-10-16 19:59:19 +03:00
|
|
|
subq := r.db.
|
|
|
|
Table("leaderboard_items").
|
2022-10-06 16:23:59 +03:00
|
|
|
Select("*, rank() over (partition by \"key\" order by total desc) as \"rank\"").
|
|
|
|
Where("\"interval\" in ?", *key)
|
2022-10-16 19:59:19 +03:00
|
|
|
subq = utils.WhereNullable(subq, "\"by\"", by)
|
|
|
|
|
2022-10-19 19:28:30 +03:00
|
|
|
q := r.db.Table("(?) as ranked", subq).Where("user_id = ?", userId)
|
2022-10-16 19:59:19 +03:00
|
|
|
q = r.withPaging(q, limit, skip)
|
2022-10-02 01:01:39 +03:00
|
|
|
|
|
|
|
if err := q.Find(&items).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2022-10-06 15:52:06 +03:00
|
|
|
func (r *LeaderboardRepository) DeleteByUser(userId string) error {
|
|
|
|
if err := r.db.
|
|
|
|
Where("user_id = ?", userId).
|
|
|
|
Delete(models.LeaderboardItem{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 01:01:39 +03:00
|
|
|
func (r *LeaderboardRepository) DeleteByUserAndInterval(userId string, key *models.IntervalKey) error {
|
|
|
|
if err := r.db.
|
|
|
|
Where("user_id = ?", userId).
|
2022-10-06 16:23:59 +03:00
|
|
|
Where("\"interval\" in ?", *key).
|
2022-10-02 01:01:39 +03:00
|
|
|
Delete(models.LeaderboardItem{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-16 19:59:19 +03:00
|
|
|
|
|
|
|
func (r *LeaderboardRepository) withPaging(q *gorm.DB, limit, skip int) *gorm.DB {
|
|
|
|
if limit > 0 {
|
|
|
|
q = q.Where("\"rank\" <= ?", skip+limit)
|
|
|
|
}
|
|
|
|
if skip > 0 {
|
|
|
|
q = q.Where("\"rank\" > ?", skip)
|
|
|
|
}
|
|
|
|
return q
|
|
|
|
}
|