forked from GoesToEleven/GolangTraining
/
tweets.go
32 lines (26 loc) · 788 Bytes
/
tweets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"net/http"
)
func putTweet(req *http.Request, user *User, tweet *Tweet) error {
ctx := appengine.NewContext(req)
userKey := datastore.NewKey(ctx, "Users", user.UserName, 0, nil)
key := datastore.NewIncompleteKey(ctx, "Tweets", userKey)
_, err := datastore.Put(ctx, key, tweet)
return err
}
func getTweets(req *http.Request, user *User) ([]Tweet, error) {
ctx := appengine.NewContext(req)
var tweets []Tweet
q := datastore.NewQuery("Tweets")
if user != nil {
// show tweets of a specific user
userKey := datastore.NewKey(ctx, "Users", user.UserName, 0, nil)
q = q.Ancestor(userKey)
}
q = q.Order("-Time").Limit(20)
_, err := q.GetAll(ctx, &tweets)
return tweets, err
}