feat: update by query

This commit is contained in:
Rogee
2024-12-19 22:56:54 +08:00
parent e3be1079b4
commit 24553e9875
13 changed files with 43 additions and 32 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
hashids "github.com/speps/go-hashids/v2"
"github.com/speps/go-hashids/v2"
)
// @provider

View File

@@ -8,7 +8,7 @@ import (
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/contracts"
"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 {
@@ -24,7 +24,6 @@ func Provide(opts ...opt.Option) error {
}); err != nil {
return err
}
if err := container.Container.Provide(func(
controller *Controller,
) (contracts.HttpRoute, error) {
@@ -38,7 +37,6 @@ func Provide(opts ...opt.Option) error {
}, atom.GroupRoutes); err != nil {
return err
}
if err := container.Container.Provide(func(
db *sql.DB,
hashIds *hashids.HashID,
@@ -56,6 +54,5 @@ func Provide(opts ...opt.Option) error {
}); err != nil {
return err
}
return nil
}

View File

@@ -18,6 +18,7 @@ import (
"backend/providers/storage"
. "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/qrm"
"github.com/grafov/m3u8"
"github.com/pkg/errors"
"github.com/samber/lo"
@@ -163,7 +164,7 @@ func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *Li
}
// 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")
tbl := table.UserMedias
@@ -229,11 +230,21 @@ 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.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(
// if hash exists then update
stmt := tbl.SELECT(tbl.ID.AS("id")).WHERE(tbl.Hash.EQ(String(item.Hash)))
log.Debug(stmt.DebugSql())
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(
tbl.Title.SET(String(item.Name)),
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.Publish.SET(Bool(true)),
tbl.UpdatedAt.SET(TimestampT(time.Now())),
),
)
log.Debug(stmt.DebugSql())
).
WHERE(tbl.ID.EQ(Int(m.ID)))
log.Debug(stmt2.DebugSql())
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return errors.Wrapf(err, "upsert media: %s %s", item.Hash, item.Name)
if _, err := stmt2.ExecContext(ctx, svc.db); err != nil {
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
}