modified: serv_nginx/api_bb/internal/repository/user_stats_repository.go
new file: serv_nginx/api_bb/internal/scripts/migrate_existing_users.go modified: serv_nginx/api_bb/internal/service/auth_service.go modified: serv_nginx/api_bb/internal/service/user_stats_service.go modified: serv_nginx/bbvue/src/views/Achievements.vue modified: serv_nginx/bbvue/src/views/Reviews.vue modified: serv_nginx/bbvue/src/views/Training.vue fix bag with no stats into table
This commit is contained in:
@@ -4,9 +4,9 @@ package repository
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"api_bb/internal/models"
|
||||
"api_bb/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserStatsRepository interface {
|
||||
@@ -21,6 +21,7 @@ type UserStatsRepository interface {
|
||||
IncrementWorkouts(userID uint, distance float64, duration int) error
|
||||
UpdatePersonalBest(userID uint, distanceType string, time string) error
|
||||
GetUserStatsResponse(userID uint) (*models.UserStatsResponse, error)
|
||||
GetByUserIDOrCreate(userID uint) (*models.UserStats, error)
|
||||
}
|
||||
|
||||
type userStatsRepository struct {
|
||||
@@ -31,6 +32,39 @@ func NewUserStatsRepository(db *gorm.DB) UserStatsRepository {
|
||||
return &userStatsRepository{db: db}
|
||||
}
|
||||
|
||||
// GetByUserIDOrCreate возвращает статистику по ID пользователя или создает новую
|
||||
func (r *userStatsRepository) GetByUserIDOrCreate(userID uint) (*models.UserStats, error) {
|
||||
userStats, err := r.GetByUserID(userID)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
// Создаем новую статистику
|
||||
newStats := &models.UserStats{
|
||||
UserID: userID,
|
||||
TotalDistance: 0,
|
||||
TotalTime: 0,
|
||||
AvgPace: "0:00",
|
||||
WorkoutsCount: 0,
|
||||
CurrentStreak: 0,
|
||||
LongestStreak: 0,
|
||||
WeeklyDistance: 0,
|
||||
MonthlyDistance: 0,
|
||||
Best5K: "",
|
||||
Best10K: "",
|
||||
BestHalf: "",
|
||||
BestMarathon: "",
|
||||
LastWorkout: time.Time{},
|
||||
}
|
||||
|
||||
if err := r.Create(newStats); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newStats, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return userStats, nil
|
||||
}
|
||||
|
||||
// Create создает новую статистику пользователя
|
||||
func (r *userStatsRepository) Create(userStats *models.UserStats) error {
|
||||
return r.db.Create(userStats).Error
|
||||
@@ -156,7 +190,7 @@ func (r *userStatsRepository) UpdatePersonalBest(userID uint, distanceType strin
|
||||
|
||||
// GetUserStatsResponse возвращает статистику в формате DTO
|
||||
func (r *userStatsRepository) GetUserStatsResponse(userID uint) (*models.UserStatsResponse, error) {
|
||||
userStats, err := r.GetByUserID(userID)
|
||||
userStats, err := r.GetByUserIDOrCreate(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -178,4 +212,3 @@ func (r *userStatsRepository) GetUserStatsResponse(userID uint) (*models.UserSta
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user