Comments service (#21)

This commit is contained in:
Janos Dobronszki
2020-10-30 13:49:03 +01:00
committed by GitHub
parent ab521639d4
commit 3642f7279d
8 changed files with 316 additions and 85 deletions

View File

@@ -2,15 +2,45 @@ package handler
import (
"context"
"time"
"github.com/micro/micro/v3/service/logger"
"github.com/google/uuid"
"github.com/micro/dev/model"
"github.com/micro/micro/v3/service/store"
pb "github.com/micro/services/blog/comments/proto"
)
type Comments struct{}
// Call is a single request handler called via client.Call or the generated client code
func (c *Comments) Save(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
logger.Info("Not yet implemented")
return nil
type Comments struct {
comments model.Table
idIndex model.Index
postIndex model.Index
}
func NewComments() *Comments {
postIndex := model.ByEquality("post")
postIndex.Order.Type = model.OrderTypeDesc
postIndex.Order.FieldName = "created"
idIndex := model.ByEquality("id")
idIndex.Order.Type = model.OrderTypeUnordered
return &Comments{
comments: model.NewTable(store.DefaultStore, "users", model.Indexes(postIndex), nil),
postIndex: postIndex,
idIndex: idIndex,
}
}
func (c *Comments) New(ctx context.Context, req *pb.NewRequest, rsp *pb.NewResponse) error {
return c.comments.Save(pb.Comment{
Id: uuid.New().String(),
Post: req.Post,
Author: req.Author,
Message: req.Message,
Created: time.Now().Unix(),
})
}
func (c *Comments) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
return c.comments.List(c.postIndex.ToQuery(req.Post), &rsp.Comments)
}