allowanddenyip.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  15. func NewAllowAndDenyIpRepository(
  16. repository *Repository,
  17. ) AllowAndDenyIpRepository {
  18. return &allowAndDenyIpRepository{
  19. Repository: repository,
  20. }
  21. }
  22. type allowAndDenyIpRepository struct {
  23. *Repository
  24. }
  25. func (r *allowAndDenyIpRepository) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) {
  26. var res model.AllowAndDenyIp
  27. if err := r.DB(ctx).Where("id = ?", id).First(&res).Error; err != nil {
  28. return nil, err
  29. }
  30. return &res, nil
  31. }
  32. func (r *allowAndDenyIpRepository) AddAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error {
  33. if err := r.db.WithContext(ctx).Create(&req).Error; err != nil {
  34. return fmt.Errorf("create error: %v", err)
  35. }
  36. return nil
  37. }
  38. func (r *allowAndDenyIpRepository) EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error {
  39. allowAndDenyIp := map[string]interface{}{
  40. "allow_or_deny": req.AllowOrDeny,
  41. }
  42. if err := r.db.WithContext(ctx).Where("id = ?", req.Id).Updates(&req).Updates(allowAndDenyIp).Error; err != nil {
  43. return fmt.Errorf("update error: %v", err)
  44. }
  45. return nil
  46. }
  47. func (r *allowAndDenyIpRepository) DeleteAllowAndDenyIps(ctx context.Context, id int64) error {
  48. if err := r.db.WithContext(ctx).Where("id = ?", id).Delete(&model.AllowAndDenyIp{}).Error; err != nil {
  49. return fmt.Errorf("delete error: %v", err)
  50. }
  51. return nil
  52. }
  53. func (r *allowAndDenyIpRepository) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) {
  54. var res []*model.AllowAndDenyIp
  55. if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Find(&res).Error; err != nil {
  56. return nil, err
  57. }
  58. return res, nil
  59. }
  60. func (r *allowAndDenyIpRepository) GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error) {
  61. var count int64
  62. if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Where("ip = ?", ip).Count(&count).Error; err != nil {
  63. return 0, err
  64. }
  65. return count, nil
  66. }