modified: serv_nginx/api_bb/internal/database/migrate.go

modified:   serv_nginx/api_bb/internal/handlers/handlers.go
	new file:   serv_nginx/api_bb/internal/handlers/personal_best_handler.go
	modified:   serv_nginx/api_bb/internal/models/personal_best.go
	modified:   serv_nginx/api_bb/internal/models/user_stats.go
	modified:   serv_nginx/api_bb/internal/repository/personal_best_repository.go
	modified:   serv_nginx/api_bb/internal/routes/routes.go
	modified:   serv_nginx/api_bb/internal/service/personal_best_service.go
	modified:   serv_nginx/bbvue/src/stores/user.js
personal bests add handler, rout, service, repository, logic and
migrations for
This commit is contained in:
2025-10-20 03:06:06 +05:00
parent 1d0d99e938
commit 402296b726
9 changed files with 708 additions and 14 deletions
@@ -5,6 +5,7 @@ import (
"api_bb/internal/models"
"api_bb/internal/repository"
"fmt"
"time"
"gorm.io/gorm"
)
@@ -64,11 +65,110 @@ func (s *PersonalBestService) CreatePersonalBest(userID uint, req models.Persona
return personalBest, nil
}
// GetPersonalBestByID возвращает личный рекорд по ID
func (s *PersonalBestService) GetPersonalBestByID(id uint) (*models.PersonalBest, error) {
return s.pbRepo.GetByID(id)
}
// GetUserPersonalBests возвращает все личные рекорды пользователя
func (s *PersonalBestService) GetUserPersonalBests(userID uint) ([]models.PersonalBest, error) {
return s.pbRepo.GetByUserID(userID)
}
// GetPersonalBestsByDistance возвращает личные рекорды по дистанции
func (s *PersonalBestService) GetPersonalBestsByDistance(userID uint, distanceType models.DistanceType) ([]models.PersonalBest, error) {
return s.pbRepo.GetByUserAndDistance(userID, distanceType)
}
// GetBestByDistance возвращает лучший результат на дистанции
func (s *PersonalBestService) GetBestByDistance(userID uint, distanceType models.DistanceType) (*models.PersonalBest, error) {
return s.pbRepo.GetBestByDistance(userID, distanceType)
}
// UpdatePersonalBest обновляет личный рекорд
func (s *PersonalBestService) UpdatePersonalBest(id uint, userID uint, req models.PersonalBestUpdateRequest) (*models.PersonalBest, error) {
// Получаем существующий рекорд
pb, err := s.pbRepo.GetByID(id)
if err != nil {
return nil, err
}
// Проверяем, что рекорд принадлежит пользователю
if pb.UserID != userID {
return nil, gorm.ErrRecordNotFound
}
// Обновляем поля
if req.DistanceType != "" {
pb.DistanceType = req.DistanceType
}
if req.Time != "" {
pb.Time = req.Time
// Пересчитываем темп при изменении времени
if req.Pace == "" {
calculatedPace, err := s.pbRepo.CalculatePace(req.Time, pb.DistanceType)
if err != nil {
return nil, err
}
pb.Pace = calculatedPace
}
}
if req.Pace != "" {
pb.Pace = req.Pace
}
if !req.Date.IsZero() {
pb.Date = req.Date
}
if req.EventName != "" {
pb.EventName = req.EventName
}
if req.Location != "" {
pb.Location = req.Location
}
pb.Verified = req.Verified
if err := s.pbRepo.Update(pb); err != nil {
return nil, err
}
return pb, nil
}
// DeletePersonalBest удаляет личный рекорд
func (s *PersonalBestService) DeletePersonalBest(id uint, userID uint) error {
// Проверяем, что рекорд принадлежит пользователю
pb, err := s.pbRepo.GetByID(id)
if err != nil {
return err
}
if pb.UserID != userID {
return gorm.ErrRecordNotFound
}
return s.pbRepo.Delete(id)
}
// GetVerifiedPersonalBests возвращает подтвержденные личные рекорды
func (s *PersonalBestService) GetVerifiedPersonalBests(userID uint) ([]models.PersonalBest, error) {
return s.pbRepo.GetVerifiedByUserID(userID)
}
// GetPersonalBestsByDateRange возвращает личные рекорды за период
func (s *PersonalBestService) GetPersonalBestsByDateRange(userID uint, startDate, endDate time.Time) ([]models.PersonalBest, error) {
return s.pbRepo.GetByDateRange(userID, startDate, endDate)
}
// GetRecentPersonalBests возвращает последние личные рекорды
func (s *PersonalBestService) GetRecentPersonalBests(userID uint, limit int) ([]models.PersonalBest, error) {
return s.pbRepo.GetRecentPersonalBests(userID, limit)
}
// GetPersonalBestsByEvent возвращает личные рекорды по названию события
func (s *PersonalBestService) GetPersonalBestsByEvent(userID uint, eventName string) ([]models.PersonalBest, error) {
return s.pbRepo.GetByEventName(userID, eventName)
}
// GetPersonalBestsSummary возвращает сводку лучших результатов
func (s *PersonalBestService) GetPersonalBestsSummary(userID uint) (*models.PersonalBestsSummary, error) {
return s.pbRepo.GetPersonalBestsSummary(userID)
@@ -89,3 +189,8 @@ func (s *PersonalBestService) VerifyPersonalBest(id uint, userID uint) error {
pb.Verified = true
return s.pbRepo.Update(pb)
}
// CalculatePace вычисляет темп для времени и дистанции
func (s *PersonalBestService) CalculatePace(timeStr string, distanceType models.DistanceType) (string, error) {
return s.pbRepo.CalculatePace(timeStr, distanceType)
}