70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package storages
|
|
|
|
import (
|
|
"backend/database/models/qvyun_v2/public/model"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/samber/lo"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// @provider
|
|
type Controller struct {
|
|
svc *Service
|
|
log *log.Entry `inject:"false"`
|
|
}
|
|
|
|
func (c *Controller) Prepare() error {
|
|
c.log = log.WithField("module", "storages.Controller")
|
|
return nil
|
|
}
|
|
|
|
// @Router /api/v1/storages [get]
|
|
func (ctl *Controller) List(ctx fiber.Ctx) ([]Storage, error) {
|
|
storages, err := ctl.svc.GetStorages(ctx.Context())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return lo.Map(storages, func(item model.Storages, _ int) Storage {
|
|
return Storage{Storages: item}
|
|
}), nil
|
|
}
|
|
|
|
// @Router /api/v1/storages/:id [get]
|
|
// @Bind id path
|
|
func (ctl *Controller) Show(ctx fiber.Ctx, id int64) (*Storage, error) {
|
|
storage, err := ctl.svc.GetStorageByID(ctx.Context(), id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Storage{Storages: *storage}, nil
|
|
}
|
|
|
|
// Delete
|
|
// @Router /api/v1/storages/:id [delete]
|
|
// @Bind id path
|
|
func (ctl *Controller) Delete(ctx fiber.Ctx, id int64) error {
|
|
return ctl.svc.DeleteStorageByID(ctx.Context(), id)
|
|
}
|
|
|
|
// Create
|
|
// @Router /api/v1/storages [post]
|
|
// @Bind req body
|
|
func (ctl *Controller) Create(ctx fiber.Ctx, req *CreateStorageReq) error {
|
|
m := &model.Storages{
|
|
Name: req.Name,
|
|
Type: req.Type,
|
|
Config: req.Config,
|
|
}
|
|
return ctl.svc.Create(ctx.Context(), m)
|
|
}
|
|
|
|
// SetDefault
|
|
// @Router /api/v1/storages/:id/default [put]
|
|
// @Bind id path
|
|
func (ctl *Controller) SetDefault(ctx fiber.Ctx, id int64) error {
|
|
return ctl.svc.SetDefault(ctx.Context(), id)
|
|
}
|