|
|
|
package purchase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cls/internal/domain/purchase"
|
|
|
|
"cls/pkg/logger"
|
|
|
|
"cls/pkg/xorm_engine"
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PurchaseRepositoryORM struct {
|
|
|
|
engine *xorm_engine.Engine
|
|
|
|
log logger.New
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ purchase.Repository = (*PurchaseRepositoryORM)(nil)
|
|
|
|
|
|
|
|
func NewPurchaseRepositoryORM(db *xorm_engine.Engine, logger logger.New) purchase.Repository {
|
|
|
|
return &PurchaseRepositoryORM{
|
|
|
|
engine: db,
|
|
|
|
log: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) Save(purchase *purchase.Purchase) error {
|
|
|
|
if purchase.Id == 0 {
|
|
|
|
_, err := r.engine.Insert(purchase)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := r.engine.ID(purchase.Id).Update(purchase)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) FindByUserIdAndContent(userId, contentId uint64, contentType purchase.ContentType) (*purchase.Purchase, error) {
|
|
|
|
purchase := &purchase.Purchase{}
|
|
|
|
exist, err := r.engine.Where("user_id = ? AND content_id = ? AND content_type = ?",
|
|
|
|
userId, contentId, contentType).Get(purchase)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return purchase, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) FindByUserId(userId uint64) ([]*purchase.Purchase, error) {
|
|
|
|
purchases := make([]*purchase.Purchase, 0)
|
|
|
|
err := r.engine.Where("user_id = ?", userId).Find(&purchases)
|
|
|
|
return purchases, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) FindByContent(contentId uint64, contentType purchase.ContentType) ([]*purchase.Purchase, error) {
|
|
|
|
purchases := make([]*purchase.Purchase, 0)
|
|
|
|
err := r.engine.Where("content_id = ? AND content_type = ?", contentId, contentType).Find(&purchases)
|
|
|
|
return purchases, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) FindArticlesByUserId(userId uint64) ([]*purchase.Purchase, error) {
|
|
|
|
purchases := make([]*purchase.Purchase, 0)
|
|
|
|
err := r.engine.Where(builder.Eq{"user_id": userId}.And(builder.Eq{"content_type": purchase.ContentTypeArticle})).Find(&purchases)
|
|
|
|
return purchases, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) FindColumnsByUserId(userId uint64) ([]*purchase.Purchase, error) {
|
|
|
|
purchases := make([]*purchase.Purchase, 0)
|
|
|
|
err := r.engine.Where(builder.Eq{"user_id": userId}.And(builder.Eq{"content_type": purchase.ContentTypeColumn})).Find(&purchases)
|
|
|
|
return purchases, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) FindArticleById(uid uint64, ids ...uint64) ([]*purchase.Purchase, error) {
|
|
|
|
purchases := make([]*purchase.Purchase, 0)
|
|
|
|
err := r.engine.Where(builder.Eq{"user_id": uid}.And(builder.In("content_id", ids)).And(builder.Eq{"content_type": purchase.ContentTypeArticle})).Find(&purchases)
|
|
|
|
return purchases, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PurchaseRepositoryORM) FindColumnById(uid uint64, ids ...uint64) ([]*purchase.Purchase, error) {
|
|
|
|
purchases := make([]*purchase.Purchase, 0)
|
|
|
|
err := r.engine.Where(builder.Eq{"user_id": uid}.And(builder.In("content_id", ids)).And(builder.Eq{"content_type": purchase.ContentTypeColumn})).Find(&purchases)
|
|
|
|
return purchases, err
|
|
|
|
}
|