More tests and DELETE account
This commit is contained in:
48
src/handlers/delete.go
Normal file
48
src/handlers/delete.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// AccountDel godoc
|
||||
// @Summary Delete an account
|
||||
// @Description Requires Authorization-header with role "admin".
|
||||
// @Description Example: Authorization: bearer xxx
|
||||
// @Description Where "xxx" is a valid JWT token
|
||||
// @ID account-del
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Account ID"
|
||||
// @Success 204 {string} string ""
|
||||
// @Failure 400 {object} []ResJSONError
|
||||
// @Failure 401 {object} []ResJSONError
|
||||
// @Failure 403 {object} []ResJSONError
|
||||
// @Failure 404 {object} []ResJSONError
|
||||
// @Failure 415 {object} []ResJSONError
|
||||
// @Failure 500 {object} []ResJSONError
|
||||
// @Router /account/:id [delete]
|
||||
func (h Handlers) AccountDel(c *fiber.Ctx) error {
|
||||
accountID := c.Params("accountID")
|
||||
|
||||
_, uuidErr := uuid.Parse(accountID)
|
||||
if uuidErr != nil {
|
||||
return c.Status(400).JSON([]ResJSONError{{Error: "Invalid uuid format"}})
|
||||
}
|
||||
|
||||
authErr := h.RequireAdminRole(c)
|
||||
if authErr != nil {
|
||||
return c.Status(403).JSON([]ResJSONError{{Error: authErr.Error()}})
|
||||
}
|
||||
|
||||
err := h.Db.AccountDel(accountID)
|
||||
if err != nil {
|
||||
if err.Error() == "No account found for given accountID" {
|
||||
return c.Status(404).JSON([]ResJSONError{{Error: err.Error()}})
|
||||
} else {
|
||||
return c.Status(500).JSON([]ResJSONError{{Error: "Database error when trying to remove account"}})
|
||||
}
|
||||
}
|
||||
|
||||
return c.Status(204).Send(nil)
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
// @Router /account/{id} [get]
|
||||
func (h Handlers) AccountGet(c *fiber.Ctx) error {
|
||||
accountID := c.Params("accountID")
|
||||
// logContext := log.WithFields(log.Fields{"accountID": accountID})
|
||||
|
||||
authErr := h.RequireAdminRoleOrAccountID(c, accountID)
|
||||
if authErr != nil {
|
||||
@@ -30,7 +29,11 @@ func (h Handlers) AccountGet(c *fiber.Ctx) error {
|
||||
|
||||
account, accountErr := h.Db.AccountGet(accountID, "", "")
|
||||
if accountErr != nil {
|
||||
return c.Status(500).JSON([]ResJSONError{{Error: accountErr.Error()}})
|
||||
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)
|
||||
|
||||
@@ -73,7 +73,9 @@ func (h Handlers) parseHeaders(c *fiber.Ctx) map[string]string {
|
||||
lineParts := strings.Split(line, ": ")
|
||||
|
||||
if len(lineParts) == 1 {
|
||||
h.Log.Debug("Ignoring header line", "line", line)
|
||||
if len(line) != 0 {
|
||||
h.Log.Debug("Ignoring header line", "line", line)
|
||||
}
|
||||
} else {
|
||||
headersMap[lineParts[0]] = lineParts[1]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user