datastore

package controllers

import (
    "app-engine-test/app/models"
    "strconv"

    "github.com/gin-gonic/gin"
    "google.golang.org/appengine"
    "google.golang.org/appengine/datastore"
)

// Datastore ...
type Datastore struct{}

// Create ...
func (Datastore) Create(c *gin.Context) {
    ctx := appengine.NewContext(c.Request)
    var user models.User
    user.Name = "yyy"
    c.Bind(&user)
    key := datastore.NewIncompleteKey(ctx, "User", nil)
    _, err := datastore.Put(ctx, key, &user)
    if err != nil {
        c.JSON(500, err)
    } else {
        c.JSON(200, user)
    }
}

// GetsAll ...
func (Datastore) GetsAll(c *gin.Context) {
    ctx := appengine.NewContext(c.Request)
    q := datastore.NewQuery("User").Limit(10)
    users := make([]models.User, 0, 10)
    q.GetAll(ctx, &users)
    s := strconv.Itoa(len(users))
    c.String(200, s)
}

// Find ...
func (Datastore) Find(c *gin.Context) {
    ctx := appengine.NewContext(c.Request)
    users := make([]models.User, 0, 10)
    q := datastore.NewQuery("User").Filter("Name =", "yyy")
    q.GetAll(ctx, &users)
    s := strconv.Itoa(len(users))
    c.String(200, s)
}