add api calls for removing tags, initial branch commit

This commit is contained in:
Christoph Kluge
2025-04-17 17:31:59 +02:00
parent f343fa0071
commit 9bcf7adb67
2 changed files with 213 additions and 1 deletions

View File

@@ -45,7 +45,7 @@ func (r *JobRepository) AddTag(user *schema.User, job int64, tag int64) ([]*sche
return tags, archive.UpdateTags(j, archiveTags)
}
// Removes a tag from a job
// Removes a tag from a job by its ID
func (r *JobRepository) RemoveTag(user *schema.User, job, tag int64) ([]*schema.Tag, error) {
j, err := r.FindByIdWithUser(user, job)
if err != nil {
@@ -76,6 +76,76 @@ func (r *JobRepository) RemoveTag(user *schema.User, job, tag int64) ([]*schema.
return tags, archive.UpdateTags(j, archiveTags)
}
// Removes a tag from a job by tag info
func (r *JobRepository) RemoveJobTagByRequest(user *schema.User, job int64, tagType string, tagName string, tagScope string) ([]*schema.Tag, error) {
// Get Tag ID to delete
tagID, err := r.loadTagIDByInfo(tagName, tagType, tagScope)
if err != nil {
log.Warn("Error while finding tagId with: %s, %s, %s", tagName, tagType, tagScope)
return nil, err
}
// Get Job
j, err := r.FindByIdWithUser(user, job)
if err != nil {
log.Warn("Error while finding job by id")
return nil, err
}
// Handle Delete
q := sq.Delete("jobtag").Where("jobtag.job_id = ?", job).Where("jobtag.tag_id = ?", tagID)
if _, err := q.RunWith(r.stmtCache).Exec(); err != nil {
s, _, _ := q.ToSql()
log.Errorf("Error removing tag from table 'jobTag' with %s: %v", s, err)
return nil, err
}
tags, err := r.GetTags(user, &job)
if err != nil {
log.Warn("Error while getting tags for job")
return nil, err
}
archiveTags, err := r.getArchiveTags(&job)
if err != nil {
log.Warn("Error while getting tags for job")
return nil, err
}
return tags, archive.UpdateTags(j, archiveTags)
}
// Removes a tag from db by tag info
func (r *JobRepository) RemoveTagByRequest(tagType string, tagName string, tagScope string) error {
// Get Tag ID to delete
tagID, err := r.loadTagIDByInfo(tagName, tagType, tagScope)
if err != nil {
log.Warn("Error while finding tagId with: %s, %s, %s", tagName, tagType, tagScope)
return err
}
// Handle Delete JobTagTable
qJobTag := sq.Delete("jobtag").Where("jobtag.tag_id = ?", tagID)
if _, err := qJobTag.RunWith(r.stmtCache).Exec(); err != nil {
s, _, _ := qJobTag.ToSql()
log.Errorf("Error removing tag from table 'jobTag' with %s: %v", s, err)
return err
}
// Handle Delete TagTable
qTag := sq.Delete("tag").Where("tag.id = ?", tagID)
if _, err := qTag.RunWith(r.stmtCache).Exec(); err != nil {
s, _, _ := qTag.ToSql()
log.Errorf("Error removing tag from table 'tag' with %s: %v", s, err)
return err
}
return nil
}
// CreateTag creates a new tag with the specified type and name and returns its database id.
func (r *JobRepository) CreateTag(tagType string, tagName string, tagScope string) (tagId int64, err error) {
// Default to "Global" scope if none defined
@@ -325,3 +395,29 @@ func (r *JobRepository) checkScopeAuth(user *schema.User, operation string, scop
return false, fmt.Errorf("error while checking tag operation auth: no user in context")
}
}
func (r *JobRepository) loadTagIDByInfo(tagType string, tagName string, tagScope string) (tagID int64, err error) {
// Get Tag ID to delete
getq := sq.Select("id").From("tag").
Where("tag_type = ?", tagType).
Where("tag_name = ?", tagName).
Where("tag_scope = ?", tagScope)
rows, err := getq.RunWith(r.stmtCache).Query()
if err != nil {
s, _, _ := getq.ToSql()
log.Errorf("Error get tags for delete with %s: %v", s, err)
return 0, err
}
dbTags := make([]*schema.Tag, 0)
for rows.Next() {
dbTag := &schema.Tag{}
if err := rows.Scan(&dbTag.ID); err != nil {
log.Warn("Error while scanning rows")
return 0, err
}
}
return dbTags[0].ID, nil
}