Bulk commit: November work
This commit is contained in:
171
routers/project.go
Normal file
171
routers/project.go
Normal file
@ -0,0 +1,171 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.kocoder.xyz/kocoded/vt/model"
|
||||
"git.kocoder.xyz/kocoded/vt/query"
|
||||
"git.kocoder.xyz/kocoded/vt/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type projectRouter struct {
|
||||
utils.Application
|
||||
currentMandant uint
|
||||
}
|
||||
|
||||
func RegisterProjectRouter(group fiber.Router, appCtx utils.Application) {
|
||||
router := &projectRouter{currentMandant: 1, Application: appCtx}
|
||||
|
||||
r := group.Use(utils.IsAuthenticated(appCtx))
|
||||
r.Get("/all", router.getAllProjects)
|
||||
r.Post("/new", router.createNewProject)
|
||||
r.Get("/:id<int>", router.getProject)
|
||||
r.Post("/:id<int>/edit", router.editProject)
|
||||
r.Delete("/:id<int>/delete", router.deleteProject)
|
||||
}
|
||||
|
||||
func (r *projectRouter) getAllProjects(c *fiber.Ctx) error {
|
||||
p := query.Projekt
|
||||
pph := c.Get("X-PER-PAGE")
|
||||
ofh := c.Get("X-OFFSET")
|
||||
|
||||
params := struct {
|
||||
Id string `params:"id"`
|
||||
Desc bool `params:"desc"`
|
||||
}{}
|
||||
|
||||
if err := c.QueryParser(¶ms); err != nil {
|
||||
r.Logger.Warn("Param Parser Error: ", "err", err)
|
||||
}
|
||||
|
||||
var pp, of int
|
||||
pp, err := strconv.Atoi(pph)
|
||||
if err != nil {
|
||||
r.Logger.Warn("Per Page header not found.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
of, err = strconv.Atoi(ofh)
|
||||
if err != nil {
|
||||
r.Logger.Warn("Offset header not found.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
query := p.Where().Limit(pp).Offset(of)
|
||||
|
||||
if params.Id != "" {
|
||||
f, ok := p.GetFieldByName(params.Id)
|
||||
if ok {
|
||||
if params.Desc {
|
||||
query = query.Order(f.Desc())
|
||||
} else {
|
||||
query = query.Order(f.Asc())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
query = query.Order(p.ID.Asc())
|
||||
}
|
||||
|
||||
projects, err := query.Order(p.Name.Asc()).Find()
|
||||
if err != nil {
|
||||
r.Logger.Warn("Current mandant not found.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
cnt, err := p.Count()
|
||||
if err != nil {
|
||||
r.Logger.Warn("Current mandant not found.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
res := &PaginatedProjects{Data: projects, Meta: struct {
|
||||
TotalProjectsCount int64 `json:"totalProjectsCount"`
|
||||
}{TotalProjectsCount: cnt}}
|
||||
|
||||
return c.JSON(res)
|
||||
}
|
||||
|
||||
type PaginatedProjects struct {
|
||||
Data []*model.Projekt `json:"data"`
|
||||
Meta struct {
|
||||
TotalProjectsCount int64 `json:"totalProjectsCount"`
|
||||
} `json:"meta"`
|
||||
}
|
||||
|
||||
func (r *projectRouter) getProject(c *fiber.Ctx) error {
|
||||
ids := c.Params("id")
|
||||
id, err := strconv.ParseUint(ids, 10, 32)
|
||||
if err != nil {
|
||||
r.Logger.Warn("Id is not an int.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
p := query.Projekt
|
||||
|
||||
currentProject, err := p.Where(p.ID.Eq(uint(id))).First()
|
||||
if err != nil {
|
||||
r.Logger.Warn("Current mandant not found.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return c.JSON(currentProject)
|
||||
}
|
||||
|
||||
func (r *projectRouter) createNewProject(c *fiber.Ctx) error {
|
||||
p := query.Projekt
|
||||
project := &model.Projekt{}
|
||||
|
||||
if err := c.BodyParser(project); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := p.Create(project)
|
||||
if err != nil {
|
||||
r.Logger.Warn("Couldn't create Projejct.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
utils.MessageBus.SendMBObject(int(project.MandantID), utils.MessageBusObject{Entity: []string{"projects", "list"}})
|
||||
utils.MessageBus.SendMBObject(int(project.MandantID), utils.MessageBusObject{Entity: []string{"projects", "get"}, Id: int(project.ID)})
|
||||
|
||||
return c.JSON(project)
|
||||
}
|
||||
|
||||
func (r *projectRouter) editProject(c *fiber.Ctx) error {
|
||||
p := query.Projekt
|
||||
project := &model.Projekt{}
|
||||
|
||||
if err := c.BodyParser(project); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := p.Where(p.ID.Eq(project.ID)).Updates(project)
|
||||
if err != nil {
|
||||
r.Logger.Warn("Couldn't create Projejct.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
utils.MessageBus.SendMBObject(int(project.MandantID), utils.MessageBusObject{Entity: []string{"projects", "list"}})
|
||||
utils.MessageBus.SendMBObject(int(project.MandantID), utils.MessageBusObject{Entity: []string{"projects", "get"}, Id: int(project.ID)})
|
||||
|
||||
return c.JSON(res)
|
||||
}
|
||||
|
||||
func (r *projectRouter) deleteProject(c *fiber.Ctx) error {
|
||||
ids := c.Params("id")
|
||||
id, err := strconv.ParseUint(ids, 10, 32)
|
||||
if err != nil {
|
||||
r.Logger.Warn("Id is not an int.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
p := query.Projekt
|
||||
|
||||
res, err := p.Where(p.ID.Eq(uint(id))).Delete()
|
||||
if err != nil {
|
||||
r.Logger.Warn("Couldn't create Projejct.", "error", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return c.JSON(res)
|
||||
}
|
||||
Reference in New Issue
Block a user