60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"cafe/types"
|
|
"cafe/user"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// @Schemes
|
|
// @Summary get a user
|
|
// @Description gets a user
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param username path string true "Username"
|
|
// @Success 200 {object} user.User
|
|
// @Failure 500 {object} errorResponse
|
|
// @Router /users/{username} [get]
|
|
func (a *Api) getUser(c *gin.Context) {
|
|
username := c.Param("username")
|
|
u, err := user.GetUserOrCreate(username)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, errorResponse{err.Error()})
|
|
} else {
|
|
c.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.User true "updated User"
|
|
// @Success 200 {object} user.User
|
|
// @Failure 400 {object} errorResponse
|
|
// @Failure 404 "Not Found" errorResponse
|
|
// @Failure 500 {object} errorResponse
|
|
// @Router /users [put]
|
|
func (a *Api) updateUser(c *gin.Context) {
|
|
var newUser user.User
|
|
err := c.ShouldBindJSON(&newUser)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, errorResponse{types.MissingInformation.String()})
|
|
return
|
|
}
|
|
oldUser, err := user.DoesUserExist(newUser.Username)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, errorResponse{err.Error()})
|
|
return
|
|
}
|
|
err = user.UpdateUser(&oldUser, &newUser)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, errorResponse{err.Error()})
|
|
} else {
|
|
c.JSON(http.StatusOK, newUser)
|
|
}
|
|
}
|