Ai
1 Star 1 Fork 0

湖底观景/GolangTraining

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
users.go 1.39 KB
Copy Edit Raw Blame History
GoesToEleven authored 2016-04-21 08:45 +08:00 . changes dir structure
package API
import (
"fmt"
"github.com/GoesToEleven/GolangTraining/56_twitter/19_abstract-API-Model_AE-fix/Model"
"github.com/julienschmidt/httprouter"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"io/ioutil"
"net/http"
)
func CheckUserName(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
bs, err := ioutil.ReadAll(req.Body)
sbs := string(bs)
log.Infof(ctx, "REQUEST BODY: %v", sbs)
var user Model.User
key := datastore.NewKey(ctx, "Users", sbs, 0, nil)
err = datastore.Get(ctx, key, &user)
// if there is an err, there is NO user
log.Infof(ctx, "ERR: %v", err)
if err != nil {
// there is an err, there is a NO user
fmt.Fprint(res, "false")
return
} else {
fmt.Fprint(res, "true")
}
}
func CreateUser(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
NewUser := Model.User{
Email: req.FormValue("email"),
UserName: req.FormValue("userName"),
Password: req.FormValue("password"),
}
key := datastore.NewKey(ctx, "Users", NewUser.UserName, 0, nil)
key, err := datastore.Put(ctx, key, &NewUser)
// this is the only error checking I added; any others on this page needed?
if err != nil {
log.Errorf(ctx, "error adding todo: %v", err)
http.Error(res, err.Error(), 500)
return
}
http.Redirect(res, req, "/", 302)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjianGood/GolangTraining.git
git@gitee.com:zhangjianGood/GolangTraining.git
zhangjianGood
GolangTraining
GolangTraining
afa19f5c43f3

Search