Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: appengine/apigen_examples/dumb_counter/dumbCounter/service_add.go

Issue 1750143003: Remove ephelper and other endpoints code. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698