-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_analysis.go
61 lines (51 loc) · 1.85 KB
/
delete_analysis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package usecase
import (
"context"
"errors"
"github.com/eroatta/src-reader/repository"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
// ErrAnalysisNotFound indicates there is no analysis to be processed on the usecase.
var ErrAnalysisNotFound = errors.New("analysis not found")
// DeleteAnalysisUsecase defines the contract for the use case related to delete an existing analysis.
type DeleteAnalysisUsecase interface {
// Process handles the process to delete an existing Analysis, given its ID.
Process(ctx context.Context, analysisID uuid.UUID) error
}
// NewDeleteAnalysisUsecase create initializes a DeleteAnalysisUsecase instance.
func NewDeleteAnalysisUsecase(duc DeleteInsightsUsecase, ir repository.IdentifierRepository, ar repository.AnalysisRepository) DeleteAnalysisUsecase {
return deleteAnalysisUsecase{
deleteInsightsUsecase: duc,
ir: ir,
ar: ar,
}
}
type deleteAnalysisUsecase struct {
deleteInsightsUsecase DeleteInsightsUsecase
ir repository.IdentifierRepository
ar repository.AnalysisRepository
}
func (uc deleteAnalysisUsecase) Process(ctx context.Context, analysisID uuid.UUID) error {
err := uc.deleteInsightsUsecase.Process(ctx, analysisID)
if err == ErrUnexpected {
log.Errorf("unable to execute delete insights usecase for analysis ID: %v", analysisID)
return ErrUnexpected
}
err = uc.ir.DeleteAllByAnalysisID(ctx, analysisID)
if err == repository.ErrIdentifierUnexpected {
log.Errorf("unable to delete identifiers for analysis ID: %v", analysisID)
return ErrUnexpected
}
err = uc.ar.Delete(ctx, analysisID)
switch err {
case nil:
// do nothing
case repository.ErrAnalysisNoResults:
return ErrAnalysisNotFound
default:
log.Errorf("unable to delete analysis with ID: %v", analysisID)
return ErrUnexpected
}
return nil
}