package resource

import (
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vigo"
)

type createOpts struct {
	AppID string `json:"app_id" src:"path@app_id" desc:"应用ID"`
	Name  string `json:"name" src:"json" desc:"资源名称"`
	Des   string `json:"des" src:"json" desc:"资源描述"`
}

var _ = Router.Post("/", "创建资源", createResource)

func createResource(x *vigo.X, opts *createOpts) (*models.Resource, error) {
	// 创建资源对象
	resource := &models.Resource{
		AppID: opts.AppID,
		Name:  opts.Name,
		Des:   opts.Des,
	}

	// 保存到数据库
	if err := cfg.DB().Create(resource).Error; err != nil {
		return nil, vigo.NewError("failed to create resource").WithCode(500)
	}

	return resource, nil
}
