You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oa/api/app/resource.go

92 lines
1.8 KiB
Go

package app
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
M "oa/models"
)
func useResource(r rest.Router) {
r.Post("/", resourcePost)
r.Delete("/", resourceDelete)
r.Get("/", resourceList)
2 months ago
r.Delete("/:resource_id", resourceDelete)
3 weeks ago
r.Get("/:resource_id", resourceGet)
r.Patch("/:resource_id", resourcePatch)
}
func resourceGet(x *rest.X) (any, error) {
opts := &M.ResourceGet{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Resource{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err
}
func resourcePatch(x *rest.X) (any, error) {
opts := &M.ResourcePatch{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Resource{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil {
return nil, err
}
optsMap := make(map[string]interface{})
if opts.Des != nil {
optsMap["des"] = opts.Des
}
err = cfg.DB().Model(data).Updates(optsMap).Error
return data, err
}
func resourceDelete(x *rest.X) (any, error) {
opts := &M.ResourceDelete{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Resource{}
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err
}
func resourceList(x *rest.X) (any, error) {
opts := &M.ResourceList{}
3 weeks ago
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := make([]*M.Resource, 0, 10)
3 weeks ago
query := cfg.DB()
if opts.AppID != nil {
query = query.Where("app_id LIKE ?", opts.AppID)
}
err = query.Find(&data).Error
3 weeks ago
return data, err
}
func resourcePost(x *rest.X) (any, error) {
opts := &M.ResourcePost{}
3 weeks ago
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Resource{}
data.AppID = opts.AppID
data.Name = opts.Name
data.Des = opts.Des
err = cfg.DB().Create(data).Error
3 weeks ago
return data, err
}