func ensureTagIDsBelongToOperation(db *database.Connection, tagIDs []int64, operation *models.Operation) error {
	if len(tagIDs) == 0 {
		return nil
	}
	var badTags []dtos.Tag
	err := db.Select(&badTags, sq.Select("id").
		From("tags").
		Where(sq.Eq{"id": tagIDs}).
		Where(sq.NotEq{"operation_id": operation.ID}))
	if err != nil {
		return backend.WrapError("Unable to lookup tags by operation ID", err)
	}
	if len(badTags) > 0 {
		return fmt.Errorf("Unable to verify tags for operation. Tags [%v] do not belong to operation %s", badTags, operation.Slug)
	}
	return nil
}
Returns an error if any specified tagIDs belong to an operation other than the one specified
package services
import (
	"context"
	"fmt"
	"github.com/theparanoids/ashirt-server/backend"
	"github.com/theparanoids/ashirt-server/backend/database"
	"github.com/theparanoids/ashirt-server/backend/dtos"
	"github.com/theparanoids/ashirt-server/backend/models"
	"github.com/theparanoids/ashirt-server/backend/policy"
	"github.com/theparanoids/ashirt-server/backend/server/middleware"
	sq "github.com/Masterminds/squirrel"
)
func getFindingCategory(db *database.Connection, findingCategoryID int64) (string, error) {
	var category string
	err := db.Get(&category, sq.Select("category").
		From("finding_categories").
		Where(sq.Eq{"id": findingCategoryID}),
	)
	return category, err
}
func getFindingCategoryID(findingCategory string, selectFunc func(modalSlice interface{}, sb sq.SelectBuilder) error) (*int64, error) {
	var foundCategoryID []int64
	err := selectFunc(&foundCategoryID, sq.Select("id").
		From("finding_categories").
		Where(sq.Eq{"category": findingCategory}),
	)
	if err != nil {
		return nil, err
	}
	if len(foundCategoryID) == 0 {
		return nil, nil
	}
	return &foundCategoryID[0], nil
}
func tagsForEvidenceByID(db *database.Connection, evidenceIDs []int64) (tagsByEvidenceID map[int64][]dtos.Tag, allTags []dtos.Tag, err error) {
	if len(evidenceIDs) == 0 {
		allTags = []dtos.Tag{}
		return
	}
	var tags []struct {
		models.Tag
		EvidenceID int64 `db:"evidence_id"`
	}
	err = db.Select(&tags, sq.Select("evidence_id", "tags.*").
		From("tag_evidence_map").
		LeftJoin("tags ON tag_id = tags.id").
		Where(sq.Eq{"evidence_id": evidenceIDs}).
		OrderBy("tag_id ASC"))
	if err != nil {
		return
	}
	allTagsByTagID := map[int64]bool{}
	allTags = []dtos.Tag{}
	tagsByEvidenceID = map[int64][]dtos.Tag{}
	for _, tag := range tags {
		tagDTO := dtos.Tag{
			ID:        tag.ID,
			Name:      tag.Name,
			ColorName: tag.ColorName,
		}
		if !allTagsByTagID[tag.ID] {
			allTags = append(allTags, tagDTO)
			allTagsByTagID[tag.ID] = true
		}
		tagsByEvidenceID[tag.EvidenceID] = append(tagsByEvidenceID[tag.EvidenceID], tagDTO)
	}
	return
}
func lookupOperation(db *database.Connection, operationSlug string) (*models.Operation, error) {
	var operation models.Operation
	err := db.Get(&operation, sq.Select("id", "name", "status").
		From("operations").
		Where(sq.Eq{"slug": operationSlug}))
	if err != nil {
		return &operation, backend.WrapError("Unable to lookup operation by slug", err)
	}
	return &operation, nil
}
func lookupOperationFinding(db *database.Connection, operationSlug string, findingUUID string) (*models.Operation, *models.Finding, error) {
	operation, err := lookupOperation(db, operationSlug)
	if err != nil {
		return nil, nil, err
	}
	var finding models.Finding
	err = db.Get(&finding, sq.Select("*").From("findings").Where(sq.Eq{"uuid": findingUUID}))
	if err != nil {
		return nil, nil, backend.WrapError("Unable to lookup finding by uuid", err)
	}
	if finding.OperationID != operation.ID {
		return nil, nil, fmt.Errorf("Unable to lookup operation/finding. Finding %d belongs to operation %d not %d", finding.ID, finding.OperationID, operation.ID)
	}
	return operation, &finding, nil
}
func lookupOperationEvidence(db *database.Connection, operationSlug string, evidenceUUID string) (*models.Operation, *models.Evidence, error) {
	operation, err := lookupOperation(db, operationSlug)
	if err != nil {
		return nil, nil, err
	}
	var evidence models.Evidence
	err = db.Get(&evidence, sq.Select("*").
		From("evidence").
		Where(sq.Eq{"uuid": evidenceUUID}))
	if err != nil {
		return nil, nil, backend.WrapError("Unable to lookup evidence by uuid", err)
	}
	if evidence.OperationID != operation.ID {
		return nil, nil, fmt.Errorf("Unable to lookup operation/evidence. Evidence %d belongs to operation %d not %d", evidence.ID, evidence.OperationID, operation.ID)
	}
	return operation, &evidence, nil
}