inbound.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package service
  2. import (
  3. "gorm.io/gorm"
  4. "x-ui/database"
  5. "x-ui/database/model"
  6. )
  7. type InboundService struct {
  8. }
  9. func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
  10. db := database.GetDB()
  11. var inbounds []*model.Inbound
  12. err := db.Model(model.Inbound{}).Where("user_id = ?", userId).Find(&inbounds).Error
  13. if err != nil && err != gorm.ErrRecordNotFound {
  14. return nil, err
  15. }
  16. return inbounds, nil
  17. }
  18. func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
  19. db := database.GetDB()
  20. var inbounds []*model.Inbound
  21. err := db.Model(model.Inbound{}).Find(&inbounds).Error
  22. if err != nil && err != gorm.ErrRecordNotFound {
  23. return nil, err
  24. }
  25. return inbounds, nil
  26. }
  27. func (s *InboundService) AddInbound(inbound *model.Inbound) error {
  28. db := database.GetDB()
  29. return db.Save(inbound).Error
  30. }
  31. func (s *InboundService) DelInbound(id int) error {
  32. db := database.GetDB()
  33. return db.Delete(model.Inbound{}, id).Error
  34. }
  35. func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
  36. db := database.GetDB()
  37. inbound := &model.Inbound{}
  38. err := db.Model(model.Inbound{}).First(inbound, id).Error
  39. if err != nil {
  40. return nil, err
  41. }
  42. return inbound, nil
  43. }
  44. func (s *InboundService) UpdateInbound(inbound *model.Inbound) error {
  45. oldInbound, err := s.GetInbound(inbound.Id)
  46. if err != nil {
  47. return err
  48. }
  49. oldInbound.Remark = inbound.Remark
  50. oldInbound.Enable = inbound.Enable
  51. oldInbound.ExpiryTime = inbound.ExpiryTime
  52. oldInbound.Listen = inbound.Listen
  53. oldInbound.Port = inbound.Port
  54. oldInbound.Protocol = inbound.Protocol
  55. oldInbound.Settings = inbound.Settings
  56. oldInbound.StreamSettings = inbound.StreamSettings
  57. oldInbound.Sniffing = inbound.Sniffing
  58. db := database.GetDB()
  59. return db.Save(oldInbound).Error
  60. }