allowanddenyip.go 2.0 KB

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