Esempio n. 1
0
        private void Delete(string parameters)
        {
            if (parameters is null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            Dictionary <string, string> where = QueryParser.DeleteParser(parameters);

            if (where != null)
            {
                FileCabinetRecord whereRecord = DataHelper.CreateRecordFromDict(where);

                var records = this.Service.FindRecords(whereRecord, QueryParser.TypeCondition);

                var builder             = new StringBuilder();
                int countMatchedRecords = 0;
                foreach (var item in records)
                {
                    builder.Append($"#{item.Id}, ");
                    countMatchedRecords++;
                }

                string text = countMatchedRecords == 0
                    ? $"No deleted records."
                    : $"{(countMatchedRecords == 1 ? "Record" : "Records")} " +
                              $"{builder.ToString().TrimEnd(WhiteSpace, Comma)} " +
                              $"{(countMatchedRecords == 1 ? "is" : "are")} deleted.";

                this.Service.Delete(records);
                Memoization.RefreshMemoization();
                Console.WriteLine(text);
            }
        }
        private void Update(string parameters)
        {
            if (parameters is null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var(propNewValuesPair, propWhereValuesPair) = QueryParser.UpdateParser(parameters);

            if (propNewValuesPair != null || propWhereValuesPair != null)
            {
                var set = propNewValuesPair;
                var where = propWhereValuesPair;

                var newValues  = DataHelper.CreateRecordFromDict(set);
                var oldRecords = DataHelper.CreateRecordFromDict(where);
                if (newValues != null && oldRecords != null)
                {
                    var updatedRecords = this.Service.FindRecords(oldRecords, QueryParser.TypeCondition);
                    var builder        = new StringBuilder();

                    foreach (var item in updatedRecords)
                    {
                        builder.Append($"#{item.Id}, ");
                        var current = this.TrimFields(newValues, item);
                        try
                        {
                            this.Service.EditRecord(item.Id, current);
                        }
                        catch (ArgumentException ex)
                        {
                            Console.WriteLine(ex.Message + "Invalid parameters.");
                            builder.Clear();
                        }

                        Memoization.RefreshMemoization();
                    }

                    string message = string.Empty;
                    if (builder.Length == 0)
                    {
                        message = "There is no selected records to update matching this condition.";
                    }
                    else if (set.TryGetValue("id", out string value))
                    {
                        Console.WriteLine("Unfortunately, you cannot update id.");
                    }
                    else
                    {
                        message = updatedRecords.Count() == 1
                            ? $"Record {builder.ToString().TrimEnd(' ', ',')} is updated."
                            : $"Records {builder.ToString().TrimEnd(' ', ',')} are updated.";
                    }

                    Console.WriteLine(message);
                }
            }
        }