feat: add posts
This commit is contained in:
@@ -70,8 +70,8 @@ func (ctl *Controller) Upload(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug strin
|
||||
TenantID: tenant.ID,
|
||||
UserID: claim.UserID,
|
||||
StorageID: defaultStorage.ID,
|
||||
Hash: uploadedFile.Hash,
|
||||
Name: uploadedFile.Name,
|
||||
UUID: uploadedFile.Hash,
|
||||
MimeType: uploadedFile.MimeType,
|
||||
Size: uploadedFile.Size,
|
||||
Path: uploadedFile.Path,
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"backend/providers/otel"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
)
|
||||
@@ -40,3 +41,34 @@ func (svc *Service) Create(ctx context.Context, m *model.Medias) (*model.Medias,
|
||||
}
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
// GetMediasByHash
|
||||
func (svc *Service) GetMediasByHash(ctx context.Context, tenantID, userID int64, hashes []string) ([]*model.Medias, error) {
|
||||
_, span := otel.Start(ctx, "medias.service.GetMediasByHash")
|
||||
defer span.End()
|
||||
|
||||
hashExpr := lo.Map(hashes, func(item string, index int) Expression { return String(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...),
|
||||
),
|
||||
)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"backend/app/errorx"
|
||||
"backend/app/http/medias"
|
||||
"backend/app/http/tenants"
|
||||
"backend/app/http/users"
|
||||
"backend/app/requests"
|
||||
@@ -24,6 +25,7 @@ type Controller struct {
|
||||
hashIds *hashids.HashID
|
||||
userSvc *users.Service
|
||||
tenantSvc *tenants.Service
|
||||
mediaSvc *medias.Service
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
@@ -153,24 +155,37 @@ func (ctl *Controller) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug strin
|
||||
return err
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(medias) != len(lo.Uniq(hashes)) {
|
||||
return errorx.BadRequest
|
||||
}
|
||||
|
||||
post := &model.Posts{
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
TenantID: tenant.ID,
|
||||
UserID: user.ID,
|
||||
Title: body.Title,
|
||||
Description: body.Description,
|
||||
Content: body.Content,
|
||||
PosterAssetID: 0,
|
||||
Stage: fields.PostStagePending,
|
||||
Status: fields.PostStatusPending,
|
||||
Price: body.Price,
|
||||
Discount: body.Discount,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
TenantID: tenant.ID,
|
||||
UserID: user.ID,
|
||||
Title: body.Title,
|
||||
Description: body.Description,
|
||||
Content: body.Content,
|
||||
Stage: fields.PostStagePending,
|
||||
Status: fields.PostStatusPending,
|
||||
Price: body.Price,
|
||||
Discount: body.Discount,
|
||||
Assets: body.Assets,
|
||||
Tags: body.Tags,
|
||||
}
|
||||
|
||||
if err := ctl.svc.Create(ctx.Context(), tenant, user, post); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: trigger event && jobs
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package posts
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"backend/database/fields"
|
||||
)
|
||||
|
||||
type UserPost struct {
|
||||
@@ -32,9 +34,11 @@ type UserPostFilter struct {
|
||||
}
|
||||
|
||||
type PostBody struct {
|
||||
Title string
|
||||
Description string
|
||||
Content string
|
||||
Price int64
|
||||
Discount int16
|
||||
Title string `json:"title,omitempty"`
|
||||
Tags fields.Json[[]string] `json:"tags,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Price int64 `json:"price,omitempty"`
|
||||
Discount int16 `json:"discount,omitempty"`
|
||||
Assets fields.Json[[]fields.MediaAsset] `json:"assets,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user