feat: update medias
This commit is contained in:
@@ -52,17 +52,9 @@ func (ctl *Controller) Upload(ctx fiber.Ctx, claim *jwt.Claims, file *multipart.
|
||||
return uploadedFile, nil
|
||||
}
|
||||
|
||||
uploadedFile, err = storage.Build(defaultStorage).Save(ctx.Context(), uploadedFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// save to db
|
||||
_, err = ctl.svc.Create(ctx.Context(), &model.Medias{
|
||||
userMediaID, err := ctl.svc.Create(ctx.Context(), *claim.TenantID, claim.UserID, &model.Medias{
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
TenantID: *claim.TenantID,
|
||||
UserID: claim.UserID,
|
||||
StorageID: defaultStorage.ID,
|
||||
Hash: uploadedFile.Hash,
|
||||
Name: uploadedFile.Name,
|
||||
@@ -70,6 +62,7 @@ func (ctl *Controller) Upload(ctx fiber.Ctx, claim *jwt.Claims, file *multipart.
|
||||
Size: uploadedFile.Size,
|
||||
Path: uploadedFile.Path,
|
||||
})
|
||||
uploadedFile.ID = userMediaID
|
||||
uploadedFile.Preview = ""
|
||||
|
||||
return uploadedFile, err
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"backend/providers/otel"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
@@ -28,7 +29,7 @@ func (svc *Service) Prepare() error {
|
||||
}
|
||||
|
||||
// Create
|
||||
func (svc *Service) Create(ctx context.Context, m *model.Medias) (*model.Medias, error) {
|
||||
func (svc *Service) Create(ctx context.Context, tenantID, userID int64, m *model.Medias) (int64, error) {
|
||||
_, span := otel.Start(ctx, "medias.service.Create")
|
||||
defer span.End()
|
||||
|
||||
@@ -36,40 +37,50 @@ func (svc *Service) Create(ctx context.Context, m *model.Medias) (*model.Medias,
|
||||
m.CreatedAt = time.Now()
|
||||
}
|
||||
|
||||
if m.UpdatedAt.IsZero() {
|
||||
m.UpdatedAt = time.Now()
|
||||
}
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var ret model.Medias
|
||||
if err := stmt.QueryContext(ctx, svc.db, &ret); err != nil {
|
||||
return nil, err
|
||||
var media model.Medias
|
||||
if err := stmt.QueryContext(ctx, svc.db, &media); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return &ret, nil
|
||||
|
||||
userMediaTbl := table.UserMedias
|
||||
userMediaStmt := userMediaTbl.INSERT(userMediaTbl.TenantID, userMediaTbl.UserID, userMediaTbl.MediaID).VALUES(tenantID, userID, media.ID).RETURNING(tbl.AllColumns)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(userMediaStmt.DebugSql()))
|
||||
|
||||
var ret model.UserMedias
|
||||
if err := userMediaStmt.QueryContext(ctx, svc.db, &ret); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return ret.ID, nil
|
||||
}
|
||||
|
||||
// GetMediasByHash
|
||||
func (svc *Service) GetMediasByHash(ctx context.Context, tenantID, userID int64, hashes []string) ([]*model.Medias, error) {
|
||||
_, span := otel.Start(ctx, "medias.service.GetMediasByHash")
|
||||
func (svc *Service) GetMediasByIDs(ctx context.Context, tenantID, userID int64, ids []int64) ([]*model.Medias, error) {
|
||||
_, span := otel.Start(ctx, "medias.service.GetMediasByIDs")
|
||||
defer span.End()
|
||||
|
||||
hashExpr := lo.Map(hashes, func(item string, index int) Expression { return String(item) })
|
||||
idExprs := lo.Map(ids, func(item int64, index int) Expression { return Int64(item) })
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.TenantID.
|
||||
EQ(Int64(tenantID)).
|
||||
AND(
|
||||
tbl.UserID.EQ(Int64(userID)),
|
||||
).
|
||||
AND(
|
||||
tbl.Hash.IN(hashExpr...),
|
||||
),
|
||||
stmt := SELECT(table.Medias.AllColumns).
|
||||
FROM(
|
||||
table.Medias.RIGHT_JOIN(
|
||||
table.UserMedias,
|
||||
table.UserMedias.TenantID.
|
||||
EQ(Int64(tenantID)).
|
||||
AND(
|
||||
table.UserMedias.UserID.EQ(Int64(userID)),
|
||||
).
|
||||
AND(
|
||||
table.UserMedias.ID.IN(idExprs...),
|
||||
).
|
||||
AND(
|
||||
table.Medias.ID.EQ(table.UserMedias.MediaID),
|
||||
),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
@@ -82,31 +93,20 @@ func (svc *Service) GetMediasByHash(ctx context.Context, tenantID, userID int64,
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (svc *Service) GetMediaByHash(ctx context.Context, tenantID, userID int64, hash string) (*model.Medias, error) {
|
||||
func (svc *Service) GetMediaByID(ctx context.Context, tenantID, userID, userMediaID int64) (*model.Medias, error) {
|
||||
_, span := otel.Start(ctx, "medias.service.GetMediasByHash")
|
||||
defer span.End()
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
LIMIT(1).
|
||||
WHERE(
|
||||
tbl.TenantID.
|
||||
EQ(Int64(tenantID)).
|
||||
AND(
|
||||
tbl.UserID.EQ(Int64(userID)),
|
||||
).
|
||||
AND(
|
||||
tbl.Hash.EQ(String(hash)),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var ret model.Medias
|
||||
if err := stmt.QueryContext(ctx, svc.db, &ret); err != nil {
|
||||
medias, err := svc.GetMediasByIDs(ctx, tenantID, userID, []int64{userMediaID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ret, nil
|
||||
|
||||
if len(medias) == 0 {
|
||||
return nil, qrm.ErrNoRows
|
||||
}
|
||||
|
||||
return medias[0], nil
|
||||
}
|
||||
|
||||
func (svc *Service) DeleteByID(ctx context.Context, id ...int64) error {
|
||||
@@ -117,7 +117,7 @@ func (svc *Service) DeleteByID(ctx context.Context, id ...int64) error {
|
||||
_, span := otel.Start(ctx, "medias.service.DeleteByID")
|
||||
defer span.End()
|
||||
|
||||
tbl := table.Medias
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.DELETE().WHERE(tbl.ID.IN(lo.Map(id, func(item int64, _ int) Expression { return Int64(item) })...))
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
|
||||
@@ -138,13 +138,13 @@ func (ctl *Controller) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug strin
|
||||
}
|
||||
|
||||
// check media assets exists
|
||||
hashes := lo.Map(body.Assets.Data, func(item fields.MediaAsset, _ int) string { return item.Hash })
|
||||
medias, err := ctl.mediaSvc.GetMediasByHash(ctx.Context(), tenant.ID, user.ID, hashes)
|
||||
ids := lo.Map(body.Assets.Data, func(item fields.MediaAsset, _ int) int64 { return item.Media })
|
||||
medias, err := ctl.mediaSvc.GetMediasByIDs(ctx.Context(), tenant.ID, user.ID, ids)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(medias) != len(lo.Uniq(hashes)) {
|
||||
if len(medias) != len(lo.Uniq(ids)) {
|
||||
return errorx.BadRequest
|
||||
}
|
||||
|
||||
@@ -218,13 +218,13 @@ func (ctl *Controller) Update(ctx fiber.Ctx, claim *jwt.Claims, hash string, bod
|
||||
}
|
||||
|
||||
// check media assets exists
|
||||
hashes := lo.Map(body.Assets.Data, func(item fields.MediaAsset, _ int) string { return item.Hash })
|
||||
medias, err := ctl.mediaSvc.GetMediasByHash(ctx.Context(), *claim.TenantID, post.UserID, hashes)
|
||||
ids := lo.Map(body.Assets.Data, func(item fields.MediaAsset, _ int) int64 { return item.Media })
|
||||
medias, err := ctl.mediaSvc.GetMediasByIDs(ctx.Context(), *claim.TenantID, post.UserID, ids)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(medias) != len(lo.Uniq(hashes)) {
|
||||
if len(medias) != len(lo.Uniq(ids)) {
|
||||
return errorx.BadRequest
|
||||
}
|
||||
|
||||
@@ -247,5 +247,6 @@ func (ctl *Controller) Update(ctx fiber.Ctx, claim *jwt.Claims, hash string, bod
|
||||
if err := ctl.svc.Update(ctx.Context(), post.TenantID, post.UserID, post.ID, m); err != nil {
|
||||
return err
|
||||
}
|
||||
// todo: trigger event post updated
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user