package resource

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

type getIDReq struct {
	ID string `src:"path@resource_id" desc:"资源ID"`
}

var _ = Router.Get("/{resource_id}", "获取资源详情", getResource)

func getResource(x *vigo.X, req *getIDReq) (*models.Resource, error) {
	// 查询数据库
	resource := &models.Resource{}
	err := cfg.DB().Where("id = ?", req.ID).First(resource).Error
	if err != nil {
		return nil, vigo.NewError("resource not found").WithCode(404)
	}

	return resource, nil
}
