@ -10,53 +10,55 @@ import (
// @title CreateApi
// @title CreateApi
// @description create base apis, 新增基础api
// @description create base apis, 新增基础api
// @auth (2020/04/05 20:22)
// @auth (2020/04/05 20:22)
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @param api model.SysApi
// @return error
// @return error
func CreateApi ( a model . SysApi ) ( err error ) {
findOne := global . GVA_DB . Where ( "path = ? AND method = ?" , a . Path , a . Method ) . Find ( & model . SysApi { } ) . Error
func CreateApi ( api model . SysApi ) ( err error ) {
findOne := global . GVA_DB . Where ( "path = ? AND method = ?" , api . Path , api . Method ) . Find ( & model . SysApi { } ) . Error
if findOne == nil {
if findOne == nil {
return errors . New ( "存在相同api" )
return errors . New ( "存在相同api" )
} else {
} else {
err = global . GVA_DB . Create ( & a ) . Error
err = global . GVA_DB . Create ( & api ) . Error
}
}
return err
return err
}
}
// @title DeleteApi
// @title DeleteApi
// @description delete base apis, 删除基础api
// @description delete a base api, 删除基础api
// @param api model.SysApi
// @auth (2020/04/05 20:22)
// @auth (2020/04/05 20:22)
// @return error
// @return error
func DeleteApi ( a model . SysApi ) ( err error ) {
err = global . GVA_DB . Delete ( a ) . Error
ClearCasbin ( 1 , a . Path , a . Method )
func DeleteApi ( api model . SysApi ) ( err error ) {
err = global . GVA_DB . Delete ( api ) . Error
ClearCasbin ( 1 , api . Path , api . Method )
return err
return err
}
}
// @title GetInfoList
// @title GetInfoList
// @description get apis by pagination, 分页获取数据
// @description get apis by pagination, 分页获取数据
// @auth (2020/04/05 20:22)
// @auth (2020/04/05 20:22)
// @param info PageInfo
// @param api model.SysApi
// @param info request.PageInfo
// @param order string
// @param desc bool
// @return err error
// @return err error
// @return list interface{}
// @return list interface{}
// @return total int
// @return total int
func GetAPIInfoList ( a model . SysApi , info request . PageInfo , Order string , D esc bool ) ( err error , list interface { } , total int ) {
func GetAPIInfoList ( api model . SysApi , info request . PageInfo , order string , d esc bool ) ( err error , list interface { } , total int ) {
limit := info . PageSize
limit := info . PageSize
offset := info . PageSize * ( info . Page - 1 )
offset := info . PageSize * ( info . Page - 1 )
db := global . GVA_DB
db := global . GVA_DB
var apiList [ ] model . SysApi
var apiList [ ] model . SysApi
if a . Path != "" {
db = db . Where ( "path LIKE ?" , "%" + a . Path + "%" )
if api . Path != "" {
db = db . Where ( "path LIKE ?" , "%" + api . Path + "%" )
}
}
if a . Description != "" {
db = db . Where ( "description LIKE ?" , "%" + a . Description + "%" )
if api . Description != "" {
db = db . Where ( "description LIKE ?" , "%" + api . Description + "%" )
}
}
if a . Method != "" {
db = db . Where ( "method = ?" , a . Method )
if api . Method != "" {
db = db . Where ( "method = ?" , api . Method )
}
}
err = db . Find ( & apiList ) . Count ( & total ) . Error
err = db . Find ( & apiList ) . Count ( & total ) . Error
@ -65,12 +67,12 @@ func GetAPIInfoList(a model.SysApi, info request.PageInfo, Order string, Desc bo
return err , apiList , total
return err , apiList , total
} else {
} else {
db = db . Limit ( limit ) . Offset ( offset )
db = db . Limit ( limit ) . Offset ( offset )
if O rder != "" {
if o rder != "" {
var OrderStr string
var OrderStr string
if D esc {
OrderStr = O rder + " desc"
if d esc {
OrderStr = o rder + " desc"
} else {
} else {
OrderStr = O rder
OrderStr = o rder
}
}
err = db . Order ( OrderStr , true ) . Find ( & apiList ) . Error
err = db . Order ( OrderStr , true ) . Find ( & apiList ) . Error
} else {
} else {
@ -93,6 +95,7 @@ func GetAllApis() (err error, apis []model.SysApi) {
// @title GetApiById
// @title GetApiById
// @description 根据id获取api
// @description 根据id获取api
// @auth (2020/04/05 20:22)
// @auth (2020/04/05 20:22)
// @param api model.SysApi
// @param id float64
// @param id float64
// @return error
// @return error
func GetApiById ( id float64 ) ( err error , api model . SysApi ) {
func GetApiById ( id float64 ) ( err error , api model . SysApi ) {
@ -103,14 +106,15 @@ func GetApiById(id float64) (err error, api model.SysApi) {
// @title UpdateApi
// @title UpdateApi
// @description update a base api, update api
// @description update a base api, update api
// @auth (2020/04/05 20:22)
// @auth (2020/04/05 20:22)
// @param api model.SysApi
// @return error
// @return error
func UpdateApi ( a model . SysApi ) ( err error ) {
func UpdateApi ( api model . SysApi ) ( err error ) {
var oldA model . SysApi
var oldA model . SysApi
err = global . GVA_DB . Where ( "id = ?" , a . ID ) . First ( & oldA ) . Error
err = global . GVA_DB . Where ( "id = ?" , api . ID ) . First ( & oldA ) . Error
if oldA . Path != a . Path || oldA . Method != a . Method {
flag := global . GVA_DB . Where ( "path = ? AND method = ?" , a . Path , a . Method ) . Find ( & model . SysApi { } ) . RecordNotFound ( )
if oldA . Path != api . Path || oldA . Method != api . Method {
flag := global . GVA_DB . Where ( "path = ? AND method = ?" , api . Path , api . Method ) . Find ( & model . SysApi { } ) . RecordNotFound ( )
if ! flag {
if ! flag {
return errors . New ( "存在相同api路径" )
return errors . New ( "存在相同api路径" )
}
}
@ -118,11 +122,11 @@ func UpdateApi(a model.SysApi) (err error) {
if err != nil {
if err != nil {
return err
return err
} else {
} else {
err = UpdateCasbinApi ( oldA . Path , a . Path , oldA . Method , a . Method )
err = UpdateCasbinApi ( oldA . Path , api . Path , oldA . Method , api . Method )
if err != nil {
if err != nil {
return err
return err
} else {
} else {
err = global . GVA_DB . Save ( a ) . Error
err = global . GVA_DB . Save ( api ) . Error
}
}
}
}
return err
return err