2020-11-01 18:56:36 +03:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-01 22:14:10 +03:00
|
|
|
"gorm.io/gorm"
|
2020-11-01 18:56:36 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SummaryRepository struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSummaryRepository(db *gorm.DB) *SummaryRepository {
|
|
|
|
return &SummaryRepository{db: db}
|
|
|
|
}
|
|
|
|
|
2021-04-18 11:59:13 +03:00
|
|
|
func (r *SummaryRepository) GetAll() ([]*models.Summary, error) {
|
|
|
|
var summaries []*models.Summary
|
2021-04-19 21:36:37 +03:00
|
|
|
if err := r.db.
|
|
|
|
Order("from_time asc").
|
|
|
|
Preload("Projects", "type = ?", models.SummaryProject).
|
|
|
|
Preload("Languages", "type = ?", models.SummaryLanguage).
|
|
|
|
Preload("Editors", "type = ?", models.SummaryEditor).
|
|
|
|
Preload("OperatingSystems", "type = ?", models.SummaryOS).
|
|
|
|
Preload("Machines", "type = ?", models.SummaryMachine).
|
2022-01-02 15:39:20 +03:00
|
|
|
// branch summaries are currently not persisted, as only relevant in combination with project filter
|
2021-04-19 21:36:37 +03:00
|
|
|
Find(&summaries).Error; err != nil {
|
2021-04-18 11:59:13 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return summaries, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *SummaryRepository) Insert(summary *models.Summary) error {
|
|
|
|
if err := r.db.Create(summary).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SummaryRepository) GetByUserWithin(user *models.User, from, to time.Time) ([]*models.Summary, error) {
|
|
|
|
var summaries []*models.Summary
|
|
|
|
if err := r.db.
|
|
|
|
Where(&models.Summary{UserID: user.ID}).
|
2021-04-25 15:15:18 +03:00
|
|
|
Where("from_time >= ?", from.Local()).
|
|
|
|
Where("to_time <= ?", to.Local()).
|
2020-11-01 18:56:36 +03:00
|
|
|
Order("from_time asc").
|
|
|
|
Preload("Projects", "type = ?", models.SummaryProject).
|
|
|
|
Preload("Languages", "type = ?", models.SummaryLanguage).
|
|
|
|
Preload("Editors", "type = ?", models.SummaryEditor).
|
|
|
|
Preload("OperatingSystems", "type = ?", models.SummaryOS).
|
|
|
|
Preload("Machines", "type = ?", models.SummaryMachine).
|
2022-01-02 15:39:20 +03:00
|
|
|
// branch summaries are currently not persisted, as only relevant in combination with project filter
|
2020-11-01 18:56:36 +03:00
|
|
|
Find(&summaries).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return summaries, nil
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
func (r *SummaryRepository) GetLastByUser() ([]*models.TimeByUser, error) {
|
|
|
|
var result []*models.TimeByUser
|
|
|
|
r.db.Model(&models.User{}).
|
|
|
|
Select("users.id as user, max(to_time) as time").
|
|
|
|
Joins("left join summaries on users.id = summaries.user_id").
|
|
|
|
Group("user").
|
|
|
|
Scan(&result)
|
|
|
|
return result, nil
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
2020-11-06 19:09:41 +03:00
|
|
|
|
|
|
|
func (r *SummaryRepository) DeleteByUser(userId string) error {
|
|
|
|
if err := r.db.
|
|
|
|
Where("user_id = ?", userId).
|
|
|
|
Delete(models.Summary{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-06 19:20:26 +03:00
|
|
|
}
|