From 4a04f9a495193b5ea801092c9877a2856a30b114 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 3 Mar 2021 13:51:28 +0000 Subject: [PATCH] merge the posts metadata on save --- posts/handler/posts.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/posts/handler/posts.go b/posts/handler/posts.go index 865fb5b..2a3c48c 100644 --- a/posts/handler/posts.go +++ b/posts/handler/posts.go @@ -4,10 +4,10 @@ import ( "context" "time" + "github.com/micro/micro/v3/service" "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/model" - "github.com/micro/micro/v3/service" "github.com/gosimple/slug" 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 } + + // get the old post oldPost := posts[0] post := &proto.Post{ @@ -85,12 +87,24 @@ func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.Sav Tags: oldPost.Tags, Created: oldPost.Created, Updated: req.Timestamp, - Metadata: req.Metadata, - Image: req.Image, + Metadata: oldPost.Metadata, + 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 { post.Created = time.Now().Unix() } + if len(req.Image) > 0 { + post.Image = req.Image + } if len(req.Title) > 0 { post.Title = req.Title post.Slug = slug.Make(post.Title)