Skip to content

Commit

Permalink
Add bulk update todos method
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Beslic <abeslic@abronan.com>
  • Loading branch information
abronan committed Mar 31, 2018
1 parent 0873c40 commit b382e03
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 34 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ curl -X POST -H "Content-Type: application/json" -d '{"items": [{"title":"Todo_1
{"ids":["e8924469-8847-4840-ae16-21be734173f4","0db11e34-4707-4a5d-92fe-f4952213d940"]}
```

- Bulk Update Todos:

```bash
curl -X PUT -H "Content-Type: application/json" -d '{"items": [{"id":"e94a6d0b-953b-4dad-aecb-318f183db4c7","title":"Todo_1","description":"Todo_1","completed":"true"},{"id":"d53daa2c-e6af-45ba-b192-3e1dc443b165","title":"Todo_2","description":"Todo_2","completed":"true"}]}' "http://localhost:8080/v1/todo/bulk"
```

## Language/Libraries

- golang
Expand Down
25 changes: 25 additions & 0 deletions api/api.pb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,20 @@ file {
message_type {
name: "UpdateTodoResponse"
}
message_type {
name: "UpdateTodosRequest"
field {
name: "items"
number: 1
label: LABEL_REPEATED
type: TYPE_MESSAGE
type_name: ".todo.v1.Todo"
json_name: "items"
}
}
message_type {
name: "UpdateTodosResponse"
}
service {
name: "TodoService"
method {
Expand Down Expand Up @@ -1600,6 +1614,17 @@ file {
}
}
}
method {
name: "UpdateTodos"
input_type: ".todo.v1.UpdateTodosRequest"
output_type: ".todo.v1.UpdateTodosResponse"
options {
72295728 {
3: "/v1/todo/bulk"
7: "*"
}
}
}
}
options {
go_package: "todo"
Expand Down
131 changes: 97 additions & 34 deletions api/todo/v1/todo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions api/todo/v1/todo.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions api/todo/v1/todo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ service TodoService {
body: "item"
};
}

rpc UpdateTodos(UpdateTodosRequest) returns (UpdateTodosResponse) {
option (google.api.http) ={
put: "/v1/todo/bulk"
body: "*"
};
}
}

message Todo {
Expand Down Expand Up @@ -91,3 +98,9 @@ message UpdateTodoRequest {
}

message UpdateTodoResponse {}

message UpdateTodosRequest {
repeated Todo items = 1;
}

message UpdateTodosResponse {}
16 changes: 16 additions & 0 deletions service/todo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ func (s Service) UpdateTodo(ctx context.Context, req *todo.UpdateTodoRequest) (*
}
return &todo.UpdateTodoResponse{}, nil
}

// UpdateTodos updates todo items given their respective title and description.
func (s Service) UpdateTodos(ctx context.Context, req *todo.UpdateTodosRequest) (*todo.UpdateTodosResponse, error) {
time := types.TimestampNow()
for _, item := range req.Items {
item.UpdatedAt = time
}
res, err := s.DB.Model(&req.Items).Column("title", "description", "completed", "updated_at").Update()
if res.RowsAffected() == 0 {
return nil, grpc.Errorf(codes.NotFound, "Could not update item: not found")
}
if err != nil {
return nil, grpc.Errorf(codes.Internal, "Could not update item from the database: %s", err)
}
return &todo.UpdateTodosResponse{}, nil
}

0 comments on commit b382e03

Please sign in to comment.