From d0b722cc8b029e3224af8b8402f19155656f5071 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 13 Jan 2015 21:57:02 -0800 Subject: [PATCH] Insert and Update Users instead of generic Save function. Check ID != 0 --- server/datastore/database/user.go | 11 +++-------- server/handler/login.go | 7 +++++++ shared/model/request.go | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/server/datastore/database/user.go b/server/datastore/database/user.go index 25de08026..f6b25040d 100644 --- a/server/datastore/database/user.go +++ b/server/datastore/database/user.go @@ -49,20 +49,15 @@ func (db *Userstore) GetUserList() ([]*model.User, error) { // PostUser saves a User in the datastore. func (db *Userstore) PostUser(user *model.User) error { - if user.Created == 0 { - user.Created = time.Now().UTC().Unix() - } + user.Created = time.Now().UTC().Unix() user.Updated = time.Now().UTC().Unix() - return meddler.Save(db, userTable, user) + return meddler.Insert(db, userTable, user) } // PutUser saves a user in the datastore. func (db *Userstore) PutUser(user *model.User) error { - if user.Created == 0 { - user.Created = time.Now().UTC().Unix() - } user.Updated = time.Now().UTC().Unix() - return meddler.Save(db, userTable, user) + return meddler.Update(db, userTable, user) } // DelUser removes the user from the datastore. diff --git a/server/handler/login.go b/server/handler/login.go index 285702d09..2f08debed 100644 --- a/server/handler/login.go +++ b/server/handler/login.go @@ -70,6 +70,13 @@ func GetLogin(c web.C, w http.ResponseWriter, r *http.Request) { return } + // the user id should NEVER equal zero + if u.ID == 0 { + log.Println("Unable to create account. User ID is zero") + w.WriteHeader(http.StatusInternalServerError) + return + } + // if this is the first user, they // should be an admin. if u.ID == 1 { diff --git a/shared/model/request.go b/shared/model/request.go index 594cbeb2f..94075a88c 100644 --- a/shared/model/request.go +++ b/shared/model/request.go @@ -1,5 +1,9 @@ package model +import ( + "fmt" +) + type Request struct { Host string `json:"-"` User *User `json:"-"` @@ -7,3 +11,16 @@ type Request struct { Commit *Commit `json:"commit"` Prior *Commit `json:"prior_commit"` } + +// URL returns the link to the commit in +// string format. +func (r *Request) URL() string { + return fmt.Sprintf("%s/%s/%s/%s", + r.Host, + r.Repo.Host, + r.Repo.Owner, + r.Repo.Name, + r.Commit.Branch, + r.Commit.Sha, + ) +}