auth-api/src/handlers/get.go

80 lines
2.4 KiB
Go
Raw Normal View History

2020-12-29 13:46:58 +01:00
package handlers
import (
"github.com/gofiber/fiber/v2"
2021-06-24 00:46:28 +02:00
"github.com/google/uuid"
2020-12-29 13:46:58 +01:00
)
2021-06-22 22:52:48 +02:00
// AccountGet godoc
2021-06-22 23:49:11 +02:00
// @Summary Get account by id
// @Description Requires Authorization-header with either role "admin" or with a matching account id.
// @Description Example: Authorization: bearer xxx
// @Description Where "xxx" is a valid JWT token
2021-06-22 22:52:48 +02:00
// @ID get-account-by-id
// @Accept json
// @Produce json
// @Param id path string true "Account ID"
// @Success 200 {object} db.Account
2021-06-22 23:49:11 +02:00
// @Failure 401 {object} []ResJSONError
// @Failure 403 {object} []ResJSONError
// @Failure 415 {object} []ResJSONError
// @Failure 500 {object} []ResJSONError
2021-06-22 22:52:48 +02:00
// @Router /account/{id} [get]
2021-01-02 11:56:24 +01:00
func (h Handlers) AccountGet(c *fiber.Ctx) error {
accountID := c.Params("accountID")
2021-06-24 00:46:28 +02:00
_, uuidErr := uuid.Parse(accountID)
if uuidErr != nil {
return c.Status(400).JSON([]ResJSONError{{Error: "Invalid uuid format"}})
}
authErr := h.RequireAdminRoleOrAccountID(c, accountID)
if authErr != nil {
return c.Status(403).JSON([]ResJSONError{{Error: authErr.Error()}})
}
account, accountErr := h.Db.AccountGet(accountID, "", "")
if accountErr != nil {
2021-06-24 00:42:54 +02:00
if accountErr.Error() == "no rows in result set" {
return c.Status(404).JSON([]ResJSONError{{Error: "No account found for given accountID"}})
} else {
return c.Status(500).JSON([]ResJSONError{{Error: accountErr.Error()}})
}
}
return c.JSON(account)
2020-12-29 13:46:58 +01:00
}
// AccountGet godoc
// @Summary Get accounts
// @Description Requires Authorization-header with role "admin".
// @Description Example: Authorization: bearer xxx
// @Description Where "xxx" is a valid JWT token
// @Accept json
// @Produce json
// @Success 200 {object} []db.Account
// @Failure 401 {object} []ResJSONError
// @Failure 403 {object} []ResJSONError
// @Failure 415 {object} []ResJSONError
// @Failure 500 {object} []ResJSONError
// @Router /accounts [get]
func (h Handlers) AccountsGet(c *fiber.Ctx) error {
accountID := c.Params("accountID")
authErr := h.RequireAdminRole(c)
if authErr != nil {
return c.Status(403).JSON([]ResJSONError{{Error: authErr.Error()}})
}
account, accountErr := h.Db.AccountGet(accountID, "", "")
if accountErr != nil {
if accountErr.Error() == "no rows in result set" {
return c.Status(404).JSON([]ResJSONError{{Error: "No account found for given accountID"}})
} else {
return c.Status(500).JSON([]ResJSONError{{Error: accountErr.Error()}})
}
}
return c.JSON(account)
}