Return ID when creating a user

This commit is contained in:
Magnus Åhall 2024-02-13 13:51:01 +01:00
parent 15abdd4081
commit c848ae60b5
2 changed files with 9 additions and 5 deletions

View File

@ -236,8 +236,9 @@ func (db *T) UpdateUserTime(userID int) (err error) {// {{{
return return
}// }}} }// }}}
func (db *T) CreateUser(username, password, name string) (err error) {// {{{ func (db *T) CreateUser(username, password, name string) (userID int64, err error) {// {{{
_, err = db.Conn.Exec(` var row *sql.Row
row = db.Conn.QueryRow(`
INSERT INTO _webservice.user(username, password, name) INSERT INTO _webservice.user(username, password, name)
VALUES( VALUES(
$1, $1,
@ -250,11 +251,14 @@ func (db *T) CreateUser(username, password, name string) (err error) {// {{{
), ),
$3 $3
) )
RETURNING id
`, `,
username, username,
password, password,
name, name,
) )
err = row.Scan(&userID)
return return
}// }}} }// }}}

6
pkg.go
View File

@ -223,7 +223,7 @@ func (service *Service) InitDatabaseConnection() (err error) { // {{{
} }
return return
} // }}} } // }}}
func (service *Service) CreateUser(username, password, name string) (err error) { // {{{ func (service *Service) CreateUser(username, password, name string) (userID int64, err error) { // {{{
if service.Db != nil { if service.Db != nil {
err = service.InitDatabaseConnection() err = service.InitDatabaseConnection()
if err != nil { if err != nil {
@ -231,7 +231,7 @@ func (service *Service) CreateUser(username, password, name string) (err error)
} }
} }
err = service.Db.CreateUser(username, password, name) userID, err = service.Db.CreateUser(username, password, name)
return return
} // }}} } // }}}
func (service *Service) CreateUserPrompt() { // {{{ func (service *Service) CreateUserPrompt() { // {{{
@ -251,7 +251,7 @@ func (service *Service) CreateUserPrompt() { // {{{
password, _ = reader.ReadString('\n') password, _ = reader.ReadString('\n')
password = strings.TrimSpace(password) password = strings.TrimSpace(password)
err = service.CreateUser(username, password, name) _, err = service.CreateUser(username, password, name)
if err != nil { if err != nil {
service.logger.Error("application", "error", err) service.logger.Error("application", "error", err)
os.Exit(1) os.Exit(1)