tsk-34: Update last login of user (#37)
Closes #34 Reviewed-on: phoenix/textsender-auth#37 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||
"github.com/google/uuid"
|
||||
@@ -101,3 +102,24 @@ func (m *MockServiceUserStore) GetWithId(ctx context.Context, id uuid.UUID) (*us
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockServiceUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return 0, m.Error
|
||||
}
|
||||
|
||||
serviceUser, exists := m.ServiceUsers[id]
|
||||
if !exists {
|
||||
return 0, errors.New("Service user not found")
|
||||
}
|
||||
|
||||
serviceUser.LastLogin = &lastLogin
|
||||
|
||||
m.ServiceUsers[id] = serviceUser
|
||||
m.ServiceUsersByUsername[serviceUser.Username] = serviceUser
|
||||
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
@@ -136,3 +136,24 @@ func (m *MockUserStore) UpdatePassword(ctx context.Context, id uuid.UUID, passwo
|
||||
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return 0, m.Error
|
||||
}
|
||||
|
||||
user, exists := m.Users[id]
|
||||
if !exists {
|
||||
return 0, errors.New("User not found")
|
||||
}
|
||||
|
||||
user.LastLogin = &lastLogin
|
||||
|
||||
m.Users[id] = user
|
||||
m.UsersByUsername[user.Username] = user
|
||||
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/user"
|
||||
"github.com/google/uuid"
|
||||
@@ -10,11 +11,13 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// TODO: Rename this to ServiceUserStore
|
||||
type ServiceStore interface {
|
||||
CheckWithUsername(ctx context.Context, username string) (bool, error)
|
||||
GetWithUsername(ctx context.Context, username string) (*user.ServiceUser, error)
|
||||
GetWithId(ctx context.Context, id uuid.UUID) (*user.ServiceUser, error)
|
||||
Create(ctx context.Context, serviceUser *user.ServiceUser) error
|
||||
UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error)
|
||||
}
|
||||
|
||||
type PGServiceStore struct {
|
||||
@@ -81,3 +84,12 @@ func (s *PGServiceStore) Create(ctx context.Context, serviceUser *user.ServiceUs
|
||||
&serviceUser.Id, &serviceUser.Created,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *PGServiceStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
|
||||
query := `UPDATE service_users SET last_login = $1 WHERE id = $2`
|
||||
if affected, err := s.db.Exec(ctx, query, lastLogin, id); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -18,6 +19,7 @@ type UserStore interface {
|
||||
GetAllUsers(ctx context.Context) ([]*user.User, error)
|
||||
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)
|
||||
}
|
||||
|
||||
type PGUserStore struct {
|
||||
@@ -123,3 +125,12 @@ func (s *PGUserStore) UpdatePassword(ctx context.Context, id uuid.UUID, password
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PGUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
|
||||
query := `UPDATE users SET last_login = $1 WHERE id = $2`
|
||||
if affected, err := s.db.Exec(ctx, query, lastLogin, id); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user