From de1276a29e9bc6cf427a326d1bf41be22b195e7d Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 29 Dec 2015 19:57:27 +0100 Subject: GetUserByID implementiert. diff --git a/charakterin.go b/charakterin.go index 142484c..253e627 100644 --- a/charakterin.go +++ b/charakterin.go @@ -13,9 +13,9 @@ import ( ) const ( - NoSuchUser = "pq: no_such_user" - InvalidPassword = "pq: invalid_password" - UsernameTaken = "pq: username_taken" + NoSuchUser = "pq: no_such_user" + InvalidPassword = "pq: invalid_password" + UsernameTaken = "pq: username_taken" EmailAlreadyRegistered = "pq: email_already_registered" ) @@ -278,27 +278,22 @@ func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { } var email, name string - var displayName *string + var displayName sql.NullString var password []byte var id int - var lastActivity time.Time + var lastActivity *time.Time var isActive bool err = c.Database.QueryRow(`SELECT id, email, name, password, display_name, last_activity, is_active FROM login.get_user_by_session($1)`, cookie.Value).Scan(&id, &email, &name, &password, &displayName, &lastActivity, &isActive) if err != nil { return nil, err } - dspName := "" - if displayName != nil { - dspName = *displayName - } - user := &User{ id, name, email, password, - dspName, + displayName, lastActivity, isActive, } @@ -306,6 +301,16 @@ func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { return user, nil } +// GetUserByID gibt den User mit der gegebenen ID zurück +func (c *Charakterin) GetUserByID(id int) (*User, error) { + user := &User{} + err := c.Database.QueryRow(`SELECT id, email, name, password, display_name, last_activity, is_active FROM login.users WHERE id = $1`, id).Scan(&user.ID, &user.EMail, &user.Name, &user.Password, &user.DisplayName, &user.LastActivity, &user.IsActive) + if err != nil { + return nil, err + } + return user, nil +} + // ConfirmEmail konfirmiert die email addresse mit der gegebenen confirm id. func (c *Charakterin) ConfirmEmail(confirmId string) error { var result string diff --git a/user.go b/user.go index 909859e..dc32b3e 100644 --- a/user.go +++ b/user.go @@ -1,6 +1,7 @@ package charakterin import ( + "database/sql" "time" ) @@ -10,15 +11,15 @@ type User struct { Name string EMail string Password []byte - DisplayName string - LastActivity time.Time + DisplayName sql.NullString + LastActivity *time.Time IsActive bool } // GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück. func (u *User) GetName() string { - if len(u.DisplayName) > 0 { - return u.DisplayName + if u.DisplayName.Valid { + return u.DisplayName.String } return u.Name } -- cgit v0.10.1