add user service methods
This commit is contained in:
4
main.go
4
main.go
@@ -34,6 +34,10 @@ func main() {
|
|||||||
// Users
|
// Users
|
||||||
e.POST("/auth/login", users.Login)
|
e.POST("/auth/login", users.Login)
|
||||||
e.GET("/users/me", users.GetCurrentUser)
|
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))
|
e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,57 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error) {
|
|||||||
return data, nil
|
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{}
|
client := http.Client{}
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
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 = header
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|||||||
@@ -55,15 +55,57 @@ func Login(c echo.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetCurrentUser(c echo.Context) error {
|
func GetCurrentUser(c echo.Context) error {
|
||||||
u := new(model.User)
|
|
||||||
|
|
||||||
data, err := api.GetRequest(config.Env.MSHosts.UsersHost+"/me", c.Request().Header)
|
data, err := api.GetRequest(config.Env.MSHosts.UsersHost+"/me", c.Request().Header)
|
||||||
|
|
||||||
err = json.Unmarshal(data, &u)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.NoContent(http.StatusBadGateway)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user