OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package example |
| 6 |
| 7 import ( |
| 8 "golang.org/x/net/context" |
| 9 "infra/gae/libs/wrapper" |
| 10 "infra/gae/libs/wrapper/gae" |
| 11 "infra/gae/libs/wrapper/gae/commonErrors" |
| 12 |
| 13 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" |
| 14 ) |
| 15 |
| 16 // 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 |
| 18 // request body as JSON. |
| 19 type AddReq struct { |
| 20 Name string `endpoints:"required"` |
| 21 |
| 22 Delta int64 `json:",string"` |
| 23 } |
| 24 |
| 25 // AddRsp describes the return value from the 'Add' RPC. Prev is the previous |
| 26 // value, and Cur is the post-increment value. |
| 27 type AddRsp struct { |
| 28 Prev int64 `json:",string"` |
| 29 Cur int64 `json:",string"` |
| 30 } |
| 31 |
| 32 // Add adds a value to the current counter, and returns the old+new values. It |
| 33 // may cause a counter to come into existance. |
| 34 func (Example) Add(c endpoints.Context, r *AddReq) (rsp *AddRsp, err error) { |
| 35 rsp = &AddRsp{} |
| 36 |
| 37 ds := wrapper.GetDS(gae.Use(context.Background(), c)) |
| 38 err = ds.RunInTransaction(func(context.Context) error { |
| 39 ctr := &Counter{ID: r.Name} |
| 40 if err := ds.Get(ctr); err != nil && err != commonErrors.ErrNoSu
chEntityDS { |
| 41 return err |
| 42 } |
| 43 rsp.Prev = ctr.Val |
| 44 ctr.Val += r.Delta |
| 45 rsp.Cur = ctr.Val |
| 46 _, err := ds.Put(ctr) |
| 47 return err |
| 48 }, nil) |
| 49 return |
| 50 } |
| 51 |
| 52 func init() { |
| 53 mi["Add"] = &endpoints.MethodInfo{ |
| 54 Path: "counter/{Name}", |
| 55 Desc: "Add an an amount to a particular counter", |
| 56 } |
| 57 } |
OLD | NEW |