示例#1
0
        public void Execute()
        {
            Console.Write("Rule name: ");
            string       ruleName = Console.ReadLine();
            RulesManager manager  = new RulesManager();
            Rule         rule     = manager.Find(ruleName);

            if (rule != null)
            {
                Console.Write("Address: ");
                string address = Console.ReadLine();
                if (rule.AddAddress(address))
                {
                    if (manager.Update(rule))
                    {
                        Console.WriteLine("Address added successfully.");
                        return;
                    }
                    Console.WriteLine("Rules file is not loaded.");
                    return;
                }
                Console.WriteLine("Address is already contained in the rule.");
                return;
            }
            Console.WriteLine("Rule not found.");
            return;
        }
示例#2
0
        /// <summary>
        /// Returns the list of rules
        /// </summary>
        /// <returns>A list containing the rules</returns>
        public List <Rule> Values()
        {
            List <Rule> rules = new List <Rule>();

            if (FileLoaded)
            {
                IEnumerable <XElement> xRules = xml.Root.Elements();
                foreach (XElement xRule in xRules)
                {
                    Rule rule = new Rule
                    {
                        Name = xRule.Attribute(xNameName).Value,
                        LocationFolderName    = xRule.Attribute(xLocationName).Value,
                        DestinationFolderName = xRule.Attribute(xDestinationName).Value
                    };
                    IEnumerable <XElement> xAddresses = xRule.Element(xAddressesName).Elements(xAddressName);
                    foreach (XElement xAddress in xAddresses)
                    {
                        rule.AddAddress(xAddress.Value);
                    }
                    rules.Add(rule);
                }
            }
            return(rules);
        }
示例#3
0
        public void Execute()
        {
            // setup rule
            Rule rule = new Rule();

            Console.Write("Rule name: ");
            rule.Name = Console.ReadLine();
            Console.Write("From folder (Location): ");
            rule.LocationFolderName = Console.ReadLine();
            Console.Write("To folder (Destination): ");
            rule.DestinationFolderName = Console.ReadLine();
            // setup addresses
            Console.Write("Addresses count: ");
            if (Int32.TryParse(Console.ReadLine(), out int addressesCount))
            {
                // get addresses
                for (int i = 1; i <= addressesCount; i++)
                {
                    Console.Write($"Address {i}: ");
                    rule.AddAddress(Console.ReadLine());
                }
                // save rule
                RulesManager manager = new RulesManager();
                if (manager.Add(rule))
                {
                    if (RulesManager.TrySave(out string message))
                    {
                        Console.WriteLine("Rule saved successfully!");
                        return;
                    }
                    Console.WriteLine("Could not save the rule into the file.");
                    Console.WriteLine(message);
                    return;
                }
                Console.WriteLine("Something went wrong while adding the rule.");
                return;
            }
            Console.WriteLine("Invalid number.");
            return;
        }
示例#4
0
 /// <summary>
 /// Finds a rule by its name
 /// </summary>
 /// <param name="ruleName">The name of the rule</param>
 /// <returns>The rule data</returns>
 public Rule Find(string ruleName)
 {
     if (FileLoaded)
     {
         IEnumerable <XElement> xRules = xml.Root.Elements();
         foreach (XElement xRule in xRules)
         {
             if (xRule.Attribute(xNameName).Value == ruleName)
             {
                 Rule rule = new Rule();
                 rule.Name = xRule.Attribute(xNameName).Value;
                 rule.LocationFolderName    = xRule.Attribute(xLocationName).Value;
                 rule.DestinationFolderName = xRule.Attribute(xDestinationName).Value;
                 IEnumerable <XElement> xAddresses = xRule.Element(xAddressesName).Elements(xAddressName);
                 foreach (XElement xAddress in xAddresses)
                 {
                     rule.AddAddress(xAddress.Value);
                 }
                 return(rule);
             }
         }
     }
     return(null);
 }