fix: jobs
This commit is contained in:
@@ -3,6 +3,7 @@ package medias
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"backend/database/models/qvyun_v2/public/model"
|
||||
"backend/database/models/qvyun_v2/public/table"
|
||||
@@ -31,6 +32,14 @@ func (svc *Service) Create(ctx context.Context, m *model.Medias) (*model.Medias,
|
||||
_, span := otel.Start(ctx, "medias.service.Create")
|
||||
defer span.End()
|
||||
|
||||
if m.CreatedAt.IsZero() {
|
||||
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()))
|
||||
@@ -72,3 +81,30 @@ func (svc *Service) GetMediasByHash(ctx context.Context, tenantID, userID int64,
|
||||
return &item
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (svc *Service) GetMediaByHash(ctx context.Context, tenantID, userID int64, hash string) (*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 {
|
||||
return nil, err
|
||||
}
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"backend/app/requests"
|
||||
"backend/database"
|
||||
"backend/database/fields"
|
||||
"backend/database/models/qvyun_v2/public/model"
|
||||
"backend/database/models/qvyun_v2/public/table"
|
||||
"backend/providers/hashids"
|
||||
@@ -308,8 +309,10 @@ func (svc *Service) Update(ctx context.Context, tenantID, userID, postID int64,
|
||||
attribute.Int64("user.id", userID),
|
||||
attribute.Int64("post.id", postID),
|
||||
)
|
||||
tbl := table.Posts
|
||||
|
||||
post.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
UPDATE(
|
||||
tbl.MutableColumns.Except(
|
||||
@@ -332,3 +335,84 @@ func (svc *Service) Update(ctx context.Context, tenantID, userID, postID int64,
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AttachAssets to post
|
||||
func (svc *Service) AttachAssets(ctx context.Context, tenantID, userID, postID int64, mediaAssets []fields.MediaAsset) error {
|
||||
_, span := otel.Start(ctx, "users.service.AttachAssets")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.Int64("tenant.id", tenantID),
|
||||
attribute.Int64("user.id", userID),
|
||||
attribute.Int64("post.id", postID),
|
||||
)
|
||||
|
||||
post, err := svc.ForceGetPostByID(ctx, postID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
assets := append(post.Assets.Data, mediaAssets...)
|
||||
post.Assets.Data = assets
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.UpdatedAt, tbl.Assets).
|
||||
SET(
|
||||
tbl.UpdatedAt.SET(TimestampT(time.Now())),
|
||||
tbl.Assets.SET(Json(post.Assets)),
|
||||
).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(postID)).AND(
|
||||
tbl.TenantID.EQ(Int64(tenantID)).AND(
|
||||
tbl.UserID.EQ(Int64(userID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return svc.Update(ctx, tenantID, userID, postID, post)
|
||||
}
|
||||
|
||||
// UpdateMeta
|
||||
func (svc *Service) UpdateMeta(ctx context.Context, tenantID, userID, postID int64, meta fields.PostMeta) error {
|
||||
_, span := otel.Start(ctx, "users.service.UpdateMeta")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.Int64("tenant.id", tenantID),
|
||||
attribute.Int64("user.id", userID),
|
||||
attribute.Int64("post.id", postID),
|
||||
)
|
||||
|
||||
post, err := svc.ForceGetPostByID(ctx, postID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
post.Meta = meta
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.UpdatedAt, tbl.Meta).
|
||||
SET(
|
||||
tbl.UpdatedAt.SET(TimestampT(time.Now())),
|
||||
tbl.Meta.SET(Json(post.Meta)),
|
||||
).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(postID)).AND(
|
||||
tbl.TenantID.EQ(Int64(tenantID)).AND(
|
||||
tbl.UserID.EQ(Int64(userID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return svc.Update(ctx, tenantID, userID, postID, post)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user