Files
vt/routers/mandant.go
2025-08-21 19:35:01 +02:00

65 lines
1.5 KiB
Go

package routers
import (
"git.kocoder.xyz/kocoded/vt/model"
"git.kocoder.xyz/kocoded/vt/query"
"git.kocoder.xyz/kocoded/vt/utils"
"github.com/gofiber/fiber/v2"
)
type mandantRouter struct {
utils.Application
currentMandant uint
}
func RegisterMandantRouter(group fiber.Router, appCtx utils.Application) {
router := &mandantRouter{currentMandant: 1, Application: appCtx}
group.Get("/current", router.getCurrentMandant)
group.Put("/current", router.setCurrentMandant)
group.Get("/all", router.getAllMandant)
}
func (r *mandantRouter) getCurrentMandant(c *fiber.Ctx) error {
m := query.Mandant
currentMandant, err := m.Where(m.ID.Eq(r.currentMandant)).First()
if err != nil {
r.Logger.Warn("Current mandant not found.", "error", err)
return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(currentMandant)
}
func (r *mandantRouter) getAllMandant(c *fiber.Ctx) error {
m := query.Mandant
mandanten, err := m.Find()
if err != nil {
r.Logger.Warn("Current mandant not found.", "error", err)
return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(mandanten)
}
func (r *mandantRouter) setCurrentMandant(c *fiber.Ctx) error {
m := query.Mandant
mandant := &model.Mandant{}
if err := c.BodyParser(mandant); err != nil {
return err
}
r.currentMandant = mandant.ID
currentMandant, err := m.Where(m.ID.Eq(r.currentMandant)).First()
if err != nil {
r.Logger.Warn("Current mandant not found.", "error", err)
return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(currentMandant)
}