fix: issues
This commit is contained in:
@@ -44,14 +44,9 @@ func (c *Controller) Show(ctx fiber.Ctx) error {
|
||||
|
||||
// Audio
|
||||
func (c *Controller) Video(ctx fiber.Ctx) error {
|
||||
mediaId := ToInt64(ctx.Params("media"))
|
||||
tenantId := ToInt64(ctx.Locals("tenantId"))
|
||||
userId := ToInt64(ctx.Locals("userId"))
|
||||
// mediaId := ToInt64(ctx.Params("media"))
|
||||
// tenantId := ToInt64(ctx.Locals("tenantId"))
|
||||
// userId := ToInt64(ctx.Locals("userId"))
|
||||
|
||||
item, err := c.svc.GetVideo(ctx.Context(), tenantId, userId, mediaId)
|
||||
if err != nil {
|
||||
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
|
||||
}
|
||||
|
||||
return ctx.JSON(item)
|
||||
return ctx.JSON(nil)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,5 @@ type ListFilter struct {
|
||||
|
||||
type ListItem struct {
|
||||
model.Medias
|
||||
Bought bool `json:"bought"`
|
||||
MediaResources []model.MediaResources `json:"media_resources"`
|
||||
Bought bool `json:"bought"`
|
||||
}
|
||||
|
||||
@@ -3,12 +3,15 @@ package medias
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/pkg/path"
|
||||
"backend/pkg/pg"
|
||||
"backend/providers/storage"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -16,8 +19,9 @@ import (
|
||||
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
log *logrus.Entry `inject:"false"`
|
||||
db *sql.DB
|
||||
storageConfig *storage.Config
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (svc *Service) Prepare() error {
|
||||
@@ -42,26 +46,7 @@ func (svc *Service) GetByID(ctx context.Context, tenantId, userId, id int64) (*L
|
||||
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
|
||||
// todo: resources
|
||||
|
||||
var err error
|
||||
media.Bought, err = svc.HasUserBought(ctx, tenantId, userId, media.ID)
|
||||
@@ -72,55 +57,6 @@ func (svc *Service) GetByID(ctx context.Context, tenantId, userId, id int64) (*L
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
// Decorate List Resources
|
||||
func (svc *Service) DecorateListResources(ctx context.Context, tenantId, userId int64, items []ListItem) ([]ListItem, error) {
|
||||
log := svc.log.WithField("method", "DecorateListResources")
|
||||
|
||||
mediaIDs := make([]int64, len(items))
|
||||
for i, item := range items {
|
||||
mediaIDs[i] = item.ID
|
||||
}
|
||||
|
||||
tbl := table.MediaResources
|
||||
stmt := tbl.
|
||||
SELECT(
|
||||
tbl.MediaID,
|
||||
tbl.Type,
|
||||
tbl.Size,
|
||||
).
|
||||
WHERE(tbl.MediaID.IN(lo.Map(mediaIDs, func(item int64, _ int) Expression {
|
||||
return Int(item)
|
||||
})...))
|
||||
log.Debug(stmt.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 nil, nil
|
||||
}
|
||||
|
||||
// group resources by media id
|
||||
resourcesMap := make(map[int64][]model.MediaResources)
|
||||
for _, resource := range resources {
|
||||
if _, ok := resourcesMap[resource.MediaID]; !ok {
|
||||
resourcesMap[resource.MediaID] = make([]model.MediaResources, 0)
|
||||
}
|
||||
resourcesMap[resource.MediaID] = append(resourcesMap[resource.MediaID], resource)
|
||||
}
|
||||
|
||||
// set resources to items
|
||||
for i, item := range items {
|
||||
if resources, ok := resourcesMap[item.ID]; ok {
|
||||
items[i].MediaResources = resources
|
||||
}
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// List
|
||||
func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *ListFilter) ([]ListItem, error) {
|
||||
log := svc.log.WithField("method", "List")
|
||||
@@ -226,57 +162,76 @@ func (svc *Service) HasUserBought(ctx context.Context, tenantId, userId, mediaId
|
||||
return mediaID > 0, nil
|
||||
}
|
||||
|
||||
// GetVideo
|
||||
func (svc *Service) GetVideo(ctx context.Context, tenantId, userId, mediaId int64) (*pg.MediaSource, error) {
|
||||
log := svc.log.WithField("method", "GetVideo")
|
||||
// UnPublishTenantWithNotInUUIDs
|
||||
func (svc *Service) UnPublishTenantWithNotInUUIDs(ctx context.Context, tenantId int64, uuids []string) error {
|
||||
log := svc.log.WithField("method", "UnPublishTenantWithNotInUUIDs")
|
||||
|
||||
ok, err := svc.HasUserBought(ctx, tenantId, userId, mediaId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "check user bought")
|
||||
}
|
||||
log.Debugf("user bought media: %d of tennatID: %d", mediaId, tenantId)
|
||||
|
||||
source, err := svc.GetResourceOfType(ctx, mediaId, pg.MediaTypeVideo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get video resource")
|
||||
}
|
||||
|
||||
if ok {
|
||||
return source, nil
|
||||
}
|
||||
// cut 1 min video
|
||||
maxDuration := int64(60)
|
||||
duration := int64(0)
|
||||
|
||||
for i, item := range source.Items {
|
||||
duration += item.Duration
|
||||
if duration >= maxDuration {
|
||||
source.Items = source.Items[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return source, nil
|
||||
}
|
||||
|
||||
// GetResourceOfType
|
||||
func (svc *Service) GetResourceOfType(ctx context.Context, mediaId int64, resourceType pg.MediaType) (*pg.MediaSource, error) {
|
||||
log := svc.log.WithField("method", "GetResourceOfType")
|
||||
|
||||
tbl := table.MediaResources
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.Source).
|
||||
UPDATE().
|
||||
SET(
|
||||
tbl.Publish.SET(Bool(false)),
|
||||
).
|
||||
WHERE(
|
||||
tbl.MediaID.EQ(Int(mediaId)).AND(
|
||||
tbl.Type.EQ(String(resourceType.String())),
|
||||
tbl.TenantID.EQ(Int(tenantId)).AND(
|
||||
tbl.UUID.NOT_IN(lo.Map(uuids, func(item string, _ int) Expression {
|
||||
return String(item)
|
||||
})...),
|
||||
),
|
||||
)
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var source pg.MediaSource
|
||||
if err := stmt.QueryContext(ctx, svc.db, &source); err != nil {
|
||||
return nil, errors.Wrap(err, "query media source")
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return errors.Wrap(err, "unpublish tenant with not in uuids")
|
||||
}
|
||||
|
||||
return &source, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTenantUUIDs
|
||||
func (svc *Service) GetTenantUUIDs(ctx context.Context, tenantId int64) ([]string, error) {
|
||||
log := svc.log.WithField("method", "GetTenantUUIDs")
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.UUID).
|
||||
WHERE(tbl.TenantID.EQ(Int(tenantId)))
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var uuids []string
|
||||
if err := stmt.QueryContext(ctx, svc.db, &uuids); err != nil {
|
||||
return nil, errors.Wrap(err, "query tenant uuids")
|
||||
}
|
||||
|
||||
return uuids, nil
|
||||
}
|
||||
|
||||
// PublishTenant
|
||||
func (svc *Service) PublishTenantMedia(ctx context.Context, tenantId int64, uuid uuid.UUID, name string) error {
|
||||
log := svc.log.WithField("method", "PublishTenant")
|
||||
|
||||
resources := pg.MediaResources{}
|
||||
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypeVideo.String())) {
|
||||
resources = append(resources, pg.MediaTypeVideo)
|
||||
}
|
||||
|
||||
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypeAudio.String())) {
|
||||
resources = append(resources, pg.MediaTypeAudio)
|
||||
}
|
||||
|
||||
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypePdf.String())) {
|
||||
resources = append(resources, pg.MediaTypePdf)
|
||||
}
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
INSERT(tbl.TenantID, tbl.UUID, tbl.Title, tbl.Resources).
|
||||
VALUES(Int(tenantId), UUID(uuid), String(name), Json(resources.MustValue()))
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return errors.Wrap(err, "publish tenant")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/fixtures"
|
||||
dbUtil "backend/pkg/db"
|
||||
"backend/pkg/pg"
|
||||
"backend/providers/storage"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/google/uuid"
|
||||
@@ -118,59 +118,6 @@ func TestService_List(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("create media's resources", func() {
|
||||
items := []model.MediaResources{
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 3,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 3,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
}
|
||||
|
||||
tbl := table.MediaResources
|
||||
stmt := tbl.INSERT(
|
||||
tbl.MediaID,
|
||||
tbl.Type,
|
||||
tbl.Size,
|
||||
tbl.Publish,
|
||||
).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("get list", func() {
|
||||
svc := &Service{db: db}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
@@ -213,61 +160,50 @@ func TestService_List1(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_DecorateListResources(t *testing.T) {
|
||||
Convey("TestService_DecorateListResources", t, func() {
|
||||
func TestService_PublishTenant(t *testing.T) {
|
||||
Convey("TestService_PublishTenant", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
defer db.Close()
|
||||
|
||||
Convey("truncate all tables", func() {
|
||||
So(dbUtil.TruncateAllTables(context.TODO(), db, "medias", "media_resources"), ShouldBeNil)
|
||||
So(dbUtil.TruncateAllTables(context.TODO(), db, "medias"), ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("create media's resources", func() {
|
||||
items := []model.MediaResources{
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
u1, err := uuid.Parse("6c63c4d8-b2cb-4588-8dd3-9a2276918d17")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
u2, err := uuid.Parse("8c183427-02b1-4426-ad65-18c6e7735072")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
u3, err := uuid.Parse("24aa56a1-a85b-4a03-9a1b-c39a36103ddf")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("insert some data", func() {
|
||||
items := []model.Medias{
|
||||
{UUID: u1, TenantID: 1, Title: "title1", Description: "hello", Price: 100, Publish: true},
|
||||
{UUID: u2, TenantID: 1, Title: "title2", Description: "hello", Price: 100, Publish: true},
|
||||
{UUID: u3, TenantID: 1, Title: "title3", Description: "hello", Price: 100, Publish: true},
|
||||
}
|
||||
|
||||
tbl := table.MediaResources
|
||||
stmt := tbl.INSERT(
|
||||
tbl.MediaID,
|
||||
tbl.Type,
|
||||
tbl.Size,
|
||||
tbl.Publish,
|
||||
).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
Convey("publish tenant", func() {
|
||||
svc := &Service{
|
||||
db: db,
|
||||
storageConfig: &storage.Config{
|
||||
Path: "/projects/mp-qvyun/backend/fixtures/medias",
|
||||
},
|
||||
}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
err := svc.PublishTenantMedia(context.TODO(), 1, items[0].UUID, items[0].Title)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("decorate list resources", func() {
|
||||
items := []ListItem{
|
||||
{Medias: model.Medias{ID: 1}},
|
||||
{Medias: model.Medias{ID: 2}},
|
||||
}
|
||||
err = svc.PublishTenantMedia(context.TODO(), 1, items[1].UUID, items[1].Title)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
svc := &Service{db: db}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
|
||||
items, err := svc.DecorateListResources(context.TODO(), 1, 1, items)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
for _, item := range items {
|
||||
So(item.MediaResources, ShouldHaveLength, 1)
|
||||
}
|
||||
err = svc.PublishTenantMedia(context.TODO(), 1, items[2].UUID, items[2].Title)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user