This commit is contained in:
2023-07-05 22:07:10 +02:00
parent 0ba69b5496
commit b2e65a9df0
15 changed files with 645 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package handler
import (
"log"
"github.com/gofiber/fiber/v2"
)
var defaultErrMessage = "could not process request"
func RenderError(c *fiber.Ctx, err error, message string) error {
if err != nil {
log.Printf("renderError: %s\n", err)
}
return c.Render("error", fiber.Map{
"message": message,
}, "")
}
func JSONError(c *fiber.Ctx, status int, err error, message string) error {
if err != nil {
log.Printf("JSONError: %s\n", err)
}
return c.Status(status).SendString("error: " + message)
}

View File

@@ -0,0 +1,37 @@
package handler
import (
"fmt"
"gitea.urkob.com/urko/prosody-password/internal/services/prosody"
"github.com/gofiber/fiber/v2"
)
func NewProsodyHandler(prosodyService *prosody.Prosody) ProsodyHandler {
return ProsodyHandler{
prosodyService: prosodyService,
}
}
type ProsodyHandler struct {
prosodyService *prosody.Prosody
}
type changePasswordReq struct {
CurrentPassword string `json:"current_password"`
NewPassword string `json:"new_password"`
User string `json:"user"`
}
func (handler ProsodyHandler) Post(c *fiber.Ctx) error {
req := changePasswordReq{}
if err := c.BodyParser(&req); err != nil {
return RenderError(c, fmt.Errorf("id is empty"), defaultErrMessage)
}
if err := handler.prosodyService.ChangePassword(req.User, req.CurrentPassword, req.NewPassword); err != nil {
return RenderError(c, fmt.Errorf("ChangePassword: %w", err), defaultErrMessage)
}
return c.Render("success", fiber.Map{}, "")
}