From 7f4122360ff1c70b220ab0cad6f7e4606988ff7d Mon Sep 17 00:00:00 2001 From: kandrusyak Date: Fri, 23 Jun 2023 20:48:45 +0300 Subject: [PATCH] add user service methods --- main.go | 4 ++++ pkg/api/apiCall.go | 50 ++++++++++++++++++++++++++++++++++++++- services/users/users.go | 52 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 07978e4..7d3867f 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,10 @@ func main() { // Users e.POST("/auth/login", users.Login) e.GET("/users/me", users.GetCurrentUser) + e.GET("/users", users.GetUsers) + e.GET("/users/:id", users.GetUsers) + e.DELETE("/users/:id", users.DeleteUser) + e.PATCH("/users/:id", users.UpdateUser) e.Logger.Fatal(e.Start(":" + config.Env.Server.Port)) } diff --git a/pkg/api/apiCall.go b/pkg/api/apiCall.go index f753503..61ba0f8 100644 --- a/pkg/api/apiCall.go +++ b/pkg/api/apiCall.go @@ -34,9 +34,57 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error) { return data, nil } -func GetRequest(url string, header http.Header) ([]byte, error) { +func GetRequest(url string, header http.Header, args ...string) ([]byte, error) { client := http.Client{} req, err := http.NewRequest("GET", url, nil) + 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 + } + + data, err := io.ReadAll(resp.Body) + + if err != nil { + return nil, err + } + + return data, nil +} + +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) { + 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) diff --git a/services/users/users.go b/services/users/users.go index 3cb11d6..e8d4999 100644 --- a/services/users/users.go +++ b/services/users/users.go @@ -55,15 +55,57 @@ func Login(c echo.Context) (err error) { } func GetCurrentUser(c echo.Context) error { - u := new(model.User) - data, err := api.GetRequest(config.Env.MSHosts.UsersHost+"/me", c.Request().Header) - err = json.Unmarshal(data, &u) - if err != nil { return c.NoContent(http.StatusBadGateway) } - return c.JSON(http.StatusOK, u) + return c.JSONBlob(http.StatusOK, data) +} + +func GetUsers(c echo.Context) error { + id := c.Param("id") + + data, err := api.GetRequest( + config.Env.MSHosts.UsersHost+"/"+id, + c.Request().Header, c.Request().URL.RawQuery) + + if err != nil { + return c.NoContent(http.StatusBadGateway) + } + + return c.JSONBlob(http.StatusOK, data) +} + +func DeleteUser(c echo.Context) error { + id := c.Param("id") + + data, err := api.DeleteRequest( + config.Env.MSHosts.UsersHost+"/"+id, + c.Request().Header) + + if err != nil { + return c.NoContent(http.StatusBadGateway) + } + + return c.JSONBlob(http.StatusOK, data) +} + +func UpdateUser(c echo.Context) (err error) { + id := c.Param("id") + + // Bind + u := new(model.User) + if err = c.Bind(u); err != nil { + return + } + + data, err := api.PatchRequest(config.Env.MSHosts.UsersHost+"/"+id, u, c.Request().Header) + + if err != nil { + return c.NoContent(http.StatusBadGateway) + } + + return c.JSONBlob(http.StatusOK, data) }