105 lines
1.9 KiB
Go
105 lines
1.9 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 firmaRouter struct {
|
|
utils.Application
|
|
}
|
|
|
|
func RegisterFirmaRouter(group fiber.Router, appCtx utils.Application) {
|
|
router := &firmaRouter{Application: appCtx}
|
|
|
|
group.Post("/new", router.createFirma)
|
|
group.Get("/all", router.getAllFirmen)
|
|
group.Get("/:id<int>", router.getFirma)
|
|
group.Put("/:id<int>", router.updateFirma)
|
|
group.Delete("/:id<int>", router.deleteFirma)
|
|
}
|
|
|
|
func (r *firmaRouter) createFirma(c *fiber.Ctx) error {
|
|
firma := new(model.Firma)
|
|
|
|
if err := c.BodyParser(firma); err != nil {
|
|
return err
|
|
}
|
|
|
|
ap := query.Firma
|
|
|
|
if err := ap.Omit(ap.UpdatedAt, ap.DeletedAt).Create(firma); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.SendString("Hello")
|
|
}
|
|
|
|
func (r *firmaRouter) getAllFirmen(c *fiber.Ctx) error {
|
|
aps, err := query.Firma.Find()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = c.JSON(aps)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *firmaRouter) getFirma(c *fiber.Ctx) error {
|
|
id, err := c.ParamsInt("id")
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ap, err := query.Firma.Where(query.Firma.ID.Eq(uint(id))).First()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(ap)
|
|
}
|
|
|
|
func (r *firmaRouter) updateFirma(c *fiber.Ctx) error {
|
|
firma := new(model.Firma)
|
|
id, err := c.ParamsInt("id")
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = c.BodyParser(firma); err != nil {
|
|
return err
|
|
}
|
|
|
|
ap := query.Firma
|
|
|
|
res, err := ap.Where(ap.ID.Eq(uint(id))).Omit(ap.ID, ap.CreatedAt, ap.UpdatedAt, ap.DeletedAt).Updates(firma)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(res)
|
|
}
|
|
|
|
func (r *firmaRouter) deleteFirma(c *fiber.Ctx) error {
|
|
id, err := c.ParamsInt("id")
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := query.Firma.Where(query.Firma.ID.Value(uint(id))).Delete()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(res)
|
|
}
|