tsk-35: Update name of user (#38)
Closes #35 Reviewed-on: phoenix/textsender-auth#38 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -3,6 +3,7 @@ package mock
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -157,3 +158,35 @@ func (m *MockUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastL
|
||||
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) UpdateName(ctx context.Context, firstname *string, lastname *string, usr *user.User) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return 0, m.Error
|
||||
}
|
||||
|
||||
if firstname == nil && lastname == nil {
|
||||
return 0, fmt.Errorf("Names not provided")
|
||||
}
|
||||
|
||||
user, exists := m.Users[usr.Id]
|
||||
if !exists {
|
||||
return 0, errors.New("User not found")
|
||||
}
|
||||
|
||||
if firstname != nil && lastname != nil {
|
||||
user.Firstname = firstname
|
||||
user.Lastname = lastname
|
||||
} else if firstname != nil {
|
||||
user.Firstname = firstname
|
||||
} else {
|
||||
user.Lastname = lastname
|
||||
}
|
||||
|
||||
m.Users[usr.Id] = user
|
||||
m.UsersByUsername[user.Username] = user
|
||||
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ type UserStore interface {
|
||||
UserExists(ctx context.Context, username string) (bool, error)
|
||||
UpdatePassword(ctx context.Context, id uuid.UUID, password string) (int64, error)
|
||||
UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error)
|
||||
UpdateName(ctx context.Context, firstname *string, lastname *string, usr *user.User) (int64, error)
|
||||
}
|
||||
|
||||
type PGUserStore struct {
|
||||
@@ -134,3 +135,39 @@ func (s *PGUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLog
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PGUserStore) UpdateName(ctx context.Context, firstname *string, lastname *string, usr *user.User) (int64, error) {
|
||||
var query string
|
||||
|
||||
if firstname == nil && lastname == nil {
|
||||
return 0, fmt.Errorf("Provided names are empty")
|
||||
} else {
|
||||
tableName := "users"
|
||||
if firstname != nil && lastname != nil {
|
||||
query = fmt.Sprintf("UPDATE %s SET first_name = $1, last_name = $2 WHERE id = $3", tableName)
|
||||
if affected, err := s.db.Exec(ctx, query, firstname, lastname, usr.Id); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
usr.Firstname = firstname
|
||||
usr.Lastname = lastname
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
} else if firstname != nil {
|
||||
query = fmt.Sprintf("UPDATE %s SET first_name = $1 WHERE id = $2", tableName)
|
||||
if affected, err := s.db.Exec(ctx, query, firstname, usr.Id); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
usr.Firstname = firstname
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
} else {
|
||||
query = fmt.Sprintf("UPDATE %s SET last_name = $1 WHERE id = $2", tableName)
|
||||
if affected, err := s.db.Exec(ctx, query, lastname, usr.Id); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
usr.Lastname = lastname
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user