Init
This commit is contained in:
commit
f90fdc0598
99 changed files with 15260 additions and 0 deletions
60
api/routesUser.go
Normal file
60
api/routesUser.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue