Restructure project
This commit is contained in:
parent
e44e7caa11
commit
16b2f17301
46 changed files with 1744 additions and 1265 deletions
54
internal/controller/routesUser.go
Normal file
54
internal/controller/routesUser.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue