Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit ba543d9

Browse files
committed
Merge branch 'master' of github.com:gophergala2016/source
2 parents 68f6b51 + d885d6f commit ba543d9

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

core/foundation/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type (
5353
}
5454

5555
Request interface {
56+
Ref() *http.Request
5657
Clone() *http.Request
5758
Body() []byte
5859
Method() string
@@ -204,6 +205,11 @@ func (c *FoundationContext) JSON(code int, obj interface{}) {
204205
/// Request interface
205206
////////////////////////////////////////
206207

208+
// Ref returns the request of the Context.
209+
func (c *FoundationContext) Ref() *http.Request {
210+
return c.opt.Request
211+
}
212+
207213
// Clone returns the cloned request of the Context.
208214
func (c *FoundationContext) Clone() *http.Request {
209215
cp := *c.opt.Request

core/models/appengine/datastore.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package appengine
2+
3+
import (
4+
"github.com/gophergala2016/source/core/foundation"
5+
"golang.org/x/net/context"
6+
7+
"google.golang.org/appengine"
8+
"google.golang.org/appengine/datastore"
9+
)
10+
11+
type (
12+
Datastore struct {
13+
ctx context.Context
14+
}
15+
IDatastore interface {
16+
}
17+
Key struct {
18+
o *datastore.Key
19+
}
20+
)
21+
22+
func NewDatastore(ctx foundation.Context) *Datastore {
23+
return &Datastore{
24+
ctx: appengine.NewContext(ctx.Request().Ref()),
25+
}
26+
}
27+
28+
func (d *Datastore) NewKey(kind, stringID string, intID int64, parent *Key) *Key {
29+
return &Key{datastore.NewKey(d.ctx, kind, stringID, intID, parent.o)}
30+
}
31+
32+
func (d *Datastore) Put(key *Key, src interface{}) (*Key, error) {
33+
k, err := datastore.Put(d.ctx, key.o, src)
34+
if err != nil {
35+
return nil, err
36+
}
37+
return &Key{k}, nil
38+
}
39+
40+
func (d *Datastore) Get(key *Key, dst interface{}) error {
41+
return datastore.Get(d.ctx, key.o, dst)
42+
}

core/models/tag_entity_repository.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package models
22

33
import (
44
"github.com/gophergala2016/source/core/foundation"
5+
"github.com/gophergala2016/source/core/models/appengine"
56
)
67

78
type TagRepository struct {
89
RootRepository
10+
*appengine.Datastore
911
}
1012

1113
func NewTagRepository(ctx foundation.Context) *TagRepository {
1214
return &TagRepository{
1315
RootRepository: NewRootRepository(ctx),
16+
Datastore: appengine.NewDatastore(ctx),
1417
}
1518
}
1619

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package models
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gophergala2016/source/core/foundation/foundationtest"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// TestTagEntityRepository ...
11+
func TestTagEntityRepository(t *testing.T) {
12+
assert := assert.New(t)
13+
14+
ctx := foundationtest.NewContext()
15+
repo := NewTagRepository(ctx)
16+
17+
ent := NewTag("foo", "#000000", 0)
18+
k := repo.NewKey("Tag", "", 0, nil)
19+
key, err := repo.Put(k, &ent)
20+
assert.NoError(err)
21+
_ = key
22+
}

0 commit comments

Comments
 (0)