new file: serv_nginx/api_bb/internal/repository/gallery_repository.go
new file: serv_nginx/api_bb/internal/repository/training_plan_repository.go add gallery and training_plan repository
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
// repository/gallery_repository.go
|
||||
package repository
|
||||
|
||||
import (
|
||||
"api_bb/internal/models"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GalleryRepository interface {
|
||||
Create(gallery *models.Gallery) error
|
||||
FindByID(id uint) (*models.Gallery, error)
|
||||
FindAll() ([]models.Gallery, error)
|
||||
Update(gallery *models.Gallery) error
|
||||
Delete(id uint) error
|
||||
FindByCategory(category models.GalleryCategory) ([]models.Gallery, error)
|
||||
FindByAuthor(authorID uint) ([]models.Gallery, error)
|
||||
FindPopular(limit int) ([]models.Gallery, error)
|
||||
FindRecent(limit int) ([]models.Gallery, error)
|
||||
IncrementViews(galleryID uint) error
|
||||
IncrementLikes(galleryID uint) error
|
||||
DecrementLikes(galleryID uint) error
|
||||
FindByEventDateRange(startDate, endDate time.Time) ([]models.Gallery, error)
|
||||
}
|
||||
|
||||
type galleryRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewGalleryRepository(db *gorm.DB) GalleryRepository {
|
||||
return &galleryRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *galleryRepository) Create(gallery *models.Gallery) error {
|
||||
return r.db.Create(gallery).Error
|
||||
}
|
||||
|
||||
func (r *galleryRepository) FindByID(id uint) (*models.Gallery, error) {
|
||||
var gallery models.Gallery
|
||||
err := r.db.Preload("Author").First(&gallery, id).Error
|
||||
return &gallery, err
|
||||
}
|
||||
|
||||
func (r *galleryRepository) FindAll() ([]models.Gallery, error) {
|
||||
var galleries []models.Gallery
|
||||
err := r.db.Preload("Author").Order("created_at DESC").Find(&galleries).Error
|
||||
return galleries, err
|
||||
}
|
||||
|
||||
func (r *galleryRepository) Update(gallery *models.Gallery) error {
|
||||
return r.db.Save(gallery).Error
|
||||
}
|
||||
|
||||
func (r *galleryRepository) Delete(id uint) error {
|
||||
return r.db.Delete(&models.Gallery{}, id).Error
|
||||
}
|
||||
|
||||
func (r *galleryRepository) FindByCategory(category models.GalleryCategory) ([]models.Gallery, error) {
|
||||
var galleries []models.Gallery
|
||||
err := r.db.Preload("Author").Where("category = ?", category).Order("created_at DESC").Find(&galleries).Error
|
||||
return galleries, err
|
||||
}
|
||||
|
||||
func (r *galleryRepository) FindByAuthor(authorID uint) ([]models.Gallery, error) {
|
||||
var galleries []models.Gallery
|
||||
err := r.db.Preload("Author").Where("author_id = ?", authorID).Order("created_at DESC").Find(&galleries).Error
|
||||
return galleries, err
|
||||
}
|
||||
|
||||
func (r *galleryRepository) FindPopular(limit int) ([]models.Gallery, error) {
|
||||
var galleries []models.Gallery
|
||||
err := r.db.Preload("Author").Order("views DESC, likes DESC").Limit(limit).Find(&galleries).Error
|
||||
return galleries, err
|
||||
}
|
||||
|
||||
func (r *galleryRepository) FindRecent(limit int) ([]models.Gallery, error) {
|
||||
var galleries []models.Gallery
|
||||
err := r.db.Preload("Author").Order("created_at DESC").Limit(limit).Find(&galleries).Error
|
||||
return galleries, err
|
||||
}
|
||||
|
||||
func (r *galleryRepository) IncrementViews(galleryID uint) error {
|
||||
result := r.db.Model(&models.Gallery{}).Where("id = ?", galleryID).Update("views", gorm.Expr("views + ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("gallery not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *galleryRepository) IncrementLikes(galleryID uint) error {
|
||||
result := r.db.Model(&models.Gallery{}).Where("id = ?", galleryID).Update("likes", gorm.Expr("likes + ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("gallery not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *galleryRepository) DecrementLikes(galleryID uint) error {
|
||||
result := r.db.Model(&models.Gallery{}).Where("id = ? AND likes > 0", galleryID).Update("likes", gorm.Expr("likes - ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("gallery not found or likes already zero")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *galleryRepository) FindByEventDateRange(startDate, endDate time.Time) ([]models.Gallery, error) {
|
||||
var galleries []models.Gallery
|
||||
err := r.db.Preload("Author").
|
||||
Where("event_date BETWEEN ? AND ?", startDate, endDate).
|
||||
Order("event_date DESC").
|
||||
Find(&galleries).Error
|
||||
return galleries, err
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
// repositories/training_plan_repository.go
|
||||
package repository
|
||||
|
||||
import (
|
||||
"api_bb/internal/models"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TrainingPlanRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewTrainingPlanRepository(db *gorm.DB) *TrainingPlanRepository {
|
||||
return &TrainingPlanRepository{db: db}
|
||||
}
|
||||
|
||||
// Create создает новый план тренировок
|
||||
func (r *TrainingPlanRepository) Create(plan *models.TrainingPlan) error {
|
||||
return r.db.Create(plan).Error
|
||||
}
|
||||
|
||||
// GetByID возвращает план тренировок по ID
|
||||
func (r *TrainingPlanRepository) GetByID(id uint) (*models.TrainingPlan, error) {
|
||||
var plan models.TrainingPlan
|
||||
err := r.db.Preload("Workouts").First(&plan, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &plan, nil
|
||||
}
|
||||
|
||||
// GetByUserID возвращает все планы тренировок пользователя
|
||||
func (r *TrainingPlanRepository) GetByUserID(userID uint) ([]models.TrainingPlan, error) {
|
||||
var plans []models.TrainingPlan
|
||||
err := r.db.Preload("Workouts").Where("user_id = ?", userID).Find(&plans).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return plans, nil
|
||||
}
|
||||
|
||||
// Update обновляет план тренировок
|
||||
func (r *TrainingPlanRepository) Update(plan *models.TrainingPlan) error {
|
||||
return r.db.Save(plan).Error
|
||||
}
|
||||
|
||||
// Delete удаляет план тренировок
|
||||
func (r *TrainingPlanRepository) Delete(id uint) error {
|
||||
return r.db.Delete(&models.TrainingPlan{}, id).Error
|
||||
}
|
||||
|
||||
// UpdateCurrentWeek обновляет текущую неделю плана тренировок
|
||||
func (r *TrainingPlanRepository) UpdateCurrentWeek(id uint, currentWeek int) error {
|
||||
return r.db.Model(&models.TrainingPlan{}).Where("id = ?", id).Update("current_week", currentWeek).Error
|
||||
}
|
||||
|
||||
// MarkAsCompleted помечает план тренировок как завершенный
|
||||
func (r *TrainingPlanRepository) MarkAsCompleted(id uint) error {
|
||||
return r.db.Model(&models.TrainingPlan{}).Where("id = ?", id).Update("completed", true).Error
|
||||
}
|
||||
|
||||
// GetActivePlan возвращает активный (не завершенный) план тренировок пользователя
|
||||
func (r *TrainingPlanRepository) GetActivePlan(userID uint) (*models.TrainingPlan, error) {
|
||||
var plan models.TrainingPlan
|
||||
err := r.db.Preload("Workouts").Where("user_id = ? AND completed = ?", userID, false).First(&plan).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &plan, nil
|
||||
}
|
||||
|
||||
// CreateWorkout создает тренировку в плане
|
||||
func (r *TrainingPlanRepository) CreateWorkout(workout *models.TrainingWorkout) error {
|
||||
return r.db.Create(workout).Error
|
||||
}
|
||||
|
||||
// GetWorkoutByID возвращает тренировку по ID
|
||||
func (r *TrainingPlanRepository) GetWorkoutByID(id uint) (*models.TrainingWorkout, error) {
|
||||
var workout models.TrainingWorkout
|
||||
err := r.db.First(&workout, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &workout, nil
|
||||
}
|
||||
|
||||
// GetWorkoutsByPlanID возвращает все тренировки плана
|
||||
func (r *TrainingPlanRepository) GetWorkoutsByPlanID(planID uint) ([]models.TrainingWorkout, error) {
|
||||
var workouts []models.TrainingWorkout
|
||||
err := r.db.Where("plan_id = ?", planID).Find(&workouts).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return workouts, nil
|
||||
}
|
||||
|
||||
// UpdateWorkout обновляет тренировку
|
||||
func (r *TrainingPlanRepository) UpdateWorkout(workout *models.TrainingWorkout) error {
|
||||
return r.db.Save(workout).Error
|
||||
}
|
||||
|
||||
// DeleteWorkout удаляет тренировку
|
||||
func (r *TrainingPlanRepository) DeleteWorkout(id uint) error {
|
||||
return r.db.Delete(&models.TrainingWorkout{}, id).Error
|
||||
}
|
||||
|
||||
// MarkWorkoutAsCompleted помечает тренировку как завершенную
|
||||
func (r *TrainingPlanRepository) MarkWorkoutAsCompleted(id uint) error {
|
||||
now := time.Now()
|
||||
return r.db.Model(&models.TrainingWorkout{}).Where("id = ?", id).Updates(map[string]interface{}{
|
||||
"completed": true,
|
||||
"completed_at": now,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// GetWorkoutsByWeek возвращает тренировки для определенной недели плана
|
||||
func (r *TrainingPlanRepository) GetWorkoutsByWeek(planID uint, week int) ([]models.TrainingWorkout, error) {
|
||||
var workouts []models.TrainingWorkout
|
||||
err := r.db.Where("plan_id = ? AND week = ?", planID, week).Find(&workouts).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return workouts, nil
|
||||
}
|
||||
|
||||
// GetCompletedWorkouts возвращает завершенные тренировки плана
|
||||
func (r *TrainingPlanRepository) GetCompletedWorkouts(planID uint) ([]models.TrainingWorkout, error) {
|
||||
var workouts []models.TrainingWorkout
|
||||
err := r.db.Where("plan_id = ? AND completed = ?", planID, true).Find(&workouts).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return workouts, nil
|
||||
}
|
||||
Reference in New Issue
Block a user