mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-22 23:35:26 +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")
|
return errors.BadRequest("proto.save.input-check", "Id is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
// read by post
|
// look for an existing post
|
||||||
posts := []*proto.Post{}
|
posts := []*proto.Post{}
|
||||||
q := model.QueryEquals("id", req.Id)
|
q := model.QueryEquals("id", req.Id)
|
||||||
q.Order.Type = model.OrderTypeUnordered
|
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())
|
return errors.InternalServerError("proto.save.store-id-read", "Failed to read post by id: %v", err.Error())
|
||||||
}
|
}
|
||||||
postSlug := slug.Make(req.Title)
|
postSlug := slug.Make(req.Title)
|
||||||
|
|
||||||
// If no existing record is found, create a new one
|
// If no existing record is found, create a new one
|
||||||
if len(posts) == 0 {
|
if len(posts) == 0 {
|
||||||
post := &proto.Post{
|
post := &proto.Post{
|
||||||
@@ -125,8 +126,7 @@ func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.Sav
|
|||||||
}
|
}
|
||||||
|
|
||||||
postsWithThisSlug := []*proto.Post{}
|
postsWithThisSlug := []*proto.Post{}
|
||||||
err = p.db.Read(model.QueryEquals("slug", postSlug), &postsWithThisSlug)
|
if err := p.db.Read(model.QueryEquals("slug", postSlug), &postsWithThisSlug); err != nil {
|
||||||
if err != nil {
|
|
||||||
return errors.InternalServerError("proto.save.store-read", "Failed to read post by slug: %v", err.Error())
|
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 {
|
func (p *Posts) savePost(ctx context.Context, oldPost, post *proto.Post) error {
|
||||||
err := p.db.Create(post)
|
if err := p.db.Create(post); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,8 +159,10 @@ func (p *Posts) savePost(ctx context.Context, oldPost, post *proto.Post) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.diffTags(ctx, post.Id, oldPost.Tags, post.Tags)
|
return p.diffTags(ctx, post.Id, oldPost.Tags, post.Tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user