127 lines
3.2 KiB
Go
127 lines
3.2 KiB
Go
package medias
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
|
|
"backend/database/schemas/public/model"
|
|
"backend/database/schemas/public/table"
|
|
"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"
|
|
)
|
|
|
|
// @provider:except
|
|
type Service struct {
|
|
db *sql.DB
|
|
log *log.Entry `inject:"false"`
|
|
}
|
|
|
|
func (svc *Service) Prepare() error {
|
|
svc.log = log.WithField("module", "medias.service")
|
|
_ = Int(1)
|
|
return nil
|
|
}
|
|
|
|
// Create
|
|
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()
|
|
|
|
if m.CreatedAt.IsZero() {
|
|
m.CreatedAt = time.Now()
|
|
}
|
|
|
|
tbl := table.Medias
|
|
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
|
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
|
|
|
var media model.Medias
|
|
if err := stmt.QueryContext(ctx, svc.db, &media); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
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) GetMediasByIDs(ctx context.Context, tenantID, userID int64, ids []int64) ([]*model.Medias, error) {
|
|
_, span := otel.Start(ctx, "medias.service.GetMediasByIDs")
|
|
defer span.End()
|
|
|
|
idExprs := lo.Map(ids, func(item int64, index int) Expression { return Int64(item) })
|
|
|
|
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()))
|
|
|
|
var ret []model.Medias
|
|
if err := stmt.QueryContext(ctx, svc.db, &ret); err != nil {
|
|
return nil, err
|
|
}
|
|
return lo.Map(ret, func(item model.Medias, _ int) *model.Medias {
|
|
return &item
|
|
}), nil
|
|
}
|
|
|
|
func (svc *Service) GetMediaByID(ctx context.Context, tenantID, userID, userMediaID int64) (*model.Medias, error) {
|
|
_, span := otel.Start(ctx, "medias.service.GetMediasByHash")
|
|
defer span.End()
|
|
|
|
medias, err := svc.GetMediasByIDs(ctx, tenantID, userID, []int64{userMediaID})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(medias) == 0 {
|
|
return nil, qrm.ErrNoRows
|
|
}
|
|
|
|
return medias[0], nil
|
|
}
|
|
|
|
func (svc *Service) DeleteByID(ctx context.Context, id ...int64) error {
|
|
if len(id) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, span := otel.Start(ctx, "medias.service.DeleteByID")
|
|
defer span.End()
|
|
|
|
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()))
|
|
|
|
_, err := stmt.ExecContext(ctx, svc.db)
|
|
return err
|
|
}
|