allowanddenyip.go 2.9 KB

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