Add blog service & follow model changes in tags/posts (#22)

This commit is contained in:
Janos Dobronszki
2020-11-03 14:14:00 +01:00
committed by GitHub
parent 3642f7279d
commit b30adabfc3
20 changed files with 415 additions and 117 deletions

View File

@@ -5,7 +5,7 @@ MODIFY=Mgithub.com/micro/micro/proto/api/api.proto=github.com/micro/micro/v3/pro
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=${MODIFY}:. --go_out=${MODIFY}:. proto/posts/posts.proto
protoc --proto_path=. --micro_out=${MODIFY}:. --go_out=${MODIFY}:. proto/posts.proto
.PHONY: build

View File

@@ -11,8 +11,7 @@ import (
"github.com/micro/dev/model"
"github.com/gosimple/slug"
pb "github.com/micro/services/blog/posts/proto/posts"
posts "github.com/micro/services/blog/posts/proto/posts"
proto "github.com/micro/services/blog/posts/proto"
tags "github.com/micro/services/blog/tags/proto"
)
@@ -20,19 +19,9 @@ const (
tagType = "post-tag"
)
type Post struct {
ID string `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Content string `json:"content"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Tags []string `json:"tags"`
}
type Posts struct {
Tags tags.TagsService
db model.Table
db model.Model
}
func NewPosts(tagsService tags.TagsService) *Posts {
@@ -41,35 +30,35 @@ func NewPosts(tagsService tags.TagsService) *Posts {
return &Posts{
Tags: tagsService,
db: model.NewTable(
db: model.New(
store.DefaultStore,
"posts",
model.Indexes(model.ByEquality("slug"), createdIndex),
&model.TableOptions{
&model.ModelOptions{
Debug: false,
},
),
}
}
func (p *Posts) Save(ctx context.Context, req *posts.SaveRequest, rsp *posts.SaveResponse) error {
func (p *Posts) Save(ctx context.Context, req *proto.SaveRequest, rsp *proto.SaveResponse) error {
if len(req.Id) == 0 {
return errors.BadRequest("posts.save.input-check", "Id is missing")
return errors.BadRequest("proto.save.input-check", "Id is missing")
}
// read by post
posts := []Post{}
posts := []*proto.Post{}
q := model.Equals("id", req.Id)
q.Order.Type = model.OrderTypeUnordered
err := p.db.List(q, &posts)
if err != nil {
return errors.InternalServerError("posts.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)
// If no existing record is found, create a new one
if len(posts) == 0 {
post := &Post{
ID: req.Id,
post := &proto.Post{
Id: req.Id,
Title: req.Title,
Content: req.Content,
Tags: req.Tags,
@@ -78,14 +67,14 @@ func (p *Posts) Save(ctx context.Context, req *posts.SaveRequest, rsp *posts.Sav
}
err := p.savePost(ctx, nil, post)
if err != nil {
return errors.InternalServerError("posts.save.post-save", "Failed to save new post: %v", err.Error())
return errors.InternalServerError("proto.save.post-save", "Failed to save new post: %v", err.Error())
}
return nil
}
oldPost := &posts[0]
oldPost := posts[0]
post := &Post{
ID: req.Id,
post := &proto.Post{
Id: req.Id,
Title: oldPost.Title,
Content: oldPost.Content,
Slug: oldPost.Slug,
@@ -112,22 +101,22 @@ func (p *Posts) Save(ctx context.Context, req *posts.SaveRequest, rsp *posts.Sav
}
}
postsWithThisSlug := []Post{}
postsWithThisSlug := []*proto.Post{}
err = p.db.List(model.Equals("slug", postSlug), &postsWithThisSlug)
if err != nil {
return errors.InternalServerError("posts.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())
}
if len(postsWithThisSlug) > 0 {
if oldPost.ID != postsWithThisSlug[0].ID {
return errors.BadRequest("posts.save.slug-check", "An other post with this slug already exists")
if oldPost.Id != postsWithThisSlug[0].Id {
return errors.BadRequest("proto.save.slug-check", "An other post with this slug already exists")
}
}
return p.savePost(ctx, oldPost, post)
}
func (p *Posts) savePost(ctx context.Context, oldPost, post *Post) error {
func (p *Posts) savePost(ctx context.Context, oldPost, post *proto.Post) error {
err := p.db.Save(post)
if err != nil {
return err
@@ -135,7 +124,7 @@ func (p *Posts) savePost(ctx context.Context, oldPost, post *Post) error {
if oldPost == nil {
for _, tagName := range post.Tags {
_, err := p.Tags.Add(ctx, &tags.AddRequest{
ResourceID: post.ID,
ResourceID: post.Id,
Type: tagType,
Title: tagName,
})
@@ -145,7 +134,7 @@ func (p *Posts) savePost(ctx context.Context, oldPost, post *Post) error {
}
return nil
}
return p.diffTags(ctx, post.ID, oldPost.Tags, post.Tags)
return p.diffTags(ctx, post.Id, oldPost.Tags, post.Tags)
}
func (p *Posts) diffTags(ctx context.Context, parentID string, oldTagNames, newTagNames []string) error {
@@ -186,7 +175,7 @@ func (p *Posts) diffTags(ctx context.Context, parentID string, oldTagNames, newT
return nil
}
func (p *Posts) Query(ctx context.Context, req *pb.QueryRequest, rsp *pb.QueryResponse) error {
func (p *Posts) Query(ctx context.Context, req *proto.QueryRequest, rsp *proto.QueryResponse) error {
var q model.Query
if len(req.Slug) > 0 {
logger.Infof("Reading post by slug: %v", req.Slug)
@@ -208,15 +197,15 @@ func (p *Posts) Query(ctx context.Context, req *pb.QueryRequest, rsp *pb.QueryRe
logger.Infof("Listing posts, offset: %v, limit: %v", req.Offset, limit)
}
posts := []Post{}
posts := []*proto.Post{}
err := p.db.List(q, &posts)
if err != nil {
return errors.BadRequest("posts.query.store-read", "Failed to read from store: %v", err.Error())
return errors.BadRequest("proto.query.store-read", "Failed to read from store: %v", err.Error())
}
rsp.Posts = make([]*pb.Post, len(posts))
rsp.Posts = make([]*proto.Post, len(posts))
for i, post := range posts {
rsp.Posts[i] = &pb.Post{
Id: post.ID,
rsp.Posts[i] = &proto.Post{
Id: post.Id,
Title: post.Title,
Slug: post.Slug,
Content: post.Content,
@@ -226,7 +215,7 @@ func (p *Posts) Query(ctx context.Context, req *pb.QueryRequest, rsp *pb.QueryRe
return nil
}
func (p *Posts) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
func (p *Posts) Delete(ctx context.Context, req *proto.DeleteRequest, rsp *proto.DeleteResponse) error {
logger.Info("Received Post.Delete request")
return p.db.Delete(model.Equals("id", req.Id))
}

View File

@@ -1,5 +1,5 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: github.com/micro/services/blog/posts/proto/posts/posts.proto
// source: proto/posts.proto
package posts
@@ -38,7 +38,7 @@ func (m *Post) Reset() { *m = Post{} }
func (m *Post) String() string { return proto.CompactTextString(m) }
func (*Post) ProtoMessage() {}
func (*Post) Descriptor() ([]byte, []int) {
return fileDescriptor_ead3372a32dfec97, []int{0}
return fileDescriptor_e93dc7d934d9dc10, []int{0}
}
func (m *Post) XXX_Unmarshal(b []byte) error {
@@ -132,7 +132,7 @@ func (m *QueryRequest) Reset() { *m = QueryRequest{} }
func (m *QueryRequest) String() string { return proto.CompactTextString(m) }
func (*QueryRequest) ProtoMessage() {}
func (*QueryRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_ead3372a32dfec97, []int{1}
return fileDescriptor_e93dc7d934d9dc10, []int{1}
}
func (m *QueryRequest) XXX_Unmarshal(b []byte) error {
@@ -199,7 +199,7 @@ func (m *QueryResponse) Reset() { *m = QueryResponse{} }
func (m *QueryResponse) String() string { return proto.CompactTextString(m) }
func (*QueryResponse) ProtoMessage() {}
func (*QueryResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_ead3372a32dfec97, []int{2}
return fileDescriptor_e93dc7d934d9dc10, []int{2}
}
func (m *QueryResponse) XXX_Unmarshal(b []byte) error {
@@ -245,7 +245,7 @@ func (m *SaveRequest) Reset() { *m = SaveRequest{} }
func (m *SaveRequest) String() string { return proto.CompactTextString(m) }
func (*SaveRequest) ProtoMessage() {}
func (*SaveRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_ead3372a32dfec97, []int{3}
return fileDescriptor_e93dc7d934d9dc10, []int{3}
}
func (m *SaveRequest) XXX_Unmarshal(b []byte) error {
@@ -319,7 +319,7 @@ func (m *SaveResponse) Reset() { *m = SaveResponse{} }
func (m *SaveResponse) String() string { return proto.CompactTextString(m) }
func (*SaveResponse) ProtoMessage() {}
func (*SaveResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_ead3372a32dfec97, []int{4}
return fileDescriptor_e93dc7d934d9dc10, []int{4}
}
func (m *SaveResponse) XXX_Unmarshal(b []byte) error {
@@ -358,7 +358,7 @@ func (m *DeleteRequest) Reset() { *m = DeleteRequest{} }
func (m *DeleteRequest) String() string { return proto.CompactTextString(m) }
func (*DeleteRequest) ProtoMessage() {}
func (*DeleteRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_ead3372a32dfec97, []int{5}
return fileDescriptor_e93dc7d934d9dc10, []int{5}
}
func (m *DeleteRequest) XXX_Unmarshal(b []byte) error {
@@ -396,7 +396,7 @@ func (m *DeleteResponse) Reset() { *m = DeleteResponse{} }
func (m *DeleteResponse) String() string { return proto.CompactTextString(m) }
func (*DeleteResponse) ProtoMessage() {}
func (*DeleteResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_ead3372a32dfec97, []int{6}
return fileDescriptor_e93dc7d934d9dc10, []int{6}
}
func (m *DeleteResponse) XXX_Unmarshal(b []byte) error {
@@ -428,34 +428,32 @@ func init() {
}
func init() {
proto.RegisterFile("github.com/micro/services/blog/posts/proto/posts/posts.proto", fileDescriptor_ead3372a32dfec97)
proto.RegisterFile("proto/posts.proto", fileDescriptor_e93dc7d934d9dc10)
}
var fileDescriptor_ead3372a32dfec97 = []byte{
// 397 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x93, 0x4b, 0xee, 0xd3, 0x30,
0x10, 0xc6, 0xc9, 0xf3, 0x4f, 0xa7, 0x0f, 0x55, 0x6e, 0x41, 0x56, 0x85, 0x20, 0x64, 0xd5, 0x55,
0x23, 0x0a, 0x12, 0x1b, 0x96, 0x1c, 0x00, 0xc2, 0x09, 0xd2, 0xc4, 0x4d, 0x2d, 0x25, 0x75, 0x88,
0x27, 0x95, 0x38, 0x07, 0xa7, 0xe0, 0x0a, 0x9c, 0x0e, 0xf9, 0x55, 0xd2, 0x8a, 0xee, 0xd8, 0x44,
0xf3, 0x7d, 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, 0x13, 0x67, 0xc9, 0xc8, 0x5b,
0x30, 0x51, 0x52, 0x2f, 0x09, 0xb6, 0xd3, 0xfd, 0x74, 0x67, 0x52, 0x56, 0xa1, 0xe6, 0x36, 0xe4,
0x9f, 0x1e, 0x4c, 0xbf, 0x15, 0x17, 0xf6, 0x88, 0xf3, 0x7f, 0x64, 0xfd, 0x0a, 0x26, 0xc8, 0x5b,
0x26, 0xb1, 0x68, 0x3b, 0x4b, 0xfc, 0xd7, 0xb8, 0xa6, 0x17, 0x8f, 0xd2, 0x7b, 0x0d, 0x33, 0x03,
0x65, 0x17, 0xb9, 0xa3, 0x4a, 0xdf, 0xc0, 0xfc, 0x33, 0x6b, 0x18, 0x3e, 0xc2, 0x4e, 0x97, 0xb0,
0x70, 0x2f, 0x98, 0x2b, 0xf6, 0xbf, 0x3c, 0x88, 0xd4, 0xe2, 0x92, 0x7c, 0x80, 0x48, 0xc7, 0x44,
0x56, 0x36, 0x8f, 0xf1, 0x87, 0xda, 0xac, 0x6f, 0x4d, 0x73, 0x3a, 0x7d, 0x46, 0xde, 0x41, 0xa8,
0x90, 0x08, 0xb1, 0xfd, 0x51, 0x68, 0x9b, 0xd5, 0x8d, 0x77, 0x3d, 0xf2, 0x11, 0x62, 0x03, 0x41,
0xdc, 0xa5, 0x37, 0xd0, 0x9b, 0x17, 0x77, 0xae, 0x3b, 0x78, 0x88, 0xf5, 0x7f, 0xf0, 0xfe, 0x4f,
0x00, 0x00, 0x00, 0xff, 0xff, 0xce, 0x40, 0x57, 0x3c, 0x47, 0x03, 0x00, 0x00,
var fileDescriptor_e93dc7d934d9dc10 = []byte{
// 367 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xac, 0x53, 0x5d, 0x4e, 0xf2, 0x50,
0x10, 0xfd, 0x4a, 0x7f, 0xf8, 0x18, 0x7e, 0x82, 0x03, 0x9a, 0x1b, 0x62, 0x14, 0xfb, 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21, 0xeb, 0xdd, 0x80, 0x70, 0x01, 0x7d, 0xbb, 0x53, 0x66,
0x62, 0x2b, 0x39, 0x5e, 0x80, 0x89, 0xb2, 0xdc, 0xeb, 0xce, 0xba, 0x8b, 0xee, 0xdc, 0xa4, 0xac,
0x42, 0x8d, 0x6c, 0xc8, 0x1f, 0x0e, 0x74, 0x1f, 0xe3, 0x37, 0x7e, 0xc8, 0xe7, 0x5f, 0x64, 0x7d,
0x0a, 0x1d, 0x4a, 0x36, 0xe5, 0xf4, 0x78, 0x93, 0x59, 0xc7, 0x3f, 0xc4, 0x2e, 0xbd, 0xa0, 0x96,
0xde, 0x19, 0xf4, 0x8c, 0x29, 0x7b, 0xc8, 0x9e, 0xab, 0xf0, 0x1c, 0xfa, 0x77, 0x3c, 0xe5, 0x74,
0xc8, 0x76, 0x38, 0x84, 0x41, 0xf5, 0x81, 0x19, 0xb1, 0xf8, 0x74, 0xc0, 0x57, 0x87, 0x4b, 0xbc,
0x01, 0x5f, 0xc7, 0x84, 0x23, 0x9b, 0x47, 0xfd, 0x47, 0x4d, 0xc6, 0x4d, 0xd2, 0x74, 0x87, 0xff,
0xf0, 0x0a, 0x3c, 0x65, 0x09, 0xd1, 0xea, 0xb5, 0xd0, 0x26, 0xa3, 0x06, 0xb7, 0x6b, 0xb9, 0x85,
0xc0, 0x98, 0xc0, 0x6a, 0x68, 0xc3, 0xf4, 0xe4, 0x78, 0x8f, 0xad, 0x1a, 0x9f, 0x02, 0xfd, 0x0e,
0xae, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0x5f, 0x8c, 0x67, 0xbb, 0x1c, 0x03, 0x00, 0x00,
}

View File

@@ -1,5 +1,5 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: github.com/micro/services/blog/posts/proto/posts/posts.proto
// source: proto/posts.proto
package posts