API增删改查忘词嗯

This commit is contained in:
pixel
2019-09-28 16:34:43 +08:00
parent 324d45f1de
commit 3237b8402b
11 changed files with 267 additions and 40 deletions
+50 -6
View File
@@ -57,7 +57,7 @@ func DeleteApi(c *gin.Context) {
type AuthAndPathIn struct {
AuthorityId string `json:"authorityId"`
Apis []dbModel.Api `json:"apis"`
Apis []dbModel.Api `json:"apis"`
}
// @Tags Api
@@ -67,11 +67,11 @@ type AuthAndPathIn struct {
// @Produce application/json
// @Param data body api.AuthAndPathIn true "创建api和角色关系"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/setAuthAndPath [post]
func SetAuthAndPath(c *gin.Context) {
// @Router /api/setAuthAndApi [post]
func SetAuthAndApi(c *gin.Context) {
var authAndPathIn AuthAndPathIn
_ = c.BindJSON(&authAndPathIn)
err := new(dbModel.ApiAuthority).SetAuthAndPath(authAndPathIn.AuthorityId, authAndPathIn.Apis)
err := new(dbModel.ApiAuthority).SetAuthAndApi(authAndPathIn.AuthorityId, authAndPathIn.Apis)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("添加失败:%v", err), gin.H{})
} else {
@@ -80,11 +80,11 @@ func SetAuthAndPath(c *gin.Context) {
}
// @Tags Api
// @Summary 分页获取角色列表
// @Summary 分页获取API列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body modelInterface.PageInfo true "分页获取用户列表"
// @Param data body modelInterface.PageInfo true "分页获取API列表"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiList [post]
func GetApiList(c *gin.Context) {
@@ -103,3 +103,47 @@ func GetApiList(c *gin.Context) {
}
}
// @Tags Api
// @Summary 根据id获取api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body modelInterface.PageInfo true "分页获取用户列表"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiById [post]
func GetApiById(c *gin.Context) {
var idInfo GetById
_ = c.BindJSON(&idInfo)
err, api := new(dbModel.Api).GetApiById(idInfo.Id)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "获取数据成功", gin.H{
"api": api,
})
}
}
// @Tags Api
// @Summary 创建基础api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateApiParams true "创建api"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/updataApi [post]
func UpdataApi(c *gin.Context) {
var api dbModel.Api
_ = c.BindJSON(&api)
err := api.UpdataApi()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("修改数据失败,%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "修改数据成功", gin.H{})
}
}
+1 -1
View File
@@ -11,6 +11,6 @@ func PagingServer(paging modelInterface.Paging, info modelInterface.PageInfo) (e
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
err = qmsql.DEFAULTDB.Model(paging).Count(&total).Error
db = qmsql.DEFAULTDB.Limit(limit).Offset(offset)
db = qmsql.DEFAULTDB.Limit(limit).Offset(offset).Order("id desc")
return err, db, total
}
+8 -3
View File
@@ -16,7 +16,7 @@ type Api struct {
func (a *Api) CreateApi() (err error) {
findOne := qmsql.DEFAULTDB.Where("path = ?", a.Path).Find(&Menu{}).Error
if findOne != nil {
if findOne == nil {
return errors.New("存在相同api")
} else {
err = qmsql.DEFAULTDB.Create(a).Error
@@ -29,11 +29,16 @@ func (a *Api) DeleteApi() (err error) {
return err
}
func (a *Api) EditApi() (err error) {
err = qmsql.DEFAULTDB.Update(a).Error
func (a *Api) UpdataApi() (err error) {
err = qmsql.DEFAULTDB.Save(a).Error
return err
}
func (a *Api) GetApiById(id float64)(err error,api Api){
err = qmsql.DEFAULTDB.Where("id = ?",id).First(&api).Error
return
}
// 分页获取数据 需要分页实现这个接口即可
func (a *Api) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
// 封装分页方法 调用即可 传入 当前的结构体和分页信息
+3 -4
View File
@@ -1,7 +1,6 @@
package dbModel
import (
"fmt"
"github.com/jinzhu/gorm"
"main/init/qmsql"
)
@@ -10,15 +9,15 @@ type ApiAuthority struct {
gorm.Model
AuthorityId string
Authority Authority `gorm:"ForeignKey:AuthorityId;AssociationForeignKey:AuthorityId"` //其实没有关联的必要
ApiId string
ApiId uint
Api Api
}
//创建角色api关联关系
func (a *ApiAuthority) SetAuthAndPath(authId string, apis []Api) (err error) {
func (a *ApiAuthority) SetAuthAndApi(authId string, apis []Api) (err error) {
err = qmsql.DEFAULTDB.Where("authority_id = ?", authId).Delete(&ApiAuthority{}).Error
for _, v := range apis {
err = qmsql.DEFAULTDB.Create(&ApiAuthority{AuthorityId: authId, ApiId: fmt.Sprintf("%v", v.ID)}).Error
err = qmsql.DEFAULTDB.Create(&ApiAuthority{AuthorityId: authId, ApiId: v.ID}).Error
if err != nil {
return err
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
)
type Authority struct {
gorm.Model `json:"-"`
gorm.Model
AuthorityId string `json:"authorityId" gorm:"not null;unique"`
AuthorityName string `json:"authorityName"`
}
+6 -4
View File
@@ -9,9 +9,11 @@ import (
func InitApiRouter(Router *gin.Engine) {
ApiRouter := Router.Group("api").Use(middleware.JWTAuth())
{
ApiRouter.POST("createApi", api.CreateApi)
ApiRouter.POST("deleteApi", api.DeleteApi)
ApiRouter.POST("setAuthAndPath",api.SetAuthAndPath)
ApiRouter.POST("getApiList",api.GetApiList)
ApiRouter.POST("createApi", api.CreateApi) //创建Api
ApiRouter.POST("deleteApi", api.DeleteApi) //删除Api
ApiRouter.POST("setAuthAndPath",api.SetAuthAndApi) // 设置api和角色关系
ApiRouter.POST("getApiList",api.GetApiList) //获取Api列表
ApiRouter.POST("getApiById",api.GetApiById) //获取单条Api消息
ApiRouter.POST("updataApi",api.UpdataApi) //更新api
}
}