144 lines
4.1 KiB
Go
144 lines
4.1 KiB
Go
package api
|
|
|
|
import (
|
|
"SciencesServer/app/api/admin/controller/role"
|
|
"SciencesServer/app/basic/api"
|
|
"SciencesServer/app/session"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Role struct{}
|
|
|
|
func (*Role) Index(c *gin.Context) {
|
|
form := &struct {
|
|
Name string `json:"name" form:"name"`
|
|
Status int `json:"status" form:"status"`
|
|
api.PageForm
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
data, err := role.NewInstance()(api.GetSession()(c).(*session.Admin)).Index(form.Name, form.Page, form.PageSize)
|
|
api.APIResponse(err, data)(c)
|
|
}
|
|
|
|
func (*Role) Select(c *gin.Context) {
|
|
data, err := role.NewInstance()(api.GetSession()(c).(*session.Admin)).Select()
|
|
api.APIResponse(err, data)(c)
|
|
}
|
|
|
|
func (*Role) Add(c *gin.Context) {
|
|
form := &struct {
|
|
Name string `json:"name" form:"name" binding:"required"`
|
|
Remark string `json:"remark" form:"remark"`
|
|
Sort int `json:"sort" form:"sort"`
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
err := role.NewInstance()(api.GetSession()(c).(*session.Admin)).Form(0, form.Name, form.Remark, form.Sort)
|
|
api.APIResponse(err)(c)
|
|
}
|
|
|
|
func (*Role) Edit(c *gin.Context) {
|
|
form := &struct {
|
|
api.IDStringForm
|
|
Name string `json:"name" form:"name" binding:"required"`
|
|
Remark string `json:"remark" form:"remark"`
|
|
Sort int `json:"sort" form:"sort"`
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
err := role.NewInstance()(api.GetSession()(c).(*session.Admin)).Form(form.Convert(), form.Name, form.Remark, form.Sort)
|
|
api.APIResponse(err)(c)
|
|
}
|
|
|
|
func (*Role) Status(c *gin.Context) {
|
|
form := &struct {
|
|
api.IDStringForm
|
|
Status int `json:"status" form:"status" binding:"required"`
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
err := role.NewInstance()(api.GetSession()(c).(*session.Admin)).Status(form.Convert(), form.Status)
|
|
api.APIResponse(err)(c)
|
|
}
|
|
|
|
func (*Role) Delete(c *gin.Context) {
|
|
form := new(api.IDStringForm)
|
|
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
err := role.NewInstance()(api.GetSession()(c).(*session.Admin)).Delete(form.Convert())
|
|
api.APIResponse(err)(c)
|
|
}
|
|
|
|
func (*Role) Menu(c *gin.Context) {
|
|
form := &struct {
|
|
RoleID string `json:"role_id" form:"role_id" binding:"required"`
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
data, err := role.NewMenu()(api.GetSession()(c).(*session.Admin)).Index((&api.IDStringForm{ID: form.RoleID}).Convert())
|
|
api.APIResponse(err, data)(c)
|
|
}
|
|
|
|
func (*Role) MenuBind(c *gin.Context) {
|
|
form := &struct {
|
|
RoleID string `json:"role_id" form:"role_id" binding:"required"`
|
|
MenuIDs []string `json:"menu_ids" form:"menu_ids" binding:"required"`
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
menuIDs := make([]uint64, 0)
|
|
|
|
for _, v := range form.MenuIDs {
|
|
menuIDs = append(menuIDs, (&api.IDStringForm{ID: v}).Convert())
|
|
}
|
|
err := role.NewMenu()(api.GetSession()(c).(*session.Admin)).Bind((&api.IDStringForm{ID: form.RoleID}).Convert(), menuIDs)
|
|
api.APIResponse(err)(c)
|
|
}
|
|
|
|
func (*Role) Auth(c *gin.Context) {
|
|
form := &struct {
|
|
RoleID string `json:"role_id" form:"role_id" binding:"required"`
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
data, err := role.NewAuth()(api.GetSession()(c).(*session.Admin)).Instance((&api.IDStringForm{ID: form.RoleID}).Convert())
|
|
api.APIResponse(err, data)(c)
|
|
}
|
|
|
|
func (*Role) AuthBind(c *gin.Context) {
|
|
form := &struct {
|
|
RoleID string `json:"role_id" form:"role_id" binding:"required"`
|
|
AuthIDs []string `json:"auth_ids" form:"auth_ids" binding:"required"`
|
|
}{}
|
|
if err := api.Bind(form)(c); err != nil {
|
|
api.APIFailure(err.(error))(c)
|
|
return
|
|
}
|
|
authIDs := make([]uint64, 0)
|
|
|
|
for _, v := range form.AuthIDs {
|
|
authIDs = append(authIDs, (&api.IDStringForm{ID: v}).Convert())
|
|
}
|
|
err := role.NewAuth()(api.GetSession()(c).(*session.Admin)).Bind((&api.IDStringForm{ID: form.RoleID}).Convert(), authIDs)
|
|
api.APIResponse(err)(c)
|
|
}
|