Move more functions to db.Find (#28419)

Following #28220

This PR move more functions to use `db.Find`.

---------

Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
Lunny Xiao 2024-01-15 10:19:25 +08:00 committed by GitHub
parent e5313248a8
commit 70c4aad8e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 305 additions and 238 deletions

View file

@ -18,23 +18,25 @@ import (
)
func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions) {
keys, err := asymkey_model.ListGPGKeys(ctx, uid, listOptions)
keys, total, err := db.FindAndCount[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
ListOptions: listOptions,
OwnerID: uid,
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
return
}
if err := asymkey_model.GPGKeyList(keys).LoadSubKeys(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
return
}
apiKeys := make([]*api.GPGKey, len(keys))
for i := range keys {
apiKeys[i] = convert.ToGPGKey(keys[i])
}
total, err := asymkey_model.CountUserGPGKeys(ctx, uid)
if err != nil {
ctx.InternalServerError(err)
return
}
ctx.SetTotalCountHeader(total)
ctx.JSON(http.StatusOK, &apiKeys)
}
@ -121,6 +123,10 @@ func GetGPGKey(ctx *context.APIContext) {
}
return
}
if err := key.LoadSubKeys(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, "LoadSubKeys", err)
return
}
ctx.JSON(http.StatusOK, convert.ToGPGKey(key))
}
@ -198,7 +204,10 @@ func VerifyUserGPGKey(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "VerifyUserGPGKey", err)
}
key, err := asymkey_model.GetGPGKeysByKeyID(ctx, form.KeyID)
keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
KeyID: form.KeyID,
IncludeSubKeys: true,
})
if err != nil {
if asymkey_model.IsErrGPGKeyNotExist(err) {
ctx.NotFound()
@ -207,7 +216,7 @@ func VerifyUserGPGKey(ctx *context.APIContext) {
}
return
}
ctx.JSON(http.StatusOK, convert.ToGPGKey(key[0]))
ctx.JSON(http.StatusOK, convert.ToGPGKey(keys[0]))
}
// swagger:parameters userCurrentPostGPGKey