fix: create post
This commit is contained in:
@@ -3,9 +3,11 @@ package admin
|
||||
import (
|
||||
"quyun/app/models"
|
||||
"quyun/app/requests"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type ListQuery struct {
|
||||
@@ -24,10 +26,48 @@ func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
|
||||
return models.Posts.List(ctx.Context(), pagination, cond)
|
||||
}
|
||||
|
||||
type PostForm struct {
|
||||
Title string `json:"title"`
|
||||
Price int64 `json:"price"`
|
||||
Discount int16 `json:"discount"`
|
||||
Introduction string `json:"introduction"`
|
||||
Medias []int64 `json:"medias"`
|
||||
Status fields.PostStatus `json:"status"`
|
||||
}
|
||||
|
||||
// Create
|
||||
// @Router /v1/admin/posts [post]
|
||||
// @Bind form body
|
||||
func (ctl *posts) Create(ctx fiber.Ctx, form *model.Posts) error {
|
||||
func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
|
||||
post := model.Posts{
|
||||
Title: form.Title,
|
||||
Price: form.Price,
|
||||
Discount: form.Discount,
|
||||
Description: form.Introduction,
|
||||
Status: form.Status,
|
||||
Content: "",
|
||||
Tags: fields.Json[[]string]{},
|
||||
Assets: fields.Json[[]fields.MediaAsset]{},
|
||||
}
|
||||
|
||||
if form.Medias != nil {
|
||||
medias, err := models.Medias.GetByIds(ctx.Context(), form.Medias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
assets := lo.Map(medias, func(media *model.Medias, _ int) fields.MediaAsset {
|
||||
return fields.MediaAsset{
|
||||
Type: models.Medias.ConvertFileTypeByMimeType(media.MimeType),
|
||||
Media: media.ID,
|
||||
Mark: nil,
|
||||
}
|
||||
})
|
||||
post.Assets = fields.ToJson(assets)
|
||||
}
|
||||
|
||||
if err := models.Posts.Create(ctx.Context(), &post); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
|
||||
router.Post("/v1/admin/posts", Func1(
|
||||
r.posts.Create,
|
||||
Body[model.Posts]("form"),
|
||||
Body[PostForm]("form"),
|
||||
))
|
||||
|
||||
router.Put("/v1/admin/posts/:id", Func2(
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
@@ -12,26 +13,14 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type MediaType string
|
||||
|
||||
const (
|
||||
MediaTypeUnknown MediaType = "unknown"
|
||||
MediaTypeArchive MediaType = "archive"
|
||||
MediaTypeImage MediaType = "image"
|
||||
MediaTypeVideo MediaType = "video"
|
||||
MediaTypeDocument MediaType = "document"
|
||||
MediaTypeAudio MediaType = "audio"
|
||||
MediaTypePDF MediaType = "pdf"
|
||||
)
|
||||
|
||||
type MediaItem struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
UploadTime string `json:"upload_time"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
MimeType string `json:"media_type"`
|
||||
FileType MediaType `json:"file_type"`
|
||||
ThumbnailUrl string `json:"thumbnail_url"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
UploadTime string `json:"upload_time"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
MimeType string `json:"media_type"`
|
||||
FileType fields.MediaAssetType `json:"file_type"`
|
||||
ThumbnailUrl string `json:"thumbnail_url"`
|
||||
}
|
||||
|
||||
// @provider
|
||||
@@ -150,30 +139,57 @@ func (m *mediasModel) Create(ctx context.Context, model *model.Medias) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mediasModel) ConvertFileTypeByMimeType(mimeType string) MediaType {
|
||||
func (m *mediasModel) ConvertFileTypeByMimeType(mimeType string) fields.MediaAssetType {
|
||||
switch mimeType {
|
||||
case "image/jpeg", "image/jpg", "image/png":
|
||||
return MediaTypeImage
|
||||
case "video/mp4":
|
||||
return MediaTypeVideo
|
||||
case "image/jpeg", "image/jpg", "image/png", "image/gif":
|
||||
return fields.MediaAssetTypeImage
|
||||
case "video/mp4", "video/x-m4v":
|
||||
return fields.MediaAssetTypeVideo
|
||||
case "audio/mpeg":
|
||||
return MediaTypeAudio
|
||||
case "application/pdf":
|
||||
return MediaTypePDF
|
||||
case "application/msword",
|
||||
return fields.MediaAssetTypeAudio
|
||||
case "application/pdf",
|
||||
"application/msword",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"application/vnd.ms-excel",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"application/vnd.ms-powerpoint",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
|
||||
return MediaTypeDocument
|
||||
return fields.MediaAssetTypeDocument
|
||||
case "application/rar",
|
||||
"application/x-rar-compressed",
|
||||
"application/x-zip-compressed",
|
||||
"application/x-zip",
|
||||
"application/zip",
|
||||
"application/x-7z-compressed":
|
||||
return MediaTypeArchive
|
||||
return fields.MediaAssetTypeArchive
|
||||
}
|
||||
return MediaTypeUnknown
|
||||
return fields.MediaAssetTypeUnknown
|
||||
}
|
||||
|
||||
// GetByIds
|
||||
func (m *mediasModel) GetByIds(ctx context.Context, ids []int64) ([]*model.Medias, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||
return Int64(id)
|
||||
})
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(tbl.ID.IN(condIds...))
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var medias []model.Medias
|
||||
err := stmt.QueryContext(ctx, db, &medias)
|
||||
if err != nil {
|
||||
m.log.Errorf("error querying media items: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return lo.Map(medias, func(media model.Medias, _ int) *model.Medias {
|
||||
return &media
|
||||
}), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user