feat: add medias
This commit is contained in:
BIN
backend/modules/medias/__debug_bin790297978
Executable file
BIN
backend/modules/medias/__debug_bin790297978
Executable file
Binary file not shown.
30
backend/modules/medias/controller.go
Normal file
30
backend/modules/medias/controller.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"backend/common/errorx"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
. "github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type Controller struct {
|
||||
svc *Service
|
||||
}
|
||||
|
||||
// List
|
||||
func (c *Controller) List(ctx fiber.Ctx) error {
|
||||
filter := ListFilter{}
|
||||
if err := ctx.Bind().Body(&filter); err != nil {
|
||||
return ctx.Status(fiber.StatusBadRequest).JSON(errorx.RequestParseError)
|
||||
}
|
||||
|
||||
tenantId, userId := ToInt64(ctx.Locals("tenantId")), ToInt64(ctx.Locals("userId"))
|
||||
|
||||
items, err := c.svc.List(ctx.Context(), tenantId, userId, &filter)
|
||||
if err != nil {
|
||||
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
|
||||
}
|
||||
|
||||
return ctx.JSON(items)
|
||||
}
|
||||
18
backend/modules/medias/dto.go
Normal file
18
backend/modules/medias/dto.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/pkg/db"
|
||||
)
|
||||
|
||||
type ListFilter struct {
|
||||
db.Pagination
|
||||
Title *string `json:"title"`
|
||||
Bought *bool `json:"bought"`
|
||||
}
|
||||
|
||||
type ListItem struct {
|
||||
model.Medias
|
||||
Bought bool `json:"bought"`
|
||||
MediaResources []model.MediaResources `json:"media_resources"`
|
||||
}
|
||||
54
backend/modules/medias/provider.gen.go
Executable file
54
backend/modules/medias/provider.gen.go
Executable file
@@ -0,0 +1,54 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"backend/providers/http"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
svc *Service,
|
||||
) (*Controller, error) {
|
||||
obj := &Controller{
|
||||
svc: svc,
|
||||
}
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(func(
|
||||
controller *Controller,
|
||||
http *http.Service,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Router{
|
||||
controller: controller,
|
||||
http: http,
|
||||
}
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(func(
|
||||
db *sql.DB,
|
||||
) (*Service, error) {
|
||||
obj := &Service{
|
||||
db: db,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
26
backend/modules/medias/router.go
Executable file
26
backend/modules/medias/router.go
Executable file
@@ -0,0 +1,26 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"backend/providers/http"
|
||||
|
||||
_ "git.ipao.vip/rogeecn/atom"
|
||||
_ "git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider:except contracts.HttpRoute atom.GroupRoutes
|
||||
type Router struct {
|
||||
http *http.Service
|
||||
|
||||
controller *Controller
|
||||
}
|
||||
|
||||
func (r *Router) Register() error {
|
||||
group := r.http.Engine.Group("medias")
|
||||
log.Infof("register route group: %s", group.(*fiber.Group).Prefix)
|
||||
group.Get("", r.controller.List)
|
||||
group.Get("{id}", r.controller.List)
|
||||
|
||||
return nil
|
||||
}
|
||||
164
backend/modules/medias/service.go
Normal file
164
backend/modules/medias/service.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (svc *Service) Prepare() error {
|
||||
svc.log = logrus.WithField("module", "medias.service")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetByID
|
||||
func (svc *Service) GetByID(ctx context.Context, id int64) (*ListItem, error) {
|
||||
log := svc.log.WithField("method", "GetByID")
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int(id)))
|
||||
log.Debug(stmt.Sql())
|
||||
|
||||
var media ListItem
|
||||
if err := stmt.QueryContext(ctx, svc.db, &media); err != nil {
|
||||
return nil, errors.Wrap(err, "query media by id")
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(tbl.TenantID.EQ(Int(tenantId))).
|
||||
ORDER_BY(tbl.ID.DESC())
|
||||
|
||||
if filter.Title != nil && *filter.Title != "" {
|
||||
stmt = stmt.WHERE(tbl.Title.LIKE(String("%" + *filter.Title + "%")))
|
||||
}
|
||||
|
||||
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 {
|
||||
stmt = stmt.
|
||||
WHERE(tbl.ID.IN(lo.Map(boughtIDs, func(item int64, _ int) Expression {
|
||||
return Int(item)
|
||||
})...))
|
||||
}
|
||||
} else {
|
||||
stmt = stmt.WHERE(tbl.Publish.EQ(Bool(true)))
|
||||
}
|
||||
|
||||
if filter.OffsetID > 0 {
|
||||
if filter.Action == 0 {
|
||||
stmt = stmt.WHERE(tbl.ID.GT(Int(filter.OffsetID)))
|
||||
}
|
||||
|
||||
if filter.Action == 1 {
|
||||
stmt = stmt.WHERE(tbl.ID.LT(Int(filter.OffsetID)))
|
||||
stmt = stmt.LIMIT(10)
|
||||
}
|
||||
} else {
|
||||
stmt = stmt.LIMIT(10)
|
||||
}
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var dest []ListItem
|
||||
if err := stmt.QueryContext(ctx, svc.db, &dest); err != nil {
|
||||
return nil, errors.Wrap(err, "query medias")
|
||||
}
|
||||
|
||||
return dest, nil
|
||||
}
|
||||
|
||||
// GetUserBoughtMedias
|
||||
func (svc *Service) GetUserBoughtMedias(ctx context.Context, tenant int64, userID int64) ([]int64, error) {
|
||||
log := svc.log.WithField("method", "GetUserBoughtMedias")
|
||||
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.MediaID).
|
||||
WHERE(
|
||||
tbl.TenantID.EQ(Int(tenant)).AND(
|
||||
tbl.UserID.EQ(Int(userID)),
|
||||
),
|
||||
)
|
||||
log.Debug(stmt.Sql())
|
||||
|
||||
var mediaIDs []int64
|
||||
if err := stmt.QueryContext(ctx, svc.db, &mediaIDs); err != nil {
|
||||
return nil, errors.Wrap(err, "query user bought medias")
|
||||
}
|
||||
|
||||
return mediaIDs, nil
|
||||
}
|
||||
280
backend/modules/medias/service_test.go
Normal file
280
backend/modules/medias/service_test.go
Normal file
@@ -0,0 +1,280 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/fixtures"
|
||||
dbUtil "backend/pkg/db"
|
||||
"backend/pkg/pg"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestService_GetUserBoughtMedias(t *testing.T) {
|
||||
Convey("TestService_GetUserBoughtMedias", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
defer db.Close()
|
||||
|
||||
So(dbUtil.TruncateAllTables(context.TODO(), db, "user_medias"), ShouldBeNil)
|
||||
|
||||
Convey("insert some data", func() {
|
||||
items := []model.UserMedias{
|
||||
{UserID: 1, TenantID: 1, MediaID: 1, Price: 10},
|
||||
{UserID: 1, TenantID: 1, MediaID: 2, Price: 10},
|
||||
{UserID: 1, TenantID: 1, MediaID: 3, Price: 10},
|
||||
}
|
||||
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.INSERT(tbl.UserID, tbl.TenantID, tbl.MediaID, tbl.Price).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("get user bought medias", func() {
|
||||
svc := &Service{db: db}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
|
||||
ids, err := svc.GetUserBoughtMedias(context.TODO(), 1, 1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
for _, id := range ids {
|
||||
So(lo.Contains([]int64{1, 2, 3}, id), ShouldBeTrue)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_List(t *testing.T) {
|
||||
Convey("TestService_list", 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", "user_medias"), ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("insert user_medias data", func() {
|
||||
items := []model.UserMedias{
|
||||
{UserID: 1, TenantID: 1, MediaID: 1, Price: 10},
|
||||
}
|
||||
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.INSERT(tbl.UserID, tbl.TenantID, tbl.MediaID, tbl.Price).MODELS(items)
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("insert medias data", func() {
|
||||
items := []model.Medias{
|
||||
{
|
||||
TenantID: 1,
|
||||
Title: "title1",
|
||||
Description: "hello",
|
||||
Price: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
TenantID: 1,
|
||||
Title: "title2",
|
||||
Description: "hello",
|
||||
Price: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
TenantID: 1,
|
||||
Title: "title3",
|
||||
Description: "hello",
|
||||
Price: 100,
|
||||
Publish: false,
|
||||
},
|
||||
}
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.INSERT(
|
||||
tbl.TenantID,
|
||||
tbl.Title,
|
||||
tbl.Description,
|
||||
tbl.Price,
|
||||
tbl.Publish,
|
||||
).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("create media's resources", func() {
|
||||
items := []model.MediaResources{
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 3,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 3,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
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)
|
||||
|
||||
items, err := svc.List(context.TODO(), 1, 1, &ListFilter{
|
||||
Bought: lo.ToPtr(true),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
t.Logf("items: %+v", items)
|
||||
So(items, ShouldHaveLength, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_List1(t *testing.T) {
|
||||
Convey("TestService_list", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
defer db.Close()
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.Publish.EQ(Bool(true)).AND(
|
||||
tbl.TenantID.EQ(Int(1)),
|
||||
),
|
||||
).
|
||||
WHERE(tbl.ID.EQ(Int(1))).
|
||||
ORDER_BY(tbl.ID.DESC())
|
||||
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
type list struct {
|
||||
Media model.Medias `alias:"ListItem.*"`
|
||||
}
|
||||
|
||||
var dest []ListItem
|
||||
err = stmt.QueryContext(context.TODO(), db, &dest)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
t.Logf("dest: %+v", dest)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_DecorateListResources(t *testing.T) {
|
||||
Convey("TestService_DecorateListResources", 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)
|
||||
})
|
||||
|
||||
Convey("create media's resources", func() {
|
||||
items := []model.MediaResources{
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
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("decorate list resources", func() {
|
||||
items := []ListItem{
|
||||
{Medias: model.Medias{ID: 1}},
|
||||
{Medias: model.Medias{ID: 2}},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user