OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package example | 5 package example |
6 | 6 |
7 import ( | 7 import ( |
8 "golang.org/x/net/context" | 8 "golang.org/x/net/context" |
9 » "infra/gae/libs/wrapper" | 9 » "infra/gae/libs/gae" |
10 » "infra/gae/libs/wrapper/gae" | 10 » "infra/gae/libs/gae/prod" |
11 » "infra/gae/libs/wrapper/gae/commonErrors" | |
12 | 11 |
13 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | 12 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" |
14 ) | 13 ) |
15 | 14 |
16 // AddReq describes the input parameters to the 'Add' RPC. Name is required, | 15 // AddReq describes the input parameters to the 'Add' RPC. Name is required, |
17 // which makes it show up in the REST path, and Delta will be encoded in the | 16 // which makes it show up in the REST path, and Delta will be encoded in the |
18 // request body as JSON. | 17 // request body as JSON. |
19 type AddReq struct { | 18 type AddReq struct { |
20 Name string `endpoints:"required"` | 19 Name string `endpoints:"required"` |
21 | 20 |
22 Delta int64 `json:",string"` | 21 Delta int64 `json:",string"` |
23 } | 22 } |
24 | 23 |
25 // AddRsp describes the return value from the 'Add' RPC. Prev is the previous | 24 // AddRsp describes the return value from the 'Add' RPC. Prev is the previous |
26 // value, and Cur is the post-increment value. | 25 // value, and Cur is the post-increment value. |
27 type AddRsp struct { | 26 type AddRsp struct { |
28 Prev int64 `json:",string"` | 27 Prev int64 `json:",string"` |
29 Cur int64 `json:",string"` | 28 Cur int64 `json:",string"` |
30 } | 29 } |
31 | 30 |
32 // Add adds a value to the current counter, and returns the old+new values. It | 31 // Add adds a value to the current counter, and returns the old+new values. It |
33 // may cause a counter to come into existance. | 32 // may cause a counter to come into existance. |
34 func (Example) Add(c endpoints.Context, r *AddReq) (rsp *AddRsp, err error) { | 33 func (Example) Add(c context.Context, r *AddReq) (rsp *AddRsp, err error) { |
35 rsp = &AddRsp{} | 34 rsp = &AddRsp{} |
36 | 35 |
37 » ds := wrapper.GetDS(gae.Use(context.Background(), c)) | 36 » c = prod.Use(c) |
38 » err = ds.RunInTransaction(func(context.Context) error { | 37 » err = gae.GetRDS(c).RunInTransaction(func(c context.Context) error { |
39 » » ctr := &Counter{ID: r.Name} | 38 » » rds := gae.GetRDS(c) |
40 » » if err := ds.Get(ctr); err != nil && err != commonErrors.ErrNoSu
chEntityDS { | 39 » » ctr := &Counter{} |
| 40 » » key := rds.NewKey("Counter", r.Name, 0, nil) |
| 41 » » if err := rds.Get(key, ctr); err != nil && err != gae.ErrDSNoSuc
hEntity { |
41 return err | 42 return err |
42 } | 43 } |
43 rsp.Prev = ctr.Val | 44 rsp.Prev = ctr.Val |
44 ctr.Val += r.Delta | 45 ctr.Val += r.Delta |
45 rsp.Cur = ctr.Val | 46 rsp.Cur = ctr.Val |
46 » » _, err := ds.Put(ctr) | 47 » » _, err := rds.Put(key, ctr) |
47 return err | 48 return err |
48 }, nil) | 49 }, nil) |
49 return | 50 return |
50 } | 51 } |
51 | 52 |
52 func init() { | 53 func init() { |
53 mi["Add"] = &endpoints.MethodInfo{ | 54 mi["Add"] = &endpoints.MethodInfo{ |
54 Path: "counter/{Name}", | 55 Path: "counter/{Name}", |
55 Desc: "Add an an amount to a particular counter", | 56 Desc: "Add an an amount to a particular counter", |
56 } | 57 } |
57 } | 58 } |
OLD | NEW |