215 lines
5.4 KiB
Go
215 lines
5.4 KiB
Go
package groups
|
|
|
|
import (
|
|
"system-trace/core/database"
|
|
"system-trace/core/parsers"
|
|
"system-trace/core/types"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// MARK: GetGroups godoc
|
|
// @Summary Get groups (with pagination)
|
|
// @Description Returns array of groups and count
|
|
// @Tags groups
|
|
// @Produce json
|
|
// @Param count query int true "Count of rows" minimum(10) maximum(100)
|
|
// @Param offset query int true "Rows to skip" minumum(0)
|
|
// @Success 200 {object} types.JSONPagination
|
|
// @Router /groups [get]
|
|
func GetGroupsHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
s, err := parsers.GetSearch(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
p, err := parsers.GetPagination(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
ob, err := parsers.GetOrderBy(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
groups, cursor, err := database.FindGroups(s, p, ob)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(types.JSONPagination{
|
|
Data: groups,
|
|
Cursor: cursor,
|
|
})
|
|
}
|
|
|
|
// MARK: GetGroupByID godoc
|
|
// @Summary Get group by ID
|
|
// @Description Returns group instance
|
|
// @Tags groups
|
|
// @Produce json
|
|
// @Param id path int true "Group ID" minimum(1)
|
|
// @Success 200 {object} database.Group
|
|
// @Router /groups/:id [get]
|
|
func GetGroupByIDHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
group, err := database.FindGroupByID(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(group)
|
|
}
|
|
|
|
// MARK: CreateGroup godoc
|
|
// @Summary Create group
|
|
// @Description Create group with specified data
|
|
// @Tags groups
|
|
// @Produce json
|
|
// @Param request body database.Group true "Request body"
|
|
// @Success 200
|
|
// @Router /groups [post]
|
|
func CreateGroupHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
g := new(database.Group)
|
|
if err := c.BodyParser(g); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
// TODO replace with locals
|
|
g.IssuerID = 2 //c.Locals("userId").(int32)
|
|
if err := database.InsertGroup(g); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
// Insert new permissions
|
|
for _, v := range g.Permissions {
|
|
v.GroupID = g.ID
|
|
}
|
|
if err := database.InsertGroupPermissions(g.Permissions); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: UpdateGroup godoc
|
|
// @Summary Update group
|
|
// @Description Update group with specified data
|
|
// @Tags groups
|
|
// @Produce json
|
|
// @Param request body database.Group true "Request body"
|
|
// @Success 200
|
|
// @Router /groups [patch]
|
|
func UpdateGroupHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
g := new(database.Group)
|
|
if err := c.BodyParser(g); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.UpdateGroup(g, []string{"name"}); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
// Remove exist permissions
|
|
if err := database.DeleteGroupPermissions(g.ID); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
// Insert new permissions
|
|
for _, v := range g.Permissions {
|
|
v.GroupID = g.ID
|
|
}
|
|
if err := database.InsertGroupPermissions(g.Permissions); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: DeleteGroup godoc
|
|
// @Summary Delete group
|
|
// @Description Delete group by ID
|
|
// @Tags groups
|
|
// @Produce json
|
|
// @Param id path int true "Group ID" minimum(1)
|
|
// @Success 200
|
|
// @Router /groups/:id [delete]
|
|
func DeleteGroupHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err = database.DeleteGroup(id); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: DeleteGroups godoc
|
|
// @Summary Delete many groups
|
|
// @Description Delete groups by ID
|
|
// @Tags groups
|
|
// @Produce json
|
|
// @Param request body types.ManyIDs true "Request body"
|
|
// @Success 200
|
|
// @Router /groups [delete]
|
|
func DeleteGroupsHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
arr := new(types.ManyIDs)
|
|
if err := c.BodyParser(arr); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.DeleteManyGroups(arr.Array); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|