You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

  1. package dbModel
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "github.com/pkg/errors"
  5. "main/controller/servers"
  6. "main/init/qmsql"
  7. "main/model/modelInterface"
  8. )
  9. type Api struct {
  10. gorm.Model
  11. Path string `json:"path"`
  12. Description string `json:"description"`
  13. Group string `json:"group"`
  14. }
  15. func (a *Api) CreateApi() (err error) {
  16. findOne := qmsql.DEFAULTDB.Where("path = ?", a.Path).Find(&Menu{}).Error
  17. if findOne == nil {
  18. return errors.New("存在相同api")
  19. } else {
  20. err = qmsql.DEFAULTDB.Create(a).Error
  21. }
  22. return err
  23. }
  24. func (a *Api) DeleteApi() (err error) {
  25. err = qmsql.DEFAULTDB.Delete(a).Error
  26. err = qmsql.DEFAULTDB.Where("api_id = ?", a.ID).Unscoped().Delete(&ApiAuthority{}).Error
  27. return err
  28. }
  29. func (a *Api) UpdataApi() (err error) {
  30. err = qmsql.DEFAULTDB.Save(a).Error
  31. return err
  32. }
  33. func (a *Api) GetApiById(id float64) (err error, api Api) {
  34. err = qmsql.DEFAULTDB.Where("id = ?", id).First(&api).Error
  35. return
  36. }
  37. // 获取所有api信息
  38. func (a *Api) GetAllApis() (err error, apis []Api) {
  39. err = qmsql.DEFAULTDB.Find(&apis).Error
  40. return
  41. }
  42. // 分页获取数据 需要分页实现这个接口即可
  43. func (a *Api) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
  44. // 封装分页方法 调用即可 传入 当前的结构体和分页信息
  45. err, db, total := servers.PagingServer(a, info)
  46. if err != nil {
  47. return
  48. } else {
  49. var apiList []Api
  50. err = db.Order("group", true).Where("path LIKE ?", "%"+a.Path+"%").Find(&apiList).Error
  51. return err, apiList, total
  52. }
  53. }