repository attachment api
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"eslogad-be/internal/entities"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RepositoryAttachmentRepository interface {
|
||||
Create(ctx context.Context, user *entities.RepositoryAttachment) error
|
||||
GetByID(ctx context.Context, id uuid.UUID) (*entities.RepositoryAttachment, error)
|
||||
Update(ctx context.Context, user *entities.RepositoryAttachment) error
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
List(ctx context.Context, search *string, limit, offset int) ([]*entities.RepositoryAttachment, int64, error)
|
||||
}
|
||||
Reference in New Issue
Block a user