mirror of
https://github.com/go-gitea/gitea
synced 2025-01-31 20:14:28 +00:00
8b86c3140a
Co-authored-by: Diogo Vicente <diogo.m.s.vicente@tecnico.ulisboa.pt>
76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package user
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
)
|
|
|
|
// RenameBadge changes the slug of a badge.
|
|
func RenameBadge(ctx context.Context, b *user_model.Badge, newSlug string) error {
|
|
if newSlug == b.Slug {
|
|
return nil
|
|
}
|
|
|
|
olderSlug := b.Slug
|
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
|
|
isExist, err := user_model.IsBadgeExist(ctx, b.ID, newSlug)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if isExist {
|
|
return user_model.ErrBadgeAlreadyExist{
|
|
Slug: newSlug,
|
|
}
|
|
}
|
|
|
|
b.Slug = newSlug
|
|
if err := user_model.UpdateBadge(ctx, b); err != nil {
|
|
b.Slug = olderSlug
|
|
return err
|
|
}
|
|
if err = committer.Commit(); err != nil {
|
|
b.Slug = olderSlug
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteBadge completely and permanently deletes everything of a badge
|
|
func DeleteBadge(ctx context.Context, b *user_model.Badge, purge bool) error {
|
|
if purge {
|
|
err := user_model.DeleteUserBadgeRecord(ctx, b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
|
|
if err := user_model.DeleteBadge(ctx, b); err != nil {
|
|
return fmt.Errorf("DeleteBadge: %w", err)
|
|
}
|
|
|
|
if err := committer.Commit(); err != nil {
|
|
return err
|
|
}
|
|
_ = committer.Close()
|
|
|
|
return nil
|
|
}
|