diff options
author | delvh <dev.lh@web.de> | 2023-12-25 21:25:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-25 21:25:29 +0100 |
commit | 778ad795fd4a19dc15723b59a846a250034c7c3a (patch) | |
tree | 8d64a21734517a9826655955d77cb0bcc187f784 /routers | |
parent | b41925cee3d67a1fe546c7a219174e4a8b2302b7 (diff) | |
download | gitea-778ad795fd4a19dc15723b59a846a250034c7c3a.tar.gz gitea-778ad795fd4a19dc15723b59a846a250034c7c3a.zip |
Refactor deletion (#28610)
Introduce the new generic deletion methods
- `func DeleteByID[T any](ctx context.Context, id int64) (int64, error)`
- `func DeleteByIDs[T any](ctx context.Context, ids ...int64) error`
- `func Delete[T any](ctx context.Context, opts FindOptions) (int64,
error)`
So, we no longer need any specific deletion method and can just use
the generic ones instead.
Replacement of #28450
Closes #28450
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/actions/artifacts.go | 13 | ||||
-rw-r--r-- | routers/api/v1/repo/mirror.go | 13 | ||||
-rw-r--r-- | routers/web/admin/notice.go | 3 | ||||
-rw-r--r-- | routers/web/repo/setting/setting.go | 2 |
4 files changed, 19 insertions, 12 deletions
diff --git a/routers/api/actions/artifacts.go b/routers/api/actions/artifacts.go index 5411237103..5bfcc9dfcd 100644 --- a/routers/api/actions/artifacts.go +++ b/routers/api/actions/artifacts.go @@ -63,7 +63,6 @@ package actions import ( "crypto/md5" - "errors" "fmt" "net/http" "strconv" @@ -423,15 +422,15 @@ func (ar artifactRoutes) downloadArtifact(ctx *ArtifactContext) { } artifactID := ctx.ParamsInt64("artifact_id") - artifact, err := actions.GetArtifactByID(ctx, artifactID) - if errors.Is(err, util.ErrNotExist) { - log.Error("Error getting artifact: %v", err) - ctx.Error(http.StatusNotFound, err.Error()) - return - } else if err != nil { + artifact, exist, err := db.GetByID[actions.ActionArtifact](ctx, artifactID) + if err != nil { log.Error("Error getting artifact: %v", err) ctx.Error(http.StatusInternalServerError, err.Error()) return + } else if !exist { + log.Error("artifact with ID %d does not exist", artifactID) + ctx.Error(http.StatusNotFound, fmt.Sprintf("artifact with ID %d does not exist", artifactID)) + return } if artifact.RunID != runID { log.Error("Error dismatch runID and artifactID, task: %v, artifact: %v", runID, artifactID) diff --git a/routers/api/v1/repo/mirror.go b/routers/api/v1/repo/mirror.go index 72ddc758dc..26e0be301c 100644 --- a/routers/api/v1/repo/mirror.go +++ b/routers/api/v1/repo/mirror.go @@ -227,11 +227,18 @@ func GetPushMirrorByName(ctx *context.APIContext) { mirrorName := ctx.Params(":name") // Get push mirror of a specific repo by remoteName - pushMirror, err := repo_model.GetPushMirror(ctx, repo_model.PushMirrorOptions{RepoID: ctx.Repo.Repository.ID, RemoteName: mirrorName}) + pushMirror, exist, err := db.Get[repo_model.PushMirror](ctx, repo_model.PushMirrorOptions{ + RepoID: ctx.Repo.Repository.ID, + RemoteName: mirrorName, + }.ToConds()) if err != nil { - ctx.Error(http.StatusNotFound, "GetPushMirrors", err) + ctx.Error(http.StatusInternalServerError, "GetPushMirrors", err) + return + } else if !exist { + ctx.Error(http.StatusNotFound, "GetPushMirrors", nil) return } + m, err := convert.ToPushMirror(ctx, pushMirror) if err != nil { ctx.ServerError("GetPushMirrorByRemoteName", err) @@ -368,7 +375,7 @@ func CreatePushMirror(ctx *context.APIContext, mirrorOption *api.CreatePushMirro RemoteAddress: remoteAddress, } - if err = repo_model.InsertPushMirror(ctx, pushMirror); err != nil { + if err = db.Insert(ctx, pushMirror); err != nil { ctx.ServerError("InsertPushMirror", err) return } diff --git a/routers/web/admin/notice.go b/routers/web/admin/notice.go index 99039a2a9f..e1cb578d05 100644 --- a/routers/web/admin/notice.go +++ b/routers/web/admin/notice.go @@ -8,6 +8,7 @@ import ( "net/http" "strconv" + "code.gitea.io/gitea/models/db" system_model "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -55,7 +56,7 @@ func DeleteNotices(ctx *context.Context) { } } - if err := system_model.DeleteNoticesByIDs(ctx, ids); err != nil { + if err := db.DeleteByIDs[system_model.Notice](ctx, ids...); err != nil { ctx.Flash.Error("DeleteNoticesByIDs: " + err.Error()) ctx.Status(http.StatusInternalServerError) } else { diff --git a/routers/web/repo/setting/setting.go b/routers/web/repo/setting/setting.go index 0864b1c911..289cf5c9ae 100644 --- a/routers/web/repo/setting/setting.go +++ b/routers/web/repo/setting/setting.go @@ -418,7 +418,7 @@ func SettingsPost(ctx *context.Context) { Interval: interval, RemoteAddress: remoteAddress, } - if err := repo_model.InsertPushMirror(ctx, m); err != nil { + if err := db.Insert(ctx, m); err != nil { ctx.ServerError("InsertPushMirror", err) return } |