Initial commit: Эфир мессенджер
This commit is contained in:
99
internal/repository/sqlite/profile_repo.go
Normal file
99
internal/repository/sqlite/profile_repo.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"messenger/internal/models"
|
||||
)
|
||||
|
||||
type ProfileRepository struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewProfileRepository(db *DB) *ProfileRepository {
|
||||
return &ProfileRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *ProfileRepository) Create(ctx context.Context, profile *models.Profile) error {
|
||||
query := `
|
||||
INSERT INTO profiles (user_id, display_name, bio, avatar_url)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`
|
||||
|
||||
_, err := r.db.ExecContext(ctx, query,
|
||||
profile.UserID, profile.DisplayName, profile.Bio, profile.AvatarURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create profile: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProfileRepository) FindByUserID(ctx context.Context, userID int64) (*models.Profile, error) {
|
||||
query := `
|
||||
SELECT user_id, display_name, bio, avatar_url
|
||||
FROM profiles
|
||||
WHERE user_id = ?
|
||||
`
|
||||
|
||||
var profile models.Profile
|
||||
|
||||
err := r.db.QueryRowContext(ctx, query, userID).Scan(
|
||||
&profile.UserID, &profile.DisplayName, &profile.Bio, &profile.AvatarURL,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find profile by user_id: %w", err)
|
||||
}
|
||||
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func (r *ProfileRepository) Update(ctx context.Context, profile *models.Profile) error {
|
||||
query := `
|
||||
UPDATE profiles
|
||||
SET display_name = COALESCE(?, display_name),
|
||||
bio = COALESCE(?, bio)
|
||||
WHERE user_id = ?
|
||||
`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, profile.DisplayName, profile.Bio, profile.UserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update profile: %w", err)
|
||||
}
|
||||
|
||||
rows, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("profile not found for user: %d", profile.UserID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProfileRepository) UpdateAvatar(ctx context.Context, userID int64, avatarURL *string) error {
|
||||
query := `UPDATE profiles SET avatar_url = ? WHERE user_id = ?`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, avatarURL, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update avatar: %w", err)
|
||||
}
|
||||
|
||||
rows, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("profile not found for user: %d", userID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user