-
This commit is contained in:
129
user/internal/repository/interfaces/interfaces.go
Normal file
129
user/internal/repository/interfaces/interfaces.go
Normal file
@ -0,0 +1,129 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/RyanCopley/skybridge/user/internal/domain"
|
||||
)
|
||||
|
||||
// UserRepository defines the interface for user data operations
|
||||
type UserRepository interface {
|
||||
// Create creates a new user
|
||||
Create(ctx context.Context, user *domain.User) error
|
||||
|
||||
// GetByID retrieves a user by ID
|
||||
GetByID(ctx context.Context, id uuid.UUID) (*domain.User, error)
|
||||
|
||||
// GetByEmail retrieves a user by email
|
||||
GetByEmail(ctx context.Context, email string) (*domain.User, error)
|
||||
|
||||
// Update updates an existing user
|
||||
Update(ctx context.Context, user *domain.User) error
|
||||
|
||||
// Delete deletes a user by ID
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
|
||||
// List retrieves users with filtering and pagination
|
||||
List(ctx context.Context, req *domain.ListUsersRequest) (*domain.ListUsersResponse, error)
|
||||
|
||||
// UpdateLastLogin updates the last login timestamp
|
||||
UpdateLastLogin(ctx context.Context, id uuid.UUID) error
|
||||
|
||||
// Count returns the total number of users matching the filter
|
||||
Count(ctx context.Context, req *domain.ListUsersRequest) (int, error)
|
||||
|
||||
// ExistsByEmail checks if a user exists with the given email
|
||||
ExistsByEmail(ctx context.Context, email string) (bool, error)
|
||||
}
|
||||
|
||||
// UserProfileRepository defines the interface for user profile operations
|
||||
type UserProfileRepository interface {
|
||||
// Create creates a new user profile
|
||||
Create(ctx context.Context, profile *domain.UserProfile) error
|
||||
|
||||
// GetByUserID retrieves a user profile by user ID
|
||||
GetByUserID(ctx context.Context, userID uuid.UUID) (*domain.UserProfile, error)
|
||||
|
||||
// Update updates an existing user profile
|
||||
Update(ctx context.Context, profile *domain.UserProfile) error
|
||||
|
||||
// Delete deletes a user profile by user ID
|
||||
Delete(ctx context.Context, userID uuid.UUID) error
|
||||
}
|
||||
|
||||
// UserSessionRepository defines the interface for user session operations
|
||||
type UserSessionRepository interface {
|
||||
// Create creates a new user session
|
||||
Create(ctx context.Context, session *domain.UserSession) error
|
||||
|
||||
// GetByToken retrieves a session by token
|
||||
GetByToken(ctx context.Context, token string) (*domain.UserSession, error)
|
||||
|
||||
// GetByUserID retrieves all sessions for a user
|
||||
GetByUserID(ctx context.Context, userID uuid.UUID) ([]domain.UserSession, error)
|
||||
|
||||
// Update updates an existing session (e.g., last used time)
|
||||
Update(ctx context.Context, session *domain.UserSession) error
|
||||
|
||||
// Delete deletes a session by ID
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
|
||||
// DeleteByUserID deletes all sessions for a user
|
||||
DeleteByUserID(ctx context.Context, userID uuid.UUID) error
|
||||
|
||||
// DeleteExpired deletes all expired sessions
|
||||
DeleteExpired(ctx context.Context) error
|
||||
|
||||
// IsValidToken checks if a token is valid and not expired
|
||||
IsValidToken(ctx context.Context, token string) (bool, error)
|
||||
}
|
||||
|
||||
// AuditRepository defines the interface for audit logging
|
||||
type AuditRepository interface {
|
||||
// LogEvent logs an audit event
|
||||
LogEvent(ctx context.Context, event *AuditEvent) error
|
||||
|
||||
// GetEvents retrieves audit events with filtering
|
||||
GetEvents(ctx context.Context, req *GetEventsRequest) (*GetEventsResponse, error)
|
||||
}
|
||||
|
||||
// AuditEvent represents an audit event
|
||||
type AuditEvent struct {
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
Type string `json:"type" db:"type"`
|
||||
Severity string `json:"severity" db:"severity"`
|
||||
Status string `json:"status" db:"status"`
|
||||
Timestamp string `json:"timestamp" db:"timestamp"`
|
||||
ActorID string `json:"actor_id" db:"actor_id"`
|
||||
ActorType string `json:"actor_type" db:"actor_type"`
|
||||
ActorIP string `json:"actor_ip" db:"actor_ip"`
|
||||
UserAgent string `json:"user_agent" db:"user_agent"`
|
||||
ResourceID string `json:"resource_id" db:"resource_id"`
|
||||
ResourceType string `json:"resource_type" db:"resource_type"`
|
||||
Action string `json:"action" db:"action"`
|
||||
Description string `json:"description" db:"description"`
|
||||
Details map[string]interface{} `json:"details" db:"details"`
|
||||
RequestID string `json:"request_id" db:"request_id"`
|
||||
SessionID string `json:"session_id" db:"session_id"`
|
||||
}
|
||||
|
||||
// GetEventsRequest represents a request to get audit events
|
||||
type GetEventsRequest struct {
|
||||
UserID *uuid.UUID `json:"user_id,omitempty"`
|
||||
ResourceType *string `json:"resource_type,omitempty"`
|
||||
Action *string `json:"action,omitempty"`
|
||||
StartTime *string `json:"start_time,omitempty"`
|
||||
EndTime *string `json:"end_time,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
Offset int `json:"offset,omitempty"`
|
||||
}
|
||||
|
||||
// GetEventsResponse represents a response for audit events
|
||||
type GetEventsResponse struct {
|
||||
Events []AuditEvent `json:"events"`
|
||||
Total int `json:"total"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
HasMore bool `json:"has_more"`
|
||||
}
|
||||
158
user/internal/repository/postgres/profile_repository.go
Normal file
158
user/internal/repository/postgres/profile_repository.go
Normal file
@ -0,0 +1,158 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/RyanCopley/skybridge/user/internal/domain"
|
||||
"github.com/RyanCopley/skybridge/user/internal/repository/interfaces"
|
||||
)
|
||||
|
||||
type userProfileRepository struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// NewUserProfileRepository creates a new user profile repository
|
||||
func NewUserProfileRepository(db *sqlx.DB) interfaces.UserProfileRepository {
|
||||
return &userProfileRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *userProfileRepository) Create(ctx context.Context, profile *domain.UserProfile) error {
|
||||
profile.CreatedAt = time.Now()
|
||||
profile.UpdatedAt = time.Now()
|
||||
|
||||
// Convert preferences to JSON
|
||||
var preferencesJSON []byte
|
||||
if profile.Preferences != nil {
|
||||
var err error
|
||||
preferencesJSON, err = json.Marshal(profile.Preferences)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal preferences: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
query := `
|
||||
INSERT INTO user_profiles (
|
||||
user_id, bio, location, website, timezone, language,
|
||||
preferences, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9
|
||||
)`
|
||||
|
||||
_, err := r.db.ExecContext(ctx, query,
|
||||
profile.UserID, profile.Bio, profile.Location, profile.Website,
|
||||
profile.Timezone, profile.Language, preferencesJSON,
|
||||
profile.CreatedAt, profile.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create user profile: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *userProfileRepository) GetByUserID(ctx context.Context, userID uuid.UUID) (*domain.UserProfile, error) {
|
||||
query := `
|
||||
SELECT user_id, bio, location, website, timezone, language,
|
||||
preferences, created_at, updated_at
|
||||
FROM user_profiles
|
||||
WHERE user_id = $1`
|
||||
|
||||
row := r.db.QueryRowContext(ctx, query, userID)
|
||||
|
||||
var profile domain.UserProfile
|
||||
var preferencesJSON sql.NullString
|
||||
|
||||
err := row.Scan(
|
||||
&profile.UserID, &profile.Bio, &profile.Location, &profile.Website,
|
||||
&profile.Timezone, &profile.Language, &preferencesJSON,
|
||||
&profile.CreatedAt, &profile.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("user profile not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user profile: %w", err)
|
||||
}
|
||||
|
||||
// Parse preferences JSON
|
||||
if preferencesJSON.Valid && preferencesJSON.String != "" {
|
||||
var preferences map[string]interface{}
|
||||
err = json.Unmarshal([]byte(preferencesJSON.String), &preferences)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal preferences: %w", err)
|
||||
}
|
||||
profile.Preferences = preferences
|
||||
}
|
||||
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func (r *userProfileRepository) Update(ctx context.Context, profile *domain.UserProfile) error {
|
||||
profile.UpdatedAt = time.Now()
|
||||
|
||||
// Convert preferences to JSON
|
||||
var preferencesJSON []byte
|
||||
if profile.Preferences != nil {
|
||||
var err error
|
||||
preferencesJSON, err = json.Marshal(profile.Preferences)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal preferences: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
query := `
|
||||
UPDATE user_profiles SET
|
||||
bio = $2,
|
||||
location = $3,
|
||||
website = $4,
|
||||
timezone = $5,
|
||||
language = $6,
|
||||
preferences = $7,
|
||||
updated_at = $8
|
||||
WHERE user_id = $1`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query,
|
||||
profile.UserID, profile.Bio, profile.Location, profile.Website,
|
||||
profile.Timezone, profile.Language, preferencesJSON,
|
||||
profile.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update user profile: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get affected rows: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("user profile not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *userProfileRepository) Delete(ctx context.Context, userID uuid.UUID) error {
|
||||
query := `DELETE FROM user_profiles WHERE user_id = $1`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete user profile: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get affected rows: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("user profile not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
305
user/internal/repository/postgres/user_repository.go
Normal file
305
user/internal/repository/postgres/user_repository.go
Normal file
@ -0,0 +1,305 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/lib/pq"
|
||||
|
||||
"github.com/RyanCopley/skybridge/user/internal/domain"
|
||||
"github.com/RyanCopley/skybridge/user/internal/repository/interfaces"
|
||||
)
|
||||
|
||||
type userRepository struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// NewUserRepository creates a new user repository
|
||||
func NewUserRepository(db *sqlx.DB) interfaces.UserRepository {
|
||||
return &userRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *userRepository) Create(ctx context.Context, user *domain.User) error {
|
||||
query := `
|
||||
INSERT INTO users (
|
||||
id, email, first_name, last_name, display_name, avatar,
|
||||
role, status, created_at, updated_at, created_by, updated_by
|
||||
) VALUES (
|
||||
:id, :email, :first_name, :last_name, :display_name, :avatar,
|
||||
:role, :status, :created_at, :updated_at, :created_by, :updated_by
|
||||
)`
|
||||
|
||||
if user.ID == uuid.Nil {
|
||||
user.ID = uuid.New()
|
||||
}
|
||||
user.CreatedAt = time.Now()
|
||||
user.UpdatedAt = time.Now()
|
||||
|
||||
if user.Status == "" {
|
||||
user.Status = domain.UserStatusPending
|
||||
}
|
||||
|
||||
_, err := r.db.NamedExecContext(ctx, query, user)
|
||||
if err != nil {
|
||||
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23505" {
|
||||
return fmt.Errorf("user with email %s already exists", user.Email)
|
||||
}
|
||||
return fmt.Errorf("failed to create user: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *userRepository) GetByID(ctx context.Context, id uuid.UUID) (*domain.User, error) {
|
||||
query := `
|
||||
SELECT id, email, first_name, last_name, display_name, avatar,
|
||||
role, status, last_login_at, created_at, updated_at, created_by, updated_by
|
||||
FROM users
|
||||
WHERE id = $1`
|
||||
|
||||
var user domain.User
|
||||
err := r.db.GetContext(ctx, &user, query, id)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("user not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user: %w", err)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) GetByEmail(ctx context.Context, email string) (*domain.User, error) {
|
||||
query := `
|
||||
SELECT id, email, first_name, last_name, display_name, avatar,
|
||||
role, status, last_login_at, created_at, updated_at, created_by, updated_by
|
||||
FROM users
|
||||
WHERE email = $1`
|
||||
|
||||
var user domain.User
|
||||
err := r.db.GetContext(ctx, &user, query, email)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("user not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user: %w", err)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) Update(ctx context.Context, user *domain.User) error {
|
||||
user.UpdatedAt = time.Now()
|
||||
|
||||
query := `
|
||||
UPDATE users SET
|
||||
email = :email,
|
||||
first_name = :first_name,
|
||||
last_name = :last_name,
|
||||
display_name = :display_name,
|
||||
avatar = :avatar,
|
||||
role = :role,
|
||||
status = :status,
|
||||
last_login_at = :last_login_at,
|
||||
updated_at = :updated_at,
|
||||
updated_by = :updated_by
|
||||
WHERE id = :id`
|
||||
|
||||
result, err := r.db.NamedExecContext(ctx, query, user)
|
||||
if err != nil {
|
||||
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23505" {
|
||||
return fmt.Errorf("user with email %s already exists", user.Email)
|
||||
}
|
||||
return fmt.Errorf("failed to update user: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get affected rows: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *userRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
query := `DELETE FROM users WHERE id = $1`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete user: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get affected rows: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *userRepository) List(ctx context.Context, req *domain.ListUsersRequest) (*domain.ListUsersResponse, error) {
|
||||
// Build WHERE clause
|
||||
var conditions []string
|
||||
var args []interface{}
|
||||
argCounter := 1
|
||||
|
||||
if req.Status != nil {
|
||||
conditions = append(conditions, fmt.Sprintf("status = $%d", argCounter))
|
||||
args = append(args, *req.Status)
|
||||
argCounter++
|
||||
}
|
||||
|
||||
if req.Role != nil {
|
||||
conditions = append(conditions, fmt.Sprintf("role = $%d", argCounter))
|
||||
args = append(args, *req.Role)
|
||||
argCounter++
|
||||
}
|
||||
|
||||
if req.Search != "" {
|
||||
searchPattern := "%" + strings.ToLower(req.Search) + "%"
|
||||
conditions = append(conditions, fmt.Sprintf("(LOWER(email) LIKE $%d OR LOWER(first_name) LIKE $%d OR LOWER(last_name) LIKE $%d OR LOWER(display_name) LIKE $%d)", argCounter, argCounter, argCounter, argCounter))
|
||||
args = append(args, searchPattern)
|
||||
argCounter++
|
||||
}
|
||||
|
||||
whereClause := ""
|
||||
if len(conditions) > 0 {
|
||||
whereClause = "WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
// Build ORDER BY clause
|
||||
orderBy := "created_at"
|
||||
orderDir := "DESC"
|
||||
if req.OrderBy != "" {
|
||||
orderBy = req.OrderBy
|
||||
}
|
||||
if req.OrderDir != "" {
|
||||
orderDir = strings.ToUpper(req.OrderDir)
|
||||
}
|
||||
|
||||
// Set default pagination
|
||||
limit := 20
|
||||
if req.Limit > 0 {
|
||||
limit = req.Limit
|
||||
}
|
||||
offset := 0
|
||||
if req.Offset > 0 {
|
||||
offset = req.Offset
|
||||
}
|
||||
|
||||
// Query for users
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, email, first_name, last_name, display_name, avatar,
|
||||
role, status, last_login_at, created_at, updated_at, created_by, updated_by
|
||||
FROM users
|
||||
%s
|
||||
ORDER BY %s %s
|
||||
LIMIT $%d OFFSET $%d`,
|
||||
whereClause, orderBy, orderDir, argCounter, argCounter+1)
|
||||
|
||||
args = append(args, limit, offset)
|
||||
|
||||
var users []domain.User
|
||||
err := r.db.SelectContext(ctx, &users, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list users: %w", err)
|
||||
}
|
||||
|
||||
// Get total count
|
||||
total, err := r.Count(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get user count: %w", err)
|
||||
}
|
||||
|
||||
hasMore := offset+len(users) < total
|
||||
|
||||
return &domain.ListUsersResponse{
|
||||
Users: users,
|
||||
Total: total,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
HasMore: hasMore,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) UpdateLastLogin(ctx context.Context, id uuid.UUID) error {
|
||||
query := `UPDATE users SET last_login_at = $1 WHERE id = $2`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, time.Now(), id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update last login: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get affected rows: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *userRepository) Count(ctx context.Context, req *domain.ListUsersRequest) (int, error) {
|
||||
var conditions []string
|
||||
var args []interface{}
|
||||
argCounter := 1
|
||||
|
||||
if req.Status != nil {
|
||||
conditions = append(conditions, fmt.Sprintf("status = $%d", argCounter))
|
||||
args = append(args, *req.Status)
|
||||
argCounter++
|
||||
}
|
||||
|
||||
if req.Role != nil {
|
||||
conditions = append(conditions, fmt.Sprintf("role = $%d", argCounter))
|
||||
args = append(args, *req.Role)
|
||||
argCounter++
|
||||
}
|
||||
|
||||
if req.Search != "" {
|
||||
searchPattern := "%" + strings.ToLower(req.Search) + "%"
|
||||
conditions = append(conditions, fmt.Sprintf("(LOWER(email) LIKE $%d OR LOWER(first_name) LIKE $%d OR LOWER(last_name) LIKE $%d OR LOWER(display_name) LIKE $%d)", argCounter, argCounter, argCounter, argCounter))
|
||||
args = append(args, searchPattern)
|
||||
argCounter++
|
||||
}
|
||||
|
||||
whereClause := ""
|
||||
if len(conditions) > 0 {
|
||||
whereClause = "WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("SELECT COUNT(*) FROM users %s", whereClause)
|
||||
|
||||
var count int
|
||||
err := r.db.GetContext(ctx, &count, query, args...)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to count users: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) ExistsByEmail(ctx context.Context, email string) (bool, error) {
|
||||
query := `SELECT EXISTS(SELECT 1 FROM users WHERE email = $1)`
|
||||
|
||||
var exists bool
|
||||
err := r.db.GetContext(ctx, &exists, query, email)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to check user existence: %w", err)
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
Reference in New Issue
Block a user