Files

133 lines
3.2 KiB
Go
Raw Permalink Normal View History

package sqlite
import (
"context"
"database/sql"
"fmt"
"messenger/internal/models"
)
type AttachmentRepository struct {
db *DB
}
func NewAttachmentRepository(db *DB) *AttachmentRepository {
return &AttachmentRepository{db: db}
}
func (r *AttachmentRepository) Create(ctx context.Context, attachment *models.Attachment) error {
query := `
INSERT INTO attachments (message_id, file_name, file_size, storage_path, mime_type, uploaded_at)
VALUES (?, ?, ?, ?, ?, ?)
RETURNING id
`
err := r.db.QueryRowContext(ctx, query,
attachment.MessageID, attachment.FileName, attachment.FileSize,
attachment.StoragePath, attachment.MimeType, attachment.UploadedAt,
).Scan(&attachment.ID)
if err != nil {
return fmt.Errorf("failed to create attachment: %w", err)
}
return nil
}
func (r *AttachmentRepository) FindByID(ctx context.Context, id int64) (*models.Attachment, error) {
query := `
SELECT id, message_id, file_name, file_size, storage_path, mime_type, uploaded_at
FROM attachments
WHERE id = ?
`
var attachment models.Attachment
var messageID sql.NullInt64
err := r.db.QueryRowContext(ctx, query, id).Scan(
&attachment.ID, &messageID, &attachment.FileName, &attachment.FileSize,
&attachment.StoragePath, &attachment.MimeType, &attachment.UploadedAt,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to find attachment by id: %w", err)
}
if messageID.Valid {
attachment.MessageID = &messageID.Int64
}
return &attachment, nil
}
func (r *AttachmentRepository) UpdateMessageID(ctx context.Context, attachmentID, messageID int64) error {
query := `UPDATE attachments SET message_id = ? WHERE id = ?`
result, err := r.db.ExecContext(ctx, query, messageID, attachmentID)
if err != nil {
return fmt.Errorf("failed to update attachment message_id: %w", err)
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return fmt.Errorf("attachment not found: %d", attachmentID)
}
return nil
}
func (r *AttachmentRepository) Delete(ctx context.Context, attachmentID int64) error {
query := `DELETE FROM attachments WHERE id = ?`
result, err := r.db.ExecContext(ctx, query, attachmentID)
if err != nil {
return fmt.Errorf("failed to delete attachment: %w", err)
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return fmt.Errorf("attachment not found: %d", attachmentID)
}
return nil
}
func (r *AttachmentRepository) GetByMessageID(ctx context.Context, messageID int64) (*models.Attachment, error) {
query := `
SELECT id, message_id, file_name, file_size, storage_path, mime_type, uploaded_at
FROM attachments
WHERE message_id = ?
`
var attachment models.Attachment
var msgID sql.NullInt64
err := r.db.QueryRowContext(ctx, query, messageID).Scan(
&attachment.ID, &msgID, &attachment.FileName, &attachment.FileSize,
&attachment.StoragePath, &attachment.MimeType, &attachment.UploadedAt,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get attachment by message_id: %w", err)
}
if msgID.Valid {
attachment.MessageID = &msgID.Int64
}
return &attachment, nil
}