Files
stream.api/internal/repository/plan_repository.go
lethdat a0ae2b681a feat: Implement video workflow repository and related services
- Added videoWorkflowRepository with methods to manage video and user interactions.
- Introduced catalog_mapper for converting database models to protobuf representations.
- Created domain_helpers for normalizing domain and ad format values.
- Defined service interfaces for payment, account, notification, domain, ad template, player config, video, and user management.
- Implemented OAuth helpers for generating state and caching keys.
- Developed payment_proto_helpers for mapping payment-related models to protobuf.
- Added service policy helpers to enforce plan requirements and user permissions.
- Created user_mapper for converting user payloads to protobuf format.
- Implemented value_helpers for handling various value conversions and nil checks.
- Developed video_helpers for normalizing video statuses and managing storage types.
- Created video_mapper for mapping video models to protobuf format.
- Implemented render workflow for managing video creation and job processing.
2026-03-26 18:38:47 +07:00

70 lines
2.1 KiB
Go

package repository
import (
"context"
"strings"
"gorm.io/gorm"
"stream.api/internal/database/model"
)
type planRepository struct {
db *gorm.DB
}
func NewPlanRepository(db *gorm.DB) *planRepository {
return &planRepository{db: db}
}
func (r *planRepository) GetByID(ctx context.Context, planID string) (*model.Plan, error) {
var plan model.Plan
if err := r.db.WithContext(ctx).Where("id = ?", strings.TrimSpace(planID)).First(&plan).Error; err != nil {
return nil, err
}
return &plan, nil
}
func (r *planRepository) ListActive(ctx context.Context) ([]model.Plan, error) {
var plans []model.Plan
if err := r.db.WithContext(ctx).Where("is_active = ?", true).Find(&plans).Error; err != nil {
return nil, err
}
return plans, nil
}
func (r *planRepository) ListAll(ctx context.Context) ([]model.Plan, error) {
var plans []model.Plan
if err := r.db.WithContext(ctx).Order("price ASC").Find(&plans).Error; err != nil {
return nil, err
}
return plans, nil
}
func (r *planRepository) Create(ctx context.Context, plan *model.Plan) error {
return r.db.WithContext(ctx).Create(plan).Error
}
func (r *planRepository) Save(ctx context.Context, plan *model.Plan) error {
return r.db.WithContext(ctx).Save(plan).Error
}
func (r *planRepository) CountPaymentsByPlan(ctx context.Context, planID string) (int64, error) {
var count int64
err := r.db.WithContext(ctx).Model(&model.Payment{}).Where("plan_id = ?", strings.TrimSpace(planID)).Count(&count).Error
return count, err
}
func (r *planRepository) CountSubscriptionsByPlan(ctx context.Context, planID string) (int64, error) {
var count int64
err := r.db.WithContext(ctx).Model(&model.PlanSubscription{}).Where("plan_id = ?", strings.TrimSpace(planID)).Count(&count).Error
return count, err
}
func (r *planRepository) SetActive(ctx context.Context, planID string, isActive bool) error {
return r.db.WithContext(ctx).Model(&model.Plan{}).Where("id = ?", strings.TrimSpace(planID)).Update("is_active", isActive).Error
}
func (r *planRepository) DeleteByID(ctx context.Context, planID string) error {
return r.db.WithContext(ctx).Where("id = ?", strings.TrimSpace(planID)).Delete(&model.Plan{}).Error
}