From de0bba2a03a86220581c825cb0781e47498f1c33 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 3 Mar 2021 14:31:55 +0000 Subject: [PATCH] few updates to posts handler --- posts/handler/posts.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/posts/handler/posts.go b/posts/handler/posts.go index 2a3c48c..69ccf10 100644 --- a/posts/handler/posts.go +++ b/posts/handler/posts.go @@ -45,15 +45,16 @@ func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.Sav return errors.BadRequest("proto.save.input-check", "Id is missing") } - // read by post + // look for an existing post posts := []*proto.Post{} q := model.QueryEquals("id", req.Id) q.Order.Type = model.OrderTypeUnordered - err := p.db.Read(q, &posts) - if err != nil { + + if err := p.db.Read(q, &posts); err != nil { return errors.InternalServerError("proto.save.store-id-read", "Failed to read post by id: %v", err.Error()) } postSlug := slug.Make(req.Title) + // If no existing record is found, create a new one if len(posts) == 0 { post := &proto.Post{ @@ -125,8 +126,7 @@ func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.Sav } postsWithThisSlug := []*proto.Post{} - err = p.db.Read(model.QueryEquals("slug", postSlug), &postsWithThisSlug) - if err != nil { + if err := p.db.Read(model.QueryEquals("slug", postSlug), &postsWithThisSlug); err != nil { return errors.InternalServerError("proto.save.store-read", "Failed to read post by slug: %v", err.Error()) } @@ -140,8 +140,7 @@ func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.Sav } func (p *Posts) savePost(ctx context.Context, oldPost, post *proto.Post) error { - err := p.db.Create(post) - if err != nil { + if err := p.db.Create(post); err != nil { return err } @@ -160,8 +159,10 @@ func (p *Posts) savePost(ctx context.Context, oldPost, post *proto.Post) error { return err } } + return nil } + return p.diffTags(ctx, post.Id, oldPost.Tags, post.Tags) }