-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.go
82 lines (72 loc) · 2.24 KB
/
todo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"fmt"
todopb "github.com/greatliontech/protoc-gen-rtk-query/example/service/gen"
"github.com/rs/zerolog/log"
"github.com/jaevor/go-nanoid"
"go.einride.tech/aip/fieldmask"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
func newTodoService() *todoService {
// ignore error since len is known to be correct
gen, _ := nanoid.Standard(21)
return &todoService{
db: map[string]*todopb.Todo{},
genId: gen,
}
}
type todoService struct {
todopb.UnimplementedTodoServiceServer
db map[string]*todopb.Todo
genId func() string
}
func (s *todoService) ListTodos(ctx context.Context, in *emptypb.Empty) (*todopb.Todos, error) {
log.Info().Msg("list todos")
ret := &todopb.Todos{}
for _, v := range s.db {
ret.Items = append(ret.Items, v)
}
return ret, nil
}
func (s *todoService) GetTodo(ctx context.Context, in *todopb.TodoId) (*todopb.Todo, error) {
log.Info().Str("id", in.Id).Msg("get todo")
item, ok := s.db[in.Id]
if !ok {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("todo with id %s not found", in.Id))
}
return item, nil
}
func (s *todoService) CreateTodo(ctx context.Context, in *todopb.Todo) (*todopb.Todo, error) {
log.Info().Interface("todo", in).Msg("create todo")
if in.Id == "" {
in.Id = s.genId()
}
if _, ok := s.db[in.Id]; ok {
return nil, status.Errorf(codes.AlreadyExists, fmt.Sprintf("todo with id %s already exists", in.Id))
}
s.db[in.Id] = in
return in, nil
}
func (s *todoService) UpdateTodo(ctx context.Context, in *todopb.UpdateTodoRequest) (*todopb.Todo, error) {
log.Info().Interface("req", in).Msg("update todo")
cur, ok := s.db[in.Todo.Id]
if !ok {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("todo with id %s not found", in.Todo.Id))
}
fieldmask.Update(in.UpdateMask, cur, in.Todo)
log.Info().Interface("cur", cur).Msg("updated todo")
s.db[in.Todo.Id] = cur
return cur, nil
}
func (s *todoService) DeleteTodo(ctx context.Context, in *todopb.TodoId) (*todopb.TodoId, error) {
log.Info().Str("id", in.Id).Msg("delete todo")
_, ok := s.db[in.Id]
if !ok {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("todo with id %s not found", in.Id))
}
delete(s.db, in.Id)
return in, nil
}