public bool Exists(PackageNotificationType type, string value)
        {
            var cleaned = _stringHelper.Clean(value);

            return(!string.IsNullOrWhiteSpace(cleaned) &&
                   _databaseContext.PackageNotificationTargets.Any(pnt => pnt.Type == type && pnt.Value.Equals(cleaned)));
        }
        public IPackageNotificationTarget Create(PackageNotificationType type, string value)
        {
            var cleaned = _stringHelper.Clean(value);

            if (string.IsNullOrWhiteSpace(cleaned))
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (Exists(type, value))
            {
                throw new ArgumentException("Package Notification Target already exists");
            }

            var entity = new PackageNotificationTargetEntity
            {
                Type  = type,
                Value = cleaned
            };

            _databaseContext.PackageNotificationTargets.Add(entity);
            _databaseContext.SaveChanges();

            return(new PackageNotificationTarget
            {
                Id = entity.Id,
                Type = entity.Type,
                Value = entity.Value
            });
        }
        public bool Exists(string package, PackageNotificationType type, string value)
        {
            var cleaned        = _stringHelper.Clean(value);
            var cleanedPackage = _stringHelper.Clean(package);

            if (string.IsNullOrWhiteSpace(cleaned) || string.IsNullOrWhiteSpace(cleanedPackage))
            {
                return(false);
            }

            return(_databaseContext.PackageNotifications.Any(pn =>
                                                             pn.Package.Name.Equals(cleanedPackage) &&
                                                             pn.Target.Type == type &&
                                                             pn.Target.Value.Equals(cleaned)));
        }
        private void Create(IPackage package, PackageNotificationType type, string value)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }

            if (!Enum.IsDefined(typeof(PackageNotificationType), type))
            {
                throw new InvalidEnumArgumentException(nameof(type), (int)type, typeof(PackageNotificationType));
            }

            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(value));
            }

            if (Exists(package.Name, type, value))
            {
                return;
            }

            var corePackageNotificationTarget = _packageNotificationTargetService.Exists(type, value)
                ? _packageNotificationTargetService.Get(type, value)
                : _packageNotificationTargetService.Create(type, value);

            if (corePackageNotificationTarget == null)
            {
                throw new NullReferenceException("A package must exist to create a package notification.");
            }

            var notification = new PackageNotificationEntity
            {
                PackageId = package.Id,
                PackageNotificationTargetId = corePackageNotificationTarget.Id
            };

            _databaseContext.PackageNotifications.Add(notification);
            _databaseContext.SaveChanges();
        }
        public IPackageNotificationTarget Get(PackageNotificationType type, string value)
        {
            var cleaned = _stringHelper.Clean(value);

            if (string.IsNullOrWhiteSpace(cleaned))
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (!Exists(type, value))
            {
                return(null);
            }

            var entity = _databaseContext.PackageNotificationTargets.Single(pnt => pnt.Type == type && pnt.Value.Equals(cleaned));

            return(new PackageNotificationTarget
            {
                Id = entity.Id,
                Type = entity.Type,
                Value = entity.Value
            });
        }