tsk-18: Create service user (#22)
Closes #18 Reviewed-on: phoenix/textsender-auth#22 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
||||
)
|
||||
|
||||
type MockServiceUserStore struct {
|
||||
ServiceUsers map[uuid.UUID]*user.ServiceUser
|
||||
ServiceUsersByUsername map[string]*user.ServiceUser
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockServiceUserStore() *MockServiceUserStore {
|
||||
return &MockServiceUserStore{
|
||||
ServiceUsers: make(map[uuid.UUID]*user.ServiceUser),
|
||||
ServiceUsersByUsername: make(map[string]*user.ServiceUser),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockServiceUserStore) Create(ctx context.Context, user *user.ServiceUser) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if user.Id == uuid.Nil {
|
||||
user.Id = uuid.New()
|
||||
}
|
||||
|
||||
if _, exists := m.ServiceUsersByUsername[user.Username]; exists {
|
||||
return errors.New("service User with username already exists")
|
||||
}
|
||||
|
||||
m.ServiceUsers[user.Id] = user
|
||||
m.ServiceUsersByUsername[user.Username] = user
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockServiceUserStore) CheckWithUsername(ctx context.Context, username string) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
var exists bool
|
||||
|
||||
for _, serviceUser := range m.ServiceUsers {
|
||||
if serviceUser.Username == username {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return exists, nil
|
||||
} else {
|
||||
return exists, nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
||||
)
|
||||
|
||||
type MockUserStore struct {
|
||||
Users map[uuid.UUID]*user.User
|
||||
UsersByUsername map[string]*user.User
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockUserStore() *MockUserStore {
|
||||
return &MockUserStore{
|
||||
Users: make(map[uuid.UUID]*user.User),
|
||||
UsersByUsername: make(map[string]*user.User),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockUserStore) CreateUser(ctx context.Context, user *user.User) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if user.Id == uuid.Nil {
|
||||
user.Id = uuid.New()
|
||||
}
|
||||
|
||||
if _, exists := m.UsersByUsername[user.Username]; exists {
|
||||
return errors.New("User with email already exists")
|
||||
}
|
||||
|
||||
m.Users[user.Id] = user
|
||||
m.UsersByUsername[user.Username] = user
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) GetUserByID(ctx context.Context, id uuid.UUID) (*user.User, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
user, exists := m.Users[id]
|
||||
if !exists {
|
||||
return nil, errors.New("User not found")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) GetUserByUsername(ctx context.Context, username string) (*user.User, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
user, exists := m.UsersByUsername[username]
|
||||
if !exists {
|
||||
return nil, errors.New("User not found")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) GetAllUsers(ctx context.Context) ([]*user.User, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
users := make([]*user.User, 0, len(m.Users))
|
||||
for _, user := range m.Users {
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) UserExists(ctx context.Context, username string) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
_, exists := m.UsersByUsername[username]
|
||||
if !exists {
|
||||
return exists, nil
|
||||
} else {
|
||||
return exists, nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/user"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type ServiceStore interface {
|
||||
CheckWithUsername(ctx context.Context, username string) (bool, error)
|
||||
Create(ctx context.Context, serviceUser *user.ServiceUser) error
|
||||
}
|
||||
|
||||
type PGServiceStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewServiceStore(db *pgxpool.Pool) *PGServiceStore {
|
||||
return &PGServiceStore{db: db}
|
||||
}
|
||||
|
||||
func (s *PGServiceStore) CheckWithUsername(ctx context.Context, username string) (bool, error) {
|
||||
var exists bool
|
||||
query := `SELECT EXISTS(SELECT 1 FROM service_users WHERE Username = $1)`
|
||||
|
||||
if err := s.db.QueryRow(ctx, query, username).Scan(&exists); err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return exists, nil
|
||||
} else {
|
||||
return exists, fmt.Errorf("Error querying row: %v", err)
|
||||
}
|
||||
} else {
|
||||
return exists, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PGServiceStore) Create(ctx context.Context, serviceUser *user.ServiceUser) error {
|
||||
query := `
|
||||
INSERT INTO service_users (username, passphrase)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, created
|
||||
`
|
||||
|
||||
return s.db.QueryRow(ctx, query, serviceUser.Username, serviceUser.Passphrase).Scan(
|
||||
&serviceUser.Id, &serviceUser.Created,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user