Files
gochat/backend/internal/service/attachment_service.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

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)
}