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
}// }}}
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
}// }}}

6
pkg.go
View File

@ -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)