mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-23 07:41:25 +00:00
merge the posts metadata on save
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user