54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// @Schemes
|
|
// @Summary get a user
|
|
// @Description gets a user
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param username path string true "Username"
|
|
// @Success 200 {object} User
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /users/{username} [get]
|
|
func (c *Controller) GetUser(ctx echo.Context) error {
|
|
username := ctx.Param("username")
|
|
u, err := c.getUserOrCreate(username)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
return ctx.JSON(http.StatusOK, u)
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary update a user
|
|
// @Description updates a user with provided information
|
|
// @Tags users
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param user body User true "updated User"
|
|
// @Success 200 {object} User
|
|
// @Failure 400 "Bad Request"
|
|
// @Failure 404 "Not Found"
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /users [put]
|
|
func (c *Controller) UpdateUser(ctx echo.Context) error {
|
|
var newUser User
|
|
err := ctx.Bind(&newUser)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
oldUser, err := c.doesUserExist(newUser.Username)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err.Error())
|
|
}
|
|
err = c.updateUser(&oldUser, &newUser)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return ctx.JSON(http.StatusOK, newUser)
|
|
}
|