mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-17 21:34:56 +00:00
Comments service (#21)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user