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

@@ -9,8 +9,13 @@ type ServerConfig struct {
ConnectionString string
}
type MSHosts struct {
StorageHost string
}
type Config struct {
Server ServerConfig
MSHosts MSHosts
}
func New() *Config {
@@ -19,6 +24,9 @@ func New() *Config {
Port: getEnv("PORT", "8080"),
ConnectionString: getEnv("CONNECTION_STRING", "host=localhost user=astra_users password=password dbname=astra_users_db port=5432 sslmode=disable TimeZone=Europe/Moscow"),
},
MSHosts: MSHosts{
StorageHost: getEnv("ASTRA-FILE-STORAGE", "http://localhost:8081"),
},
}
}

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,
})
}

View File

@@ -44,6 +44,7 @@ func main() {
e.POST("/login", h.Login)
e.GET("/me", h.GetCurrentUser)
e.POST("/", h.CreateUser)
e.POST("/:id/avatar", h.UploadAvatar)
e.GET("/", h.GetUsers)
e.GET("/:id", h.GetUser)
e.GET("/batch/:ids", h.GetUserBatch)

View File

@@ -11,6 +11,7 @@ type (
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primarykey"`
APIUser
Position Position `gorm:"-:migration"`
Avatar string `json:"avatar"`
}
)

98
pkg/api/apiCall.go Normal file
View File

@@ -0,0 +1,98 @@
package api
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
func PostRequest(url string, body io.Reader, header http.Header) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("POST", url, body)
req.Header = header
resp, err := client.Do(req)
if err != nil {
return nil, err, resp
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err, resp
}
return data, nil, resp
}
func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if len(args) > 0 {
req.URL.RawQuery = args[0]
}
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err, nil
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err, nil
}
return data, nil, resp
}
func PatchRequest(url string, model any, header http.Header) ([]byte, error) {
uJson, err := json.Marshal(model)
if err != nil {
return nil, err
}
client := http.Client{}
req, err := http.NewRequest("PATCH", url, bytes.NewReader(uJson))
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}
func DeleteRequest(url string, header http.Header) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("DELETE", url, nil)
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err, resp
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err, resp
}
return data, nil, resp
}