feat: complete get by id
This commit is contained in:
Binary file not shown.
@@ -25,7 +25,7 @@ func (svc *Service) Prepare() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetByID
|
// GetByID
|
||||||
func (svc *Service) GetByID(ctx context.Context, id int64) (*ListItem, error) {
|
func (svc *Service) GetByID(ctx context.Context, id, userId int64) (*ListItem, error) {
|
||||||
log := svc.log.WithField("method", "GetByID")
|
log := svc.log.WithField("method", "GetByID")
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := table.Medias
|
||||||
@@ -37,6 +37,33 @@ func (svc *Service) GetByID(ctx context.Context, id int64) (*ListItem, error) {
|
|||||||
return nil, errors.Wrap(err, "query media by id")
|
return nil, errors.Wrap(err, "query media by id")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tblResource := table.MediaResources
|
||||||
|
stmtResource := tblResource.
|
||||||
|
SELECT(
|
||||||
|
tblResource.MediaID,
|
||||||
|
tblResource.Type,
|
||||||
|
tblResource.Size,
|
||||||
|
).
|
||||||
|
WHERE(tblResource.MediaID.EQ(Int(id)))
|
||||||
|
log.Debug(stmtResource.DebugSql())
|
||||||
|
|
||||||
|
var resources []model.MediaResources
|
||||||
|
if err := stmt.QueryContext(ctx, svc.db, &resources); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "query media resources")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(resources) == 0 {
|
||||||
|
return &media, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
media.MediaResources = resources
|
||||||
|
|
||||||
|
var err error
|
||||||
|
media.Bought, err = svc.HasUserBought(ctx, media.TenantID, userId, media.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "check user bought")
|
||||||
|
}
|
||||||
|
|
||||||
return &media, nil
|
return &media, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +120,11 @@ func (svc *Service) DecorateListResources(ctx context.Context, tenantId, userId
|
|||||||
func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *ListFilter) ([]ListItem, error) {
|
func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *ListFilter) ([]ListItem, error) {
|
||||||
log := svc.log.WithField("method", "List")
|
log := svc.log.WithField("method", "List")
|
||||||
|
|
||||||
|
boughtIDs, err := svc.GetUserBoughtMedias(ctx, tenantId, userId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "get user bought medias")
|
||||||
|
}
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := table.Medias
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
@@ -104,11 +136,6 @@ func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *Li
|
|||||||
}
|
}
|
||||||
|
|
||||||
if filter.Bought != nil && *filter.Bought {
|
if filter.Bought != nil && *filter.Bought {
|
||||||
boughtIDs, err := svc.GetUserBoughtMedias(ctx, tenantId, userId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "get user bought medias")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(boughtIDs) > 0 {
|
if len(boughtIDs) > 0 {
|
||||||
stmt = stmt.
|
stmt = stmt.
|
||||||
WHERE(tbl.ID.IN(lo.Map(boughtIDs, func(item int64, _ int) Expression {
|
WHERE(tbl.ID.IN(lo.Map(boughtIDs, func(item int64, _ int) Expression {
|
||||||
@@ -138,7 +165,14 @@ func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *Li
|
|||||||
return nil, errors.Wrap(err, "query medias")
|
return nil, errors.Wrap(err, "query medias")
|
||||||
}
|
}
|
||||||
|
|
||||||
return dest, nil
|
items := lo.Map(dest, func(item ListItem, _ int) ListItem {
|
||||||
|
if lo.Contains(boughtIDs, item.ID) {
|
||||||
|
item.Bought = true
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
})
|
||||||
|
|
||||||
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserBoughtMedias
|
// GetUserBoughtMedias
|
||||||
@@ -162,3 +196,27 @@ func (svc *Service) GetUserBoughtMedias(ctx context.Context, tenant int64, userI
|
|||||||
|
|
||||||
return mediaIDs, nil
|
return mediaIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasUserBought
|
||||||
|
func (svc *Service) HasUserBought(ctx context.Context, tenantId, userId, mediaId int64) (bool, error) {
|
||||||
|
log := svc.log.WithField("method", "HasUserBought")
|
||||||
|
|
||||||
|
tbl := table.UserMedias
|
||||||
|
stmt := tbl.
|
||||||
|
SELECT(tbl.MediaID).
|
||||||
|
WHERE(
|
||||||
|
tbl.TenantID.EQ(Int(tenantId)).AND(
|
||||||
|
tbl.UserID.EQ(Int(userId)).AND(
|
||||||
|
tbl.MediaID.EQ(Int(mediaId)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
log.Debug(stmt.DebugSql())
|
||||||
|
|
||||||
|
var mediaID int64
|
||||||
|
if err := stmt.QueryContext(ctx, svc.db, &mediaID); err != nil {
|
||||||
|
return false, errors.Wrap(err, "query user bought media")
|
||||||
|
}
|
||||||
|
|
||||||
|
return mediaID > 0, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user