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) r.Delete("/:resource_id", resourceDelete) 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{} err := x.Parse(opts) if err != nil { return nil, err } data := make([]*M.Resource, 0, 10) query := cfg.DB() if opts.AppID != nil { query = query.Where("app_id LIKE ?", opts.AppID) } err = query.Find(&data).Error return data, err } func resourcePost(x *rest.X) (any, error) { opts := &M.ResourcePost{} 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 return data, err }