feat: add list profile
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"backend/common/media_store"
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/pkg/path"
|
||||
"backend/pkg/pg"
|
||||
@@ -30,9 +31,31 @@ func (svc *Service) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMediaByHash
|
||||
func (svc *Service) GetMediaByHash(ctx context.Context, tenantId int64, hash string) (*model.Medias, error) {
|
||||
log := svc.log.WithField("method", "GetMediaByHash")
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.Hash.EQ(String(hash)).AND(
|
||||
tbl.TenantID.EQ(Int(tenantId)),
|
||||
),
|
||||
)
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var m model.Medias
|
||||
if err := stmt.QueryContext(ctx, svc.db, &m); err != nil {
|
||||
return nil, errors.Wrapf(err, "get media by hash %s failed", hash)
|
||||
}
|
||||
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
// GetByID
|
||||
func (svc *Service) GetByID(ctx context.Context, tenantId, userId, id int64) (*ListItem, error) {
|
||||
log := svc.log.WithField("method", "GetByID")
|
||||
func (svc *Service) GetMediaByID(ctx context.Context, tenantId, userId, id int64) (*model.Medias, error) {
|
||||
log := svc.log.WithField("method", "GetMediaByID")
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(
|
||||
@@ -40,22 +63,28 @@ func (svc *Service) GetByID(ctx context.Context, tenantId, userId, id int64) (*L
|
||||
tbl.TenantID.EQ(Int(tenantId)),
|
||||
),
|
||||
)
|
||||
log.Debug(stmt.Sql())
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var media ListItem
|
||||
if err := stmt.QueryContext(ctx, svc.db, &media); err != nil {
|
||||
var m model.Medias
|
||||
if err := stmt.QueryContext(ctx, svc.db, &m); err != nil {
|
||||
return nil, errors.Wrap(err, "query media by id")
|
||||
}
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
// todo: resources
|
||||
|
||||
var err error
|
||||
media.Bought, err = svc.HasUserBought(ctx, tenantId, userId, media.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "check user bought")
|
||||
func (svc *Service) ModelToListItem(ctx context.Context, m *model.Medias) *ListItem {
|
||||
return &ListItem{
|
||||
ID: m.ID,
|
||||
Hash: m.Hash,
|
||||
Title: m.Title,
|
||||
Description: m.Description,
|
||||
Duration: m.Duration,
|
||||
Price: m.Price,
|
||||
Discount: m.Discount,
|
||||
Resources: m.Resources,
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
// List
|
||||
@@ -102,14 +131,24 @@ func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *Li
|
||||
}
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var dest []ListItem
|
||||
var dest []model.Medias
|
||||
if err := stmt.QueryContext(ctx, svc.db, &dest); err != nil {
|
||||
return nil, errors.Wrap(err, "query medias")
|
||||
}
|
||||
|
||||
items := lo.Map(dest, func(item ListItem, _ int) ListItem {
|
||||
if lo.Contains(boughtIDs, item.ID) {
|
||||
item.Bought = true
|
||||
items := lo.Map(dest, func(m model.Medias, _ int) ListItem {
|
||||
item := ListItem{
|
||||
ID: m.ID,
|
||||
Hash: m.Hash,
|
||||
Title: m.Title,
|
||||
Description: m.Description,
|
||||
Duration: m.Duration,
|
||||
Price: m.Price,
|
||||
Discount: m.Discount,
|
||||
Resources: m.Resources,
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
Bought: lo.Contains(boughtIDs, m.ID),
|
||||
}
|
||||
return item
|
||||
})
|
||||
@@ -145,7 +184,7 @@ func (svc *Service) HasUserBought(ctx context.Context, tenantId, userId, mediaId
|
||||
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.MediaID).
|
||||
SELECT(COUNT(tbl.MediaID).AS("cnt")).
|
||||
WHERE(
|
||||
tbl.TenantID.EQ(Int(tenantId)).AND(
|
||||
tbl.UserID.EQ(Int(userId)).AND(
|
||||
@@ -155,12 +194,14 @@ func (svc *Service) HasUserBought(ctx context.Context, tenantId, userId, mediaId
|
||||
)
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var mediaID int64
|
||||
if err := stmt.QueryContext(ctx, svc.db, &mediaID); err != nil {
|
||||
var m struct {
|
||||
Cnt int64
|
||||
}
|
||||
if err := stmt.QueryContext(ctx, svc.db, &m); err != nil {
|
||||
return false, errors.Wrap(err, "query user bought media")
|
||||
}
|
||||
|
||||
return mediaID > 0, nil
|
||||
return m.Cnt > 0, nil
|
||||
}
|
||||
|
||||
// Upsert
|
||||
@@ -182,14 +223,15 @@ func (svc *Service) Upsert(ctx context.Context, tenantId int64, item media_store
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
INSERT(tbl.TenantID, tbl.Hash, tbl.Title, tbl.Price, tbl.Resources, tbl.Publish).
|
||||
VALUES(Int(tenantId), String(item.Hash), String(item.Name), Int(item.Price()), Json(resources.MustValue()), Bool(true)).
|
||||
INSERT(tbl.TenantID, tbl.Hash, tbl.Title, tbl.Price, tbl.Duration, tbl.Resources, tbl.Publish).
|
||||
VALUES(Int(tenantId), String(item.Hash), String(item.Name), Int(item.Price()), Int(item.Duration), Json(resources.MustValue()), Bool(true)).
|
||||
ON_CONFLICT(tbl.Hash).
|
||||
DO_UPDATE(
|
||||
SET(
|
||||
tbl.Title.SET(String(item.Name)),
|
||||
tbl.Price.SET(Int(item.Price())),
|
||||
tbl.Resources.SET(Json(resources.MustValue())),
|
||||
tbl.Duration.SET(Int(item.Duration)),
|
||||
tbl.Publish.SET(Bool(true)),
|
||||
tbl.UpdatedAt.SET(TimestampT(time.Now())),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user