feat: update by query
This commit is contained in:
@@ -12,6 +12,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ func (d *DiscoverMedias) getFileMD5(filePath string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// extractAudio extract audio from video
|
// extractAudio extract audio from video
|
||||||
func (d *DiscoverMedias) extractAudio(video string, output string) error {
|
func (d *DiscoverMedias) extractAudio(video, output string) error {
|
||||||
args := []string{
|
args := []string{
|
||||||
"-i", video,
|
"-i", video,
|
||||||
"-vn",
|
"-vn",
|
||||||
@@ -303,7 +303,7 @@ func (d *DiscoverMedias) runCleanup(to string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getSnapshot get the snapshot of target seconds of a video
|
// getSnapshot get the snapshot of target seconds of a video
|
||||||
func (d *DiscoverMedias) ffmpegVideoToPoster(video string, output string) error {
|
func (d *DiscoverMedias) ffmpegVideoToPoster(video, output string) error {
|
||||||
args := []string{
|
args := []string{
|
||||||
"-y", // 添加 -y 参数强制覆盖
|
"-y", // 添加 -y 参数强制覆盖
|
||||||
"-i", video,
|
"-i", video,
|
||||||
|
|||||||
@@ -21,6 +21,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
userSvc *users.Service,
|
userSvc *users.Service,
|
||||||
) (*Create, error) {
|
) (*Create, error) {
|
||||||
@@ -35,7 +34,6 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
userSvc *users.Service,
|
userSvc *users.Service,
|
||||||
) (*Expire, error) {
|
) (*Expire, error) {
|
||||||
@@ -49,6 +47,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
hashids "github.com/speps/go-hashids/v2"
|
"github.com/speps/go-hashids/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @provider
|
// @provider
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"git.ipao.vip/rogeecn/atom/container"
|
"git.ipao.vip/rogeecn/atom/container"
|
||||||
"git.ipao.vip/rogeecn/atom/contracts"
|
"git.ipao.vip/rogeecn/atom/contracts"
|
||||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||||
hashids "github.com/speps/go-hashids/v2"
|
"github.com/speps/go-hashids/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Provide(opts ...opt.Option) error {
|
func Provide(opts ...opt.Option) error {
|
||||||
@@ -24,7 +24,6 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
controller *Controller,
|
controller *Controller,
|
||||||
) (contracts.HttpRoute, error) {
|
) (contracts.HttpRoute, error) {
|
||||||
@@ -38,7 +37,6 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}, atom.GroupRoutes); err != nil {
|
}, atom.GroupRoutes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
db *sql.DB,
|
db *sql.DB,
|
||||||
hashIds *hashids.HashID,
|
hashIds *hashids.HashID,
|
||||||
@@ -56,6 +54,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"backend/providers/storage"
|
"backend/providers/storage"
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
"github.com/go-jet/jet/v2/qrm"
|
||||||
"github.com/grafov/m3u8"
|
"github.com/grafov/m3u8"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
@@ -163,7 +164,7 @@ func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *Li
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetUserBoughtMedias
|
// GetUserBoughtMedias
|
||||||
func (svc *Service) GetUserBoughtMedias(ctx context.Context, tenant int64, userID int64) ([]int64, error) {
|
func (svc *Service) GetUserBoughtMedias(ctx context.Context, tenant, userID int64) ([]int64, error) {
|
||||||
log := svc.log.WithField("method", "GetUserBoughtMedias")
|
log := svc.log.WithField("method", "GetUserBoughtMedias")
|
||||||
|
|
||||||
tbl := table.UserMedias
|
tbl := table.UserMedias
|
||||||
@@ -229,11 +230,21 @@ func (svc *Service) Upsert(ctx context.Context, tenantId int64, item media_store
|
|||||||
}
|
}
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := table.Medias
|
||||||
stmt := tbl.
|
// if hash exists then update
|
||||||
INSERT(tbl.TenantID, tbl.Hash, tbl.Title, tbl.Price, tbl.Duration, tbl.Resources, tbl.Publish).
|
stmt := tbl.SELECT(tbl.ID.AS("id")).WHERE(tbl.Hash.EQ(String(item.Hash)))
|
||||||
VALUES(Int(tenantId), String(item.Hash), String(item.Name), Int(item.Price()), Int(item.Duration), Json(resources.MustValue()), Bool(true)).
|
log.Debug(stmt.DebugSql())
|
||||||
ON_CONFLICT(tbl.Hash).
|
|
||||||
DO_UPDATE(
|
var m struct {
|
||||||
|
ID int64
|
||||||
|
}
|
||||||
|
if err := stmt.QueryContext(ctx, svc.db, &m); err != nil && !errors.Is(err, qrm.ErrNoRows) {
|
||||||
|
return errors.Wrapf(err, "query media by hash %s", item.Hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.ID > 0 {
|
||||||
|
// update media
|
||||||
|
stmt2 := tbl.
|
||||||
|
UPDATE().
|
||||||
SET(
|
SET(
|
||||||
tbl.Title.SET(String(item.Name)),
|
tbl.Title.SET(String(item.Name)),
|
||||||
tbl.Price.SET(Int(item.Price())),
|
tbl.Price.SET(Int(item.Price())),
|
||||||
@@ -241,14 +252,29 @@ func (svc *Service) Upsert(ctx context.Context, tenantId int64, item media_store
|
|||||||
tbl.Duration.SET(Int(item.Duration)),
|
tbl.Duration.SET(Int(item.Duration)),
|
||||||
tbl.Publish.SET(Bool(true)),
|
tbl.Publish.SET(Bool(true)),
|
||||||
tbl.UpdatedAt.SET(TimestampT(time.Now())),
|
tbl.UpdatedAt.SET(TimestampT(time.Now())),
|
||||||
),
|
).
|
||||||
)
|
WHERE(tbl.ID.EQ(Int(m.ID)))
|
||||||
log.Debug(stmt.DebugSql())
|
log.Debug(stmt2.DebugSql())
|
||||||
|
|
||||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
if _, err := stmt2.ExecContext(ctx, svc.db); err != nil {
|
||||||
return errors.Wrapf(err, "upsert media: %s %s", item.Hash, item.Name)
|
return errors.Wrapf(err, "update media: %s %s", item.Hash, item.Name)
|
||||||
|
}
|
||||||
|
svc.log.Infof("update media: %d %s %s", m.ID, item.Hash, item.Name)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stmt3 := tbl.
|
||||||
|
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_NOTHING()
|
||||||
|
log.Debug(stmt3.DebugSql())
|
||||||
|
|
||||||
|
if _, err := stmt3.ExecContext(ctx, svc.db); err != nil {
|
||||||
|
return errors.Wrapf(err, "insert into media: %s %s", item.Hash, item.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
svc.log.Infof("insert media: %s %s", item.Hash, item.Name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
hashids "github.com/speps/go-hashids/v2"
|
"github.com/speps/go-hashids/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @provider
|
// @provider
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"git.ipao.vip/rogeecn/atom/container"
|
"git.ipao.vip/rogeecn/atom/container"
|
||||||
"git.ipao.vip/rogeecn/atom/contracts"
|
"git.ipao.vip/rogeecn/atom/contracts"
|
||||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||||
hashids "github.com/speps/go-hashids/v2"
|
"github.com/speps/go-hashids/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Provide(opts ...opt.Option) error {
|
func Provide(opts ...opt.Option) error {
|
||||||
@@ -23,7 +23,6 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
controller *Controller,
|
controller *Controller,
|
||||||
) (contracts.HttpRoute, error) {
|
) (contracts.HttpRoute, error) {
|
||||||
@@ -37,7 +36,6 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}, atom.GroupRoutes); err != nil {
|
}, atom.GroupRoutes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
db *sql.DB,
|
db *sql.DB,
|
||||||
hashIds *hashids.HashID,
|
hashIds *hashids.HashID,
|
||||||
@@ -53,6 +51,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,5 @@ func Provide(opts ...opt.Option) error {
|
|||||||
}, atom.GroupInitial); err != nil {
|
}, atom.GroupInitial); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user