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/app.go

131 lines
2.9 KiB
Go

package app
import (
"oa/cfg"
"oa/libs/auth"
M "oa/models"
"github.com/veypi/OneBD/rest"
"gorm.io/gorm"
)
func useApp(r rest.Router) {
r.Delete("/:app_id", auth.Check("app", "app_id", auth.DoDelete), appDelete)
r.Get("/:app_id", auth.Check("app", "app_id", auth.DoRead), appGet)
r.Get("/", auth.Check("app", "", auth.DoRead), appList)
r.Patch("/:app_id", auth.Check("app", "app_id", auth.DoUpdate), appPatch)
r.Post("/", auth.Check("app", "", auth.DoCreate), appPost)
}
func appDelete(x *rest.X) (any, error) {
opts := &M.AppDelete{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err
}
func appGet(x *rest.X) (any, error) {
opts := &M.AppGet{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err
}
func appList(x *rest.X) (any, error) {
opts := &M.AppList{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := make([]*struct {
M.App
UserStatus string `json:"user_status"`
}, 0, 10)
query := cfg.DB().Table("apps").Select("apps.*,app_users.status user_status")
uid := x.Request.Context().Value("uid").(string)
if opts.Name != nil {
query = query.Joins("LEFT JOIN app_users ON app_users.app_id = apps.id AND app_users.user_id = ? AND apps.name LIKE ?", uid, opts.Name)
} else {
query = query.Joins("LEFT JOIN app_users ON app_users.app_id = apps.id AND app_users.user_id = ?", uid)
}
// logv.AssertError(cfg.DB().Table("accesses a").
// Select("a.name, a.t_id, a.level").
// Joins("INNER JOIN user_roles ur ON ur.role_id = a.role_id AND ur.user_id = ?", refresh.UID).
// Scan(&acList).Error)
err = query.Scan(&data).Error
return data, err
}
func appPatch(x *rest.X) (any, error) {
opts := &M.AppPatch{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil {
return nil, err
}
optsMap := make(map[string]interface{})
if opts.Name != nil {
optsMap["name"] = opts.Name
}
if opts.Icon != nil {
optsMap["icon"] = opts.Icon
}
if opts.Des != nil {
optsMap["des"] = opts.Des
}
if opts.Typ != nil {
optsMap["typ"] = opts.Typ
}
if opts.InitRoleID != nil {
optsMap["init_role_id"] = opts.InitRoleID
}
err = cfg.DB().Model(data).Updates(optsMap).Error
return data, err
}
func appPost(x *rest.X) (any, error) {
opts := &M.AppPost{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.App{}
data.Name = opts.Name
data.Icon = opts.Icon
data.Typ = opts.Typ
if opts.Des != nil {
data.Des = *opts.Des
}
err = cfg.DB().Transaction(func(tx *gorm.DB) error {
err := tx.Create(data).Error
if err != nil {
return err
}
au := &M.AppUser{
AppID: data.ID,
UserID: x.Request.Context().Value("uid").(string),
Status: M.AUSTATUS_OK,
}
return tx.Create(au).Error
})
return data, err
}