77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
)
|
|
|
|
// AttachmentService implements business logic for Attachment operations.
|
|
// Reference: Chatwoot app/controllers/api/v1/attachments_controller.rb
|
|
type AttachmentService struct {
|
|
repo *repository.AttachmentRepo
|
|
}
|
|
|
|
// NewAttachmentService creates a new Attachment service.
|
|
func NewAttachmentService(repo *repository.AttachmentRepo) *AttachmentService {
|
|
return &AttachmentService{repo: repo}
|
|
}
|
|
|
|
// GetByID retrieves a single attachment.
|
|
func (s *AttachmentService) GetByID(ctx context.Context, id uint) (*model.Attachment, error) {
|
|
return s.repo.FindByID(ctx, id)
|
|
}
|
|
|
|
// ListByMessage retrieves all attachments for a message.
|
|
func (s *AttachmentService) ListByMessage(ctx context.Context, messageID uint) ([]model.Attachment, error) {
|
|
return s.repo.FindByMessage(ctx, messageID)
|
|
}
|
|
|
|
// CreateAttachmentRequest is the DTO for creating an attachment.
|
|
type CreateAttachmentRequest struct {
|
|
MessageID uint `json:"message_id" validate:"required"`
|
|
FileType string `json:"file_type" validate:"required,oneof=image audio video file location emoji contact"`
|
|
ExternalURL string `json:"external_url"`
|
|
FileURL string `json:"file_url"`
|
|
ThumbURL string `json:"thumb_url"`
|
|
FileSize int `json:"file_size"`
|
|
FileName string `json:"file_name"`
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
AltText string `json:"alt_text"`
|
|
Metadata string `json:"metadata"`
|
|
}
|
|
|
|
// Create adds a new attachment to a message.
|
|
func (s *AttachmentService) Create(ctx context.Context, req CreateAttachmentRequest) (*model.Attachment, error) {
|
|
attachment := &model.Attachment{
|
|
MessageID: req.MessageID,
|
|
FileType: req.FileType,
|
|
ExternalURL: req.ExternalURL,
|
|
FileURL: req.FileURL,
|
|
ThumbURL: req.ThumbURL,
|
|
FileSize: req.FileSize,
|
|
FileName: req.FileName,
|
|
Width: req.Width,
|
|
Height: req.Height,
|
|
AltText: req.AltText,
|
|
Metadata: req.Metadata,
|
|
}
|
|
if err := s.repo.Create(ctx, attachment); err != nil {
|
|
applogger.L().Errorf("AttachmentService.Create failed: %v", err)
|
|
return nil, err
|
|
}
|
|
return attachment, nil
|
|
}
|
|
|
|
// Delete removes an attachment.
|
|
func (s *AttachmentService) Delete(ctx context.Context, id uint) error {
|
|
return s.repo.Delete(ctx, id)
|
|
}
|
|
|
|
// DeleteByMessage removes all attachments for a message.
|
|
func (s *AttachmentService) DeleteByMessage(ctx context.Context, messageID uint) error {
|
|
return s.repo.DeleteByMessage(ctx, messageID)
|
|
} |