From 4c61eb82a005731f6827d7c5e6377b920e044740 Mon Sep 17 00:00:00 2001 From: kandrusyak Date: Fri, 28 Jul 2023 00:58:54 +0300 Subject: [PATCH] add avatar endpoint --- config/config.go | 10 ++++- handler/users.go | 43 ++++++++++++++++++++ main.go | 1 + model/user.go | 1 + pkg/api/apiCall.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 pkg/api/apiCall.go diff --git a/config/config.go b/config/config.go index 484b507..9bc953d 100644 --- a/config/config.go +++ b/config/config.go @@ -9,8 +9,13 @@ type ServerConfig struct { ConnectionString string } +type MSHosts struct { + StorageHost string +} + type Config struct { - Server ServerConfig + 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"), + }, } } diff --git a/handler/users.go b/handler/users.go index 14f61ea..5de4d04 100644 --- a/handler/users.go +++ b/handler/users.go @@ -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, + }) +} diff --git a/main.go b/main.go index 996adc4..7d26dce 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/model/user.go b/model/user.go index 232a04f..c74451c 100644 --- a/model/user.go +++ b/model/user.go @@ -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"` } ) diff --git a/pkg/api/apiCall.go b/pkg/api/apiCall.go new file mode 100644 index 0000000..ac715cb --- /dev/null +++ b/pkg/api/apiCall.go @@ -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 +}