55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
|
package userproductlogic
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"tianyuan-api/apps/sentinel/internal/model"
|
||
|
|
||
|
"tianyuan-api/apps/sentinel/internal/svc"
|
||
|
"tianyuan-api/apps/sentinel/sentinel"
|
||
|
|
||
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
)
|
||
|
|
||
|
type CreateUserProductLogic struct {
|
||
|
ctx context.Context
|
||
|
svcCtx *svc.ServiceContext
|
||
|
logx.Logger
|
||
|
}
|
||
|
|
||
|
func NewCreateUserProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserProductLogic {
|
||
|
return &CreateUserProductLogic{
|
||
|
ctx: ctx,
|
||
|
svcCtx: svcCtx,
|
||
|
Logger: logx.WithContext(ctx),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// UserProduct methods
|
||
|
func (l *CreateUserProductLogic) CreateUserProduct(in *sentinel.CreateUserProductRequest) (*sentinel.UserProductEmptyResponse, error) {
|
||
|
isExist, err := l.IsUserProductAssociated(l.ctx, in.UserId, in.ProductId)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if isExist {
|
||
|
return nil, errors.New("该产品已定阅读,无法重复订阅")
|
||
|
}
|
||
|
_, err = l.svcCtx.UserProductsModel.Insert(l.ctx, &model.UserProducts{UserId: in.UserId, ProductId: in.ProductId})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &sentinel.UserProductEmptyResponse{}, nil
|
||
|
}
|
||
|
func (l *CreateUserProductLogic) IsUserProductAssociated(ctx context.Context, userId, productId int64) (bool, error) {
|
||
|
_, err := l.svcCtx.UserProductsModel.FindOneUserProduct(ctx, userId, productId)
|
||
|
if err != nil {
|
||
|
if err == model.ErrNotFound {
|
||
|
return false, nil
|
||
|
}
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return true, nil
|
||
|
}
|