From c848ae60b5c3443150174017f805b668388cc9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Tue, 13 Feb 2024 13:51:01 +0100 Subject: [PATCH] Return ID when creating a user --- database/pkg.go | 8 ++++++-- pkg.go | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/database/pkg.go b/database/pkg.go index 50695a5..6cab492 100644 --- a/database/pkg.go +++ b/database/pkg.go @@ -236,8 +236,9 @@ func (db *T) UpdateUserTime(userID int) (err error) {// {{{ return }// }}} -func (db *T) CreateUser(username, password, name string) (err error) {// {{{ - _, err = db.Conn.Exec(` +func (db *T) CreateUser(username, password, name string) (userID int64, err error) {// {{{ + var row *sql.Row + row = db.Conn.QueryRow(` INSERT INTO _webservice.user(username, password, name) VALUES( $1, @@ -250,11 +251,14 @@ func (db *T) CreateUser(username, password, name string) (err error) {// {{{ ), $3 ) + RETURNING id `, username, password, name, ) + + err = row.Scan(&userID) return }// }}} diff --git a/pkg.go b/pkg.go index a8efe5a..3ea745c 100644 --- a/pkg.go +++ b/pkg.go @@ -223,7 +223,7 @@ func (service *Service) InitDatabaseConnection() (err error) { // {{{ } 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 { err = service.InitDatabaseConnection() 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 } // }}} func (service *Service) CreateUserPrompt() { // {{{ @@ -251,7 +251,7 @@ func (service *Service) CreateUserPrompt() { // {{{ password, _ = reader.ReadString('\n') password = strings.TrimSpace(password) - err = service.CreateUser(username, password, name) + _, err = service.CreateUser(username, password, name) if err != nil { service.logger.Error("application", "error", err) os.Exit(1)