service.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package proxy
  2. import (
  3. "github.com/astaxie/beego/logs"
  4. "github.com/astaxie/beego/orm"
  5. "nginx-ui/server/models"
  6. )
  7. type Service struct {
  8. }
  9. func NewProxyService() *Service {
  10. return &Service{}
  11. }
  12. func (u *Service) GetAll(req *GetProxyListReq) []*models.ProxyEntity {
  13. o := orm.NewOrm()
  14. qs := o.QueryTable(&models.ProxyEntity{})
  15. if req.Enable {
  16. qs = qs.Filter("Enable", true)
  17. }
  18. if req.Ref != "" {
  19. qs = qs.Filter("Ref", req.Ref)
  20. }
  21. if req.Uid > 0 {
  22. qs = qs.Filter("UserId", req.Uid)
  23. }
  24. list := make([]*models.ProxyEntity, 0)
  25. _, err := qs.All(&list)
  26. if err != nil {
  27. logs.Error("query proxies fail: %v", err)
  28. return make([]*models.ProxyEntity, 0)
  29. }
  30. return list
  31. }
  32. func (u *Service) Update(req *models.ProxyEntity) (*models.ProxyEntity, error) {
  33. o := orm.NewOrm()
  34. _, err := models.InsertOrUpdate(o, req)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return req, nil
  39. }
  40. func (u *Service) Delete(req *models.ProxyEntity) (*models.ProxyEntity, error) {
  41. o := orm.NewOrm()
  42. _, err := o.Delete(&models.ProxyEntity{Id: req.Id})
  43. if err != nil {
  44. return nil, err
  45. }
  46. return req, nil
  47. }