| 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 dumbCounter | |
| 6 | |
| 7 import ( | |
| 8 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | |
| 9 dstore "github.com/luci/gae/service/datastore" | |
| 10 "golang.org/x/net/context" | |
| 11 ) | |
| 12 | |
| 13 // AddReq describes the input parameters to the 'Add' RPC. Name is required, | |
| 14 // which makes it show up in the REST path, and Delta will be encoded in the | |
| 15 // request body as JSON. | |
| 16 type AddReq struct { | |
| 17 Name string `endpoints:"required"` | |
| 18 | |
| 19 Delta int64 `json:",string"` | |
| 20 } | |
| 21 | |
| 22 // AddRsp describes the return value from the 'Add' RPC. Prev is the previous | |
| 23 // value, and Cur is the post-increment value. | |
| 24 type AddRsp struct { | |
| 25 Prev int64 `json:",string"` | |
| 26 Cur int64 `json:",string"` | |
| 27 } | |
| 28 | |
| 29 // Add adds a value to the current counter, and returns the old+new values. It | |
| 30 // may cause a counter to come into existance. | |
| 31 func (e *Example) Add(c context.Context, r *AddReq) (rsp *AddRsp, err error) { | |
| 32 c, err = e.Use(c, addMethodInfo) | |
| 33 if err != nil { | |
| 34 return | |
| 35 } | |
| 36 | |
| 37 rsp = &AddRsp{} | |
| 38 | |
| 39 err = dstore.Get(c).RunInTransaction(func(c context.Context) error { | |
| 40 ds := dstore.Get(c) | |
| 41 ctr := &Counter{Name: r.Name} | |
| 42 if err := ds.Get(ctr); err != nil && err != dstore.ErrNoSuchEnti
ty { | |
| 43 return err | |
| 44 } | |
| 45 rsp.Prev = ctr.Val | |
| 46 ctr.Val += r.Delta | |
| 47 rsp.Cur = ctr.Val | |
| 48 return ds.Put(ctr) | |
| 49 }, nil) | |
| 50 return | |
| 51 } | |
| 52 | |
| 53 var addMethodInfo = &endpoints.MethodInfo{ | |
| 54 Path: "counter/{Name}", | |
| 55 Desc: "Add an an amount to a particular counter", | |
| 56 } | |
| OLD | NEW |