allowanddenyip.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package waf
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  7. )
  8. type AllowAndDenyIpRepository interface {
  9. GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error)
  10. AddAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error
  11. EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error
  12. DeleteAllowAndDenyIps(ctx context.Context, id int64) error
  13. GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error)
  14. GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error)
  15. GetIpCountListId(ctx context.Context,hostId int64) ([]int, error)
  16. }
  17. func NewAllowAndDenyIpRepository(
  18. repository *repository.Repository,
  19. ) AllowAndDenyIpRepository {
  20. return &allowAndDenyIpRepository{
  21. Repository: repository,
  22. }
  23. }
  24. type allowAndDenyIpRepository struct {
  25. *repository.Repository
  26. }
  27. func (r *allowAndDenyIpRepository) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) {
  28. var res model.AllowAndDenyIp
  29. if err := r.DB(ctx).Where("id = ?", id).First(&res).Error; err != nil {
  30. return nil, err
  31. }
  32. return &res, nil
  33. }
  34. func (r *allowAndDenyIpRepository) AddAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error {
  35. if err := r.Db.WithContext(ctx).Create(&req).Error; err != nil {
  36. return fmt.Errorf("create error: %v", err)
  37. }
  38. return nil
  39. }
  40. func (r *allowAndDenyIpRepository) EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error {
  41. allowAndDenyIp := map[string]interface{}{
  42. "allow_or_deny": req.AllowOrDeny,
  43. }
  44. if err := r.Db.WithContext(ctx).Where("id = ?", req.Id).Updates(&req).Updates(allowAndDenyIp).Error; err != nil {
  45. return fmt.Errorf("update error: %v", err)
  46. }
  47. return nil
  48. }
  49. func (r *allowAndDenyIpRepository) DeleteAllowAndDenyIps(ctx context.Context, id int64) error {
  50. if err := r.Db.WithContext(ctx).Where("id = ?", id).Delete(&model.AllowAndDenyIp{}).Error; err != nil {
  51. return fmt.Errorf("delete error: %v", err)
  52. }
  53. return nil
  54. }
  55. func (r *allowAndDenyIpRepository) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) {
  56. var res []*model.AllowAndDenyIp
  57. if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Find(&res).Error; err != nil {
  58. return nil, err
  59. }
  60. return res, nil
  61. }
  62. func (r *allowAndDenyIpRepository) GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error) {
  63. var count int64
  64. if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Where("ip = ?", ip).Count(&count).Error; err != nil {
  65. return 0, err
  66. }
  67. return count, nil
  68. }
  69. func (r *allowAndDenyIpRepository) GetIpCountListId(ctx context.Context,hostId int64) ([]int, error) {
  70. var res []int
  71. if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Pluck("id", &res).Error; err != nil {
  72. return nil, err
  73. }
  74. return res, nil
  75. }