add avatar endpoint

This commit is contained in:
kandrusyak
2023-07-28 00:58:54 +03:00
parent 4d01d35eae
commit 4c61eb82a0
5 changed files with 152 additions and 1 deletions

View File

@@ -1,7 +1,9 @@
package handler
import (
"astra-users/config"
"astra-users/model"
"astra-users/pkg/api"
"astra-users/pkg/utils"
"crypto/sha256"
"encoding/json"
@@ -151,3 +153,44 @@ func (h *Handler) CreateUser(c echo.Context) (err error) {
return c.JSON(http.StatusCreated, newUser)
}
func (h *Handler) UploadAvatar(c echo.Context) error {
id := c.Param("id")
type FileSuccess struct {
FileName string `json:"file_name"`
}
result := new(FileSuccess)
data, err, resp := api.PostRequest(config.Env.MSHosts.StorageHost+"/upload", c.Request().Body, c.Request().Header)
if err != nil {
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
}
err = json.Unmarshal(data, &result)
if err != nil {
return c.NoContent(http.StatusBadGateway)
}
user := new(model.User)
tx := h.DB.Where("id = ?", id).First(&user)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusNotFound)
}
user.Avatar = result.FileName
tx = h.DB.Save(&user)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusBadGateway)
}
return c.JSON(http.StatusOK, map[string]string{
"avatar": result.FileName,
})
}