You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package purchase
import (
"cls/internal/domain/purchase"
"errors"
)
// Service 购买记录应用服务
type Service struct {
repo purchase.Repository
}
// NewService 创建购买记录应用服务
func NewService(repo purchase.Repository) *Service {
return &Service{
repo: repo,
}
}
// HasPermission 检查用户是否有权限访问内容
func (s *Service) HasPermission(userId, contentId uint64, contentType purchase.ContentType, price float64) (bool, error) {
// 如果内容是免费的直接返回true
if price == 0 {
return true, nil
}
// 检查用户是否购买了内容
purchase, err := s.repo.FindByUserIdAndContent(userId, contentId, contentType)
if err != nil {
return false, err
}
if purchase == nil {
return false, nil
}
return purchase.IsValid(), nil
}
// CreatePurchase 创建购买记录
func (s *Service) CreatePurchase(userId, contentId uint64, contentType purchase.ContentType, price float64) error {
// 检查是否已购买
exist, err := s.repo.FindByUserIdAndContent(userId, contentId, contentType)
if err != nil {
return err
}
if exist != nil {
return errors.New("已经购买过该内容")
}
// 创建购买记录
purchase := purchase.NewPurchase(userId, contentId, contentType, price, 0)
return s.repo.Save(purchase)
}