feat: update posts
This commit is contained in:
@@ -99,7 +99,7 @@ func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
|
||||
post.Assets = fields.ToJson(assets)
|
||||
}
|
||||
|
||||
if err := model.PostsModel.Create(ctx.Context(), &post); err != nil {
|
||||
if err := post.Create(ctx.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -111,27 +111,18 @@ func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
|
||||
// @Bind id path
|
||||
// @Bind form body
|
||||
func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *PostForm) error {
|
||||
oldPost, err := model.PostsModel.GetByID(ctx.Context(), id)
|
||||
post, err := model.PostsModel.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
post := &model.Posts{
|
||||
Title: form.Title,
|
||||
HeadImages: fields.ToJson(form.HeadImages),
|
||||
Price: form.Price,
|
||||
Discount: form.Discount,
|
||||
Description: form.Introduction,
|
||||
Status: form.Status,
|
||||
Content: form.Content,
|
||||
Tags: fields.Json[[]string]{},
|
||||
Assets: fields.Json[[]fields.MediaAsset]{},
|
||||
CreatedAt: oldPost.CreatedAt,
|
||||
UpdatedAt: oldPost.UpdatedAt,
|
||||
DeletedAt: oldPost.DeletedAt,
|
||||
Views: oldPost.Views,
|
||||
Likes: oldPost.Likes,
|
||||
}
|
||||
post.Title = form.Title
|
||||
post.HeadImages = fields.ToJson(form.HeadImages)
|
||||
post.Price = form.Price
|
||||
post.Discount = form.Discount
|
||||
post.Description = form.Introduction
|
||||
post.Status = form.Status
|
||||
post.Content = form.Content
|
||||
post.Tags = fields.Json[[]string]{}
|
||||
|
||||
if form.Medias != nil {
|
||||
medias, err := model.MediasModel.GetByIds(ctx.Context(), form.Medias)
|
||||
@@ -148,7 +139,7 @@ func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *PostForm) error {
|
||||
post.Assets = fields.ToJson(assets)
|
||||
}
|
||||
|
||||
if err := model.PostsModel.Update(ctx.Context(), id, post); err != nil {
|
||||
if err := post.Update(ctx.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -167,7 +158,7 @@ func (ctl *posts) Delete(ctx fiber.Ctx, id int64) error {
|
||||
return fiber.ErrNotFound
|
||||
}
|
||||
|
||||
if err := model.PostsModel.DeleteByID(ctx.Context(), id); err != nil {
|
||||
if err := post.Delete(ctx.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -207,15 +198,17 @@ func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
|
||||
// @Bind id path
|
||||
// @Bind userId path
|
||||
func (ctl *posts) SendTo(ctx fiber.Ctx, id, userId int64) error {
|
||||
if _, err := model.PostsModel.GetByID(ctx.Context(), id); err != nil {
|
||||
post, err := model.PostsModel.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := model.UsersModel.GetByID(ctx.Context(), userId); err != nil {
|
||||
user, err := model.UsersModel.GetByID(ctx.Context(), userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := model.PostsModel.SendTo(ctx.Context(), id, userId); err != nil {
|
||||
if err := post.SendTo(ctx.Context(), user.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -184,7 +184,7 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er
|
||||
log.WithError(err).Errorf("GetByID err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
go model.PostsModel.IncrViewCount(ctx.Context(), post.ID)
|
||||
go post.IncrViewCount(ctx.Context())
|
||||
|
||||
for _, asset := range post.Assets.Data {
|
||||
if asset.Type == "video/mp4" && asset.Metas != nil && asset.Metas.Short == preview {
|
||||
|
||||
Reference in New Issue
Block a user