mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
add vote handler to app
This commit is contained in:
44
app/handler/vote.go
Normal file
44
app/handler/vote.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
|
||||
"github.com/micro/services/pkg/tenant"
|
||||
"github.com/micro/micro/v3/service/store"
|
||||
pb "github.com/micro/services/app/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
voteKey = "votes/"
|
||||
)
|
||||
|
||||
type Vote struct {
|
||||
Id string `json:"id"`
|
||||
Message string `json:"message"`
|
||||
VotedAt time.Time `json:"voted_at"`
|
||||
}
|
||||
|
||||
func (n *App) Vote(ctx context.Context, req *pb.VoteRequest, rsp *pb.VoteResponse) error {
|
||||
mtx.Lock()
|
||||
defer mtx.Unlock()
|
||||
|
||||
id, ok := tenant.FromContext(ctx)
|
||||
if !ok {
|
||||
id = "micro"
|
||||
}
|
||||
|
||||
rec := store.NewRecord(voteKey + id, &Vote{
|
||||
Id: id,
|
||||
Message: req.Message,
|
||||
VotedAt: time.Now(),
|
||||
})
|
||||
|
||||
// we don't need to check the error
|
||||
store.Write(rec)
|
||||
|
||||
rsp.Message = "Thanks for the vote!"
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user