✨ Attachment pool basis
This commit is contained in:
@ -95,7 +95,7 @@ func createAttachment(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreateAttachments", file.Size); err != nil {
|
||||
if err = gap.H.EnsureGrantedPerm(c, "CreateAttachments", file.Size); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,12 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
|
||||
api := app.Group(baseURL).Name("API")
|
||||
{
|
||||
api.Get("/pools", listPost)
|
||||
api.Get("/pools/:id", getPool)
|
||||
api.Post("/pools", createPool)
|
||||
api.Put("/pools/:id", updatePool)
|
||||
api.Delete("/pools/:id", deletePool)
|
||||
|
||||
api.Get("/attachments", listAttachment)
|
||||
api.Get("/attachments/:id/meta", getAttachmentMeta)
|
||||
api.Get("/attachments/:id", openAttachment)
|
||||
|
113
pkg/internal/server/api/pools_api.go
Normal file
113
pkg/internal/server/api/pools_api.go
Normal file
@ -0,0 +1,113 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func listPost(c *fiber.Ctx) error {
|
||||
pools, err := services.ListAttachmentPool()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
return c.JSON(pools)
|
||||
}
|
||||
|
||||
func getPool(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("id")
|
||||
pool, err := services.GetAttachmentPool(uint(id))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
return c.JSON(pool)
|
||||
}
|
||||
|
||||
func createPool(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreateAttachmentPools", true); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description"`
|
||||
Config models.AttachmentPoolConfig `json:"config"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pool := models.AttachmentPool{
|
||||
Alias: data.Alias,
|
||||
Name: data.Name,
|
||||
Description: data.Description,
|
||||
Config: datatypes.NewJSONType(data.Config),
|
||||
AccountID: &user.ID,
|
||||
}
|
||||
|
||||
if pool, err := services.NewAttachmentPool(pool); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(pool)
|
||||
}
|
||||
}
|
||||
|
||||
func updatePool(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description"`
|
||||
Config models.AttachmentPoolConfig `json:"config"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, _ := c.ParamsInt("id")
|
||||
pool, err := services.GetAttachmentPoolWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
pool.Alias = data.Alias
|
||||
pool.Name = data.Name
|
||||
pool.Description = data.Description
|
||||
pool.Config = datatypes.NewJSONType(data.Config)
|
||||
|
||||
if pool, err := services.UpdateAttachmentPool(pool); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(pool)
|
||||
}
|
||||
}
|
||||
|
||||
func deletePool(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
id, _ := c.ParamsInt("id")
|
||||
pool, err := services.GetAttachmentPoolWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if pool, err := services.DeleteAttachmentPool(pool); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(pool)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user