| 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 |
| 9 "infra/gae/libs/gae" | 10 "infra/gae/libs/gae" |
| 11 "infra/gae/libs/gae/helper" |
| 10 "infra/gae/libs/gae/prod" | 12 "infra/gae/libs/gae/prod" |
| 11 | 13 |
| 12 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | 14 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" |
| 13 ) | 15 ) |
| 14 | 16 |
| 15 // AddReq describes the input parameters to the 'Add' RPC. Name is required, | 17 // AddReq describes the input parameters to the 'Add' RPC. Name is required, |
| 16 // which makes it show up in the REST path, and Delta will be encoded in the | 18 // which makes it show up in the REST path, and Delta will be encoded in the |
| 17 // request body as JSON. | 19 // request body as JSON. |
| 18 type AddReq struct { | 20 type AddReq struct { |
| 19 Name string `endpoints:"required"` | 21 Name string `endpoints:"required"` |
| (...skipping 10 matching lines...) Expand all Loading... |
| 30 | 32 |
| 31 // Add adds a value to the current counter, and returns the old+new values. It | 33 // Add adds a value to the current counter, and returns the old+new values. It |
| 32 // may cause a counter to come into existance. | 34 // may cause a counter to come into existance. |
| 33 func (Example) Add(c context.Context, r *AddReq) (rsp *AddRsp, err error) { | 35 func (Example) Add(c context.Context, r *AddReq) (rsp *AddRsp, err error) { |
| 34 rsp = &AddRsp{} | 36 rsp = &AddRsp{} |
| 35 | 37 |
| 36 c = prod.Use(c) | 38 c = prod.Use(c) |
| 37 err = gae.GetRDS(c).RunInTransaction(func(c context.Context) error { | 39 err = gae.GetRDS(c).RunInTransaction(func(c context.Context) error { |
| 38 rds := gae.GetRDS(c) | 40 rds := gae.GetRDS(c) |
| 39 ctr := &Counter{} | 41 ctr := &Counter{} |
| 42 pls := helper.GetPLS(ctr) |
| 40 key := rds.NewKey("Counter", r.Name, 0, nil) | 43 key := rds.NewKey("Counter", r.Name, 0, nil) |
| 41 » » if err := rds.Get(key, ctr); err != nil && err != gae.ErrDSNoSuc
hEntity { | 44 » » if err := rds.Get(key, pls); err != nil && err != gae.ErrDSNoSuc
hEntity { |
| 42 return err | 45 return err |
| 43 } | 46 } |
| 44 rsp.Prev = ctr.Val | 47 rsp.Prev = ctr.Val |
| 45 ctr.Val += r.Delta | 48 ctr.Val += r.Delta |
| 46 rsp.Cur = ctr.Val | 49 rsp.Cur = ctr.Val |
| 47 » » _, err := rds.Put(key, ctr) | 50 » » _, err := rds.Put(key, pls) |
| 48 return err | 51 return err |
| 49 }, nil) | 52 }, nil) |
| 50 return | 53 return |
| 51 } | 54 } |
| 52 | 55 |
| 53 func init() { | 56 func init() { |
| 54 mi["Add"] = &endpoints.MethodInfo{ | 57 mi["Add"] = &endpoints.MethodInfo{ |
| 55 Path: "counter/{Name}", | 58 Path: "counter/{Name}", |
| 56 Desc: "Add an an amount to a particular counter", | 59 Desc: "Add an an amount to a particular counter", |
| 57 } | 60 } |
| 58 } | 61 } |
| OLD | NEW |