12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 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)
- GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error)
- GetIpCountListId(ctx context.Context,hostId int64) ([]int, 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).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Find(&res).Error; err != nil {
- return nil, err
- }
- return res, nil
- }
- func (r *allowAndDenyIpRepository) GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error) {
- var count int64
- if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Where("ip = ?", ip).Count(&count).Error; err != nil {
- return 0, err
- }
- return count, nil
- }
- func (r *allowAndDenyIpRepository) GetIpCountListId(ctx context.Context,hostId int64) ([]int, error) {
- var res []int
- if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Pluck("id", &res).Error; err != nil {
- return nil, err
- }
- return res, nil
- }
|