Feat: finish putImageAliases
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"gitea.konchin.com/go2025/backend/middlewares"
|
||||
"gitea.konchin.com/go2025/backend/utils"
|
||||
"github.com/uptrace/bunrouter"
|
||||
)
|
||||
|
||||
type putImageAliasesInputAlias = string
|
||||
type putImageAliasesInput struct {
|
||||
Aliases []string `json:"aliases"`
|
||||
}
|
||||
|
||||
// PutImageAliases
|
||||
//
|
||||
// @param id path int64 true "Image Id"
|
||||
// @param payload body []putImageAliasesInputAlias true "Payload"
|
||||
// @param id path int64 true "Image Id"
|
||||
// @param payload body putImageAliasesInput true "Payload"
|
||||
// @success 200
|
||||
// @failure 401
|
||||
// @failure 404
|
||||
@@ -20,5 +25,43 @@ type putImageAliasesInputAlias = string
|
||||
func (self *Handlers) PutImageAliases(
|
||||
w http.ResponseWriter, req bunrouter.Request,
|
||||
) error {
|
||||
ctx := req.Context()
|
||||
|
||||
imgId, err := req.Params().Int64("id")
|
||||
if err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Message: "falied to parse image id in path",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Message: "failed to read payload",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
|
||||
var input putImageAliasesInput
|
||||
if err := json.Unmarshal(b, &input); err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Message: "failed to unmarshal json",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
|
||||
err = self.db.UpdateAliases(ctx, imgId, input.Aliases)
|
||||
if err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "failed to update aliases",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
|
||||
return utils.Success(w)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user