mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-14 03:54:47 +00:00
few updates to posts handler
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user