79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package processor
|
|
|
|
import (
|
|
"context"
|
|
"eslogad-be/internal/appcontext"
|
|
"eslogad-be/internal/contract"
|
|
"eslogad-be/internal/transformer"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type RepositoryAttachmentProcessorImpl struct {
|
|
attachmentRepo RepositoryAttachmentRepository
|
|
}
|
|
|
|
func NewRepositoryAttachmentProcessor(attachmentRepo RepositoryAttachmentRepository) *RepositoryAttachmentProcessorImpl {
|
|
return &RepositoryAttachmentProcessorImpl{
|
|
attachmentRepo: attachmentRepo,
|
|
}
|
|
}
|
|
|
|
func (p *RepositoryAttachmentProcessorImpl) CreateAttachment(ctx context.Context, req *contract.CreateRepositoryAttachmentRequest) (*contract.RepositoryAttachmentsResponse, error) {
|
|
userID := getUserIDFromContext(ctx)
|
|
|
|
attachmentEntity := transformer.CreateRepositoryAttachmentRequestToEntity(req, userID)
|
|
|
|
err := p.attachmentRepo.Create(ctx, attachmentEntity)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create repository attachment: %w", err)
|
|
}
|
|
|
|
return transformer.RepositoryAttachmentEntityToContract(attachmentEntity), nil
|
|
}
|
|
|
|
func getUserIDFromContext(ctx context.Context) uuid.UUID {
|
|
appCtx := appcontext.FromGinContext(ctx)
|
|
if appCtx != nil {
|
|
return appCtx.UserID
|
|
}
|
|
return uuid.New()
|
|
}
|
|
|
|
func (p *RepositoryAttachmentProcessorImpl) DeleteAttachment(ctx context.Context, id uuid.UUID) error {
|
|
_, err := p.attachmentRepo.GetByID(ctx, id)
|
|
if err != nil {
|
|
return fmt.Errorf("repository attachment not found: %w", err)
|
|
}
|
|
|
|
err = p.attachmentRepo.Delete(ctx, id)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete repository attachment: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *RepositoryAttachmentProcessorImpl) GetById(ctx context.Context, id uuid.UUID) (*contract.RepositoryAttachmentsResponse, error) {
|
|
attachment, err := p.attachmentRepo.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("repository attachment not found: %w", err)
|
|
}
|
|
|
|
resp := transformer.RepositoryAttachmentEntityToContract(attachment)
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
func (p *RepositoryAttachmentProcessorImpl) ListAttachment(ctx context.Context, search *string, limit, offset int) ([]contract.RepositoryAttachmentsResponse, int, error) {
|
|
attachments, totalCount, err := p.attachmentRepo.List(ctx, search, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("failed to get users: %w", err)
|
|
}
|
|
|
|
responses := transformer.RepositoryAttachmentEntityToContracts(attachments)
|
|
|
|
return responses, int(totalCount), nil
|
|
}
|