package repository import ( "context" "fmt" "github.com/go-nunu/nunu-layout-advanced/internal/model" ) type AllowAndDenyIpRepository interface { GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) AddAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error DeleteAllowAndDenyIps(ctx context.Context, id int64) error GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) } func NewAllowAndDenyIpRepository( repository *Repository, ) AllowAndDenyIpRepository { return &allowAndDenyIpRepository{ Repository: repository, } } type allowAndDenyIpRepository struct { *Repository } func (r *allowAndDenyIpRepository) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) { var res model.AllowAndDenyIp if err := r.DB(ctx).Where("id = ?", id).First(&res).Error; err != nil { return nil, err } return &res, nil } func (r *allowAndDenyIpRepository) AddAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error { if err := r.db.WithContext(ctx).Create(&req).Error; err != nil { return fmt.Errorf("create error: %v", err) } return nil } func (r *allowAndDenyIpRepository) EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error { allowAndDenyIp := map[string]interface{}{ "allow_or_deny": req.AllowOrDeny, } if err := r.db.WithContext(ctx).Where("id = ?", req.Id).Updates(&req).Updates(allowAndDenyIp).Error; err != nil { return fmt.Errorf("update error: %v", err) } return nil } func (r *allowAndDenyIpRepository) DeleteAllowAndDenyIps(ctx context.Context, id int64) error { if err := r.db.WithContext(ctx).Where("id = ?", id).Delete(&model.AllowAndDenyIp{}).Error; err != nil { return fmt.Errorf("delete error: %v", err) } return nil } func (r *allowAndDenyIpRepository) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) { var res []*model.AllowAndDenyIp if err := r.DB(ctx).Where("host_id = ?", hostId).Find(&res).Error; err != nil { return nil, err } return res, nil }