feat: add test database setup and usage helpers
- Introduced a new test file for setting up an in-memory SQLite database for testing purposes. - Added helper functions for seeding test data, including users, plans, subscriptions, and wallet transactions. - Implemented usage helpers to load user video counts and storage usage. - Created user payload struct and functions to build user payloads with preferences and wallet balance. - Refactored gRPC server setup to include new services and handlers. - Updated proto files to simplify service definitions by removing redundant service prefixes.
This commit is contained in:
29
internal/service/usage_helpers.go
Normal file
29
internal/service/usage_helpers.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"stream.api/internal/database/model"
|
||||
"stream.api/pkg/logger"
|
||||
)
|
||||
|
||||
type usagePayload struct {
|
||||
UserID string `json:"user_id"`
|
||||
TotalVideos int64 `json:"total_videos"`
|
||||
TotalStorage int64 `json:"total_storage"`
|
||||
}
|
||||
|
||||
func loadUsage(ctx context.Context, db *gorm.DB, l logger.Logger, user *model.User) (*usagePayload, error) {
|
||||
var totalVideos int64
|
||||
if err := db.WithContext(ctx).Model(&model.Video{}).Where("user_id = ?", user.ID).Count(&totalVideos).Error; err != nil {
|
||||
l.Error("Failed to count user videos", "error", err, "user_id", user.ID)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &usagePayload{
|
||||
UserID: user.ID,
|
||||
TotalVideos: totalVideos,
|
||||
TotalStorage: user.StorageUsed,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user