merge the posts metadata on save

This commit is contained in:
Asim Aslam
2021-03-03 13:51:28 +00:00
parent 3c8643f200
commit 4a04f9a495

View File

@@ -4,10 +4,10 @@ import (
"context" "context"
"time" "time"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/model" "github.com/micro/micro/v3/service/model"
"github.com/micro/micro/v3/service"
"github.com/gosimple/slug" "github.com/gosimple/slug"
proto "github.com/micro/services/posts/proto" proto "github.com/micro/services/posts/proto"
@@ -75,6 +75,8 @@ func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.Sav
} }
return nil return nil
} }
// get the old post
oldPost := posts[0] oldPost := posts[0]
post := &proto.Post{ post := &proto.Post{
@@ -85,12 +87,24 @@ func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.Sav
Tags: oldPost.Tags, Tags: oldPost.Tags,
Created: oldPost.Created, Created: oldPost.Created,
Updated: req.Timestamp, Updated: req.Timestamp,
Metadata: req.Metadata, Metadata: oldPost.Metadata,
Image: req.Image, Image: oldPost.Image,
} }
// merge the metadata
for k, v := range oldPost.Metadata {
if _, ok := post.Metadata[k]; ok {
continue
}
post.Metadata[k] = v
}
if post.Created == 0 { if post.Created == 0 {
post.Created = time.Now().Unix() post.Created = time.Now().Unix()
} }
if len(req.Image) > 0 {
post.Image = req.Image
}
if len(req.Title) > 0 { if len(req.Title) > 0 {
post.Title = req.Title post.Title = req.Title
post.Slug = slug.Make(post.Title) post.Slug = slug.Make(post.Title)