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
953 B
Go
45 lines
953 B
Go
package purchase
|
|
|
|
import (
|
|
"cls/internal/domain/purchase"
|
|
"errors"
|
|
)
|
|
|
|
// Service 购买记录应用服务
|
|
type PurchaseService struct {
|
|
repo purchase.Repository
|
|
}
|
|
|
|
// NewService 创建购买记录应用服务
|
|
func NewPurchaseService(repo purchase.Repository) *PurchaseService {
|
|
return &PurchaseService{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
// CreatePurchase 创建购买记录
|
|
func (s *PurchaseService) CreatePurchase(userId, contentId uint64, contentType purchase.ContentType, price float64) error {
|
|
p := &purchase.Purchase{}
|
|
var err error
|
|
|
|
if contentType == purchase.ContentTypeColumn {
|
|
p, err = s.repo.FindColumnWithId(userId, contentId)
|
|
|
|
} else {
|
|
p, err = s.repo.FindArticleWithId(userId, contentId)
|
|
|
|
}
|
|
|
|
// 检查是否已购买
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if p != nil {
|
|
return errors.New("已经购买过该内容")
|
|
}
|
|
|
|
// 创建购买记录
|
|
newP := purchase.NewPurchase(userId, contentId, contentType, price, 0)
|
|
return s.repo.Save(newP)
|
|
}
|