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.

45 lines
913 B
Go

1 month ago
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,
}
}
// CreatePurchase 创建购买记录
func (s *Service) CreatePurchase(userId, contentId uint64, contentType purchase.ContentType, price float64) error {
p := &purchase.Purchase{}
var err error
1 month ago
if contentType == purchase.ContentTypeColumn {
p, err = s.repo.FindColumnWithId(userId, contentId)
1 month ago
} else {
p, err = s.repo.FindArticleWithId(userId, contentId)
}
1 month ago
// 检查是否已购买
if err != nil {
return err
}
if p != nil {
1 month ago
return errors.New("已经购买过该内容")
}
// 创建购买记录
newP := purchase.NewPurchase(userId, contentId, contentType, price, 0)
return s.repo.Save(newP)
1 month ago
}