|
|
|
package access
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/veypi/OneBD/rest"
|
|
|
|
"oa/cfg"
|
|
|
|
M "oa/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func useAccess(r rest.Router) {
|
|
|
|
r.Get("/", accessList)
|
|
|
|
r.Post("/", accessPost)
|
|
|
|
r.Get("/:access_id", accessGet)
|
|
|
|
r.Patch("/:access_id", accessPatch)
|
|
|
|
r.Delete("/:access_id", accessDelete)
|
|
|
|
}
|
|
|
|
func accessList(x *rest.X) (any, error) {
|
|
|
|
opts := &M.AccessList{}
|
|
|
|
err := x.Parse(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := make([]*M.Access, 0, 10)
|
|
|
|
|
|
|
|
query := cfg.DB()
|
|
|
|
if opts.CreatedAt != nil {
|
|
|
|
query = query.Where("created_at > ?", opts.CreatedAt)
|
|
|
|
}
|
|
|
|
if opts.UpdatedAt != nil {
|
|
|
|
query = query.Where("updated_at > ?", opts.UpdatedAt)
|
|
|
|
}
|
|
|
|
query = query.Where("app_id LIKE ?", opts.AppID)
|
|
|
|
if opts.UserID != nil {
|
|
|
|
query = query.Where("user_id LIKE ?", opts.UserID)
|
|
|
|
}
|
|
|
|
if opts.RoleID != nil {
|
|
|
|
query = query.Where("role_id LIKE ?", opts.RoleID)
|
|
|
|
}
|
|
|
|
if opts.Name != nil {
|
|
|
|
query = query.Where("name LIKE ?", opts.Name)
|
|
|
|
}
|
|
|
|
err = query.Find(&data).Error
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
func accessPost(x *rest.X) (any, error) {
|
|
|
|
opts := &M.AccessPost{}
|
|
|
|
err := x.Parse(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := &M.Access{}
|
|
|
|
|
|
|
|
data.AppID = opts.AppID
|
|
|
|
data.UserID = opts.UserID
|
|
|
|
data.RoleID = opts.RoleID
|
|
|
|
data.Name = opts.Name
|
|
|
|
data.TID = opts.TID
|
|
|
|
data.Level = opts.Level
|
|
|
|
err = cfg.DB().Create(data).Error
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
func accessGet(x *rest.X) (any, error) {
|
|
|
|
opts := &M.AccessGet{}
|
|
|
|
err := x.Parse(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := &M.Access{}
|
|
|
|
|
|
|
|
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
func accessPatch(x *rest.X) (any, error) {
|
|
|
|
opts := &M.AccessPatch{}
|
|
|
|
err := x.Parse(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := &M.Access{}
|
|
|
|
|
|
|
|
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
optsMap := make(map[string]interface{})
|
|
|
|
err = cfg.DB().Model(data).Updates(optsMap).Error
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
func accessDelete(x *rest.X) (any, error) {
|
|
|
|
opts := &M.AccessDelete{}
|
|
|
|
err := x.Parse(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := &M.Access{}
|
|
|
|
|
|
|
|
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|