<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using DocumentDbClientLibrary;
using Newtonsoft.Json.Linq;
using Schema.N;

namespace DocDbDemoClient
{
    class Program
    {
        private static Random random = new Random();

        static DocumentDbClient DocDbClient { get; set; }
        static void Main(string[] args)
        {
            var endpoint = ConfigurationManager.AppSettings["endpoint"];
            var authKey = ConfigurationManager.AppSettings["authKey"];
            var database = ConfigurationManager.AppSettings["database"];
            var collection = ConfigurationManager.AppSettings["collection"];
            DocDbClient = new DocumentDbClient(endpoint, authKey, database, collection);

            while (true)
            {
                Console.WriteLine("===========================");
                Console.WriteLine("Select from the Menu:");
                Console.WriteLine("===========================");
                Console.WriteLine("1. Reset Data");
                Console.WriteLine("2. Upload version 1 Data");
                Console.WriteLine("3. Upgrade from Version 1 to Version 2");
                Console.WriteLine("Choose Option (1/2/3):");
                var ch = Console.ReadLine();
                try
                {
                    switch (ch)
                    {
                        case "1":
                            Task.Run(async () => await ClearTestDB()).Wait();
                            break;
                        case "2":
                            Task.Run(async () => await UploadTestDataToDocDb()).Wait();
                            break;
                        case "3":
                            Task.Run(async () => await TestVersionUpgradeInDocDb()).Wait();
                            break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("================= Exception Occurred!! ===================");
                    Console.WriteLine(e.Message + " StackTrace: " + e.StackTrace);
                    Console.WriteLine("==========================================================");
                }
                Console.WriteLine("Continue? Press y to continue / Press any other key to exit ");
                ch = Console.ReadLine();
                if (string.Compare(ch, "y", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    continue;
                }
                return;
            }            
        }

        private static async Task ClearTestDB()
        {
            // Hacky way to delete
            var itemsV1 = await DocDbClient.GetItemsAsync<UserV1>(f => f.isActive || !f.isActive);
            var itemsV2 = await DocDbClient.GetItemsAsync<UserV2>(f => f.isActive || !f.isActive);
            foreach (var item in itemsV1.ToList())
            {
                await DocDbClient.DeleteItemAsync(item.id);
            }
            try
            {
                foreach (var item in itemsV2.ToList())
                {
                    await DocDbClient.DeleteItemAsync(item.id);
                }
            }
            catch (Exception e)
            {
                // Do nothing, all the resources has been already deleted.
            }
        }

        public static async Task UploadTestDataToDocDb()
        {
            var tasks = Enumerable.Range(0, 10)
                .Select(i => GenerateRandomObject())
                .Select(r => Task.Run(async () => await DocDbClient.CreateItemAsync(r))).ToList();

            Task.WaitAll(tasks.ToArray());
        }

        public static JObject GenerateRandomObject()
        {
            var eyeColors = new[] { "brown", "blue", "green" };<commit_msg>Fix the delete issue on demo project<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using DocumentDbClientLibrary;
using Newtonsoft.Json.Linq;
using Schema.N;

namespace DocDbDemoClient
{
    class Program
    {
        private static Random random = new Random();

        static DocumentDbClient DocDbClient { get; set; }
        static void Main(string[] args)
        {
            var endpoint = ConfigurationManager.AppSettings["endpoint"];
            var authKey = ConfigurationManager.AppSettings["authKey"];
            var database = ConfigurationManager.AppSettings["database"];
            var collection = ConfigurationManager.AppSettings["collection"];
            DocDbClient = new DocumentDbClient(endpoint, authKey, database, collection);

            while (true)
            {
                Console.WriteLine("===========================");
                Console.WriteLine("Select from the Menu:");
                Console.WriteLine("===========================");
                Console.WriteLine("1. Reset Data");
                Console.WriteLine("2. Upload version 1 Data");
                Console.WriteLine("3. Upgrade from Version 1 to Version 2");
                Console.WriteLine("Choose Option (1/2/3):");
                var ch = Console.ReadLine();
                try
                {
                    switch (ch)
                    {
                        case "1":
                            Task.Run(async () => await ClearTestDB()).Wait();
                            break;
                        case "2":
                            Task.Run(async () => await UploadTestDataToDocDb()).Wait();
                            break;
                        case "3":
                            Task.Run(async () => await TestVersionUpgradeInDocDb()).Wait();
                            break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("================= Exception Occurred!! ===================");
                    Console.WriteLine(e.Message + " StackTrace: " + e.StackTrace);
                    Console.WriteLine("==========================================================");
                }
                Console.WriteLine("Continue? Press y to continue / Press any other key to exit ");
                ch = Console.ReadLine();
                if (string.Compare(ch, "y", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    continue;
                }
                return;
            }            
        }

        private static async Task ClearTestDB()
        {
            // Hacky way to delete
            var itemsV1 = await DocDbClient.GetItemsAsync<UserV1>(f => f.isActive || !f.isActive);
            foreach (var item in itemsV1.ToList())
            {
                await DocDbClient.DeleteItemAsync(item.id);
            }
            var itemsV2 = await DocDbClient.GetItemsAsync<UserV2>(f => f.isActive || !f.isActive);
            foreach (var item in itemsV2.ToList())
            {
                await DocDbClient.DeleteItemAsync(item.id);
            }
        }

        public static async Task UploadTestDataToDocDb()
        {
            var tasks = Enumerable.Range(0, 10)
                .Select(i => GenerateRandomObject())
                .Select(r => Task.Run(async () => await DocDbClient.CreateItemAsync(r))).ToList();

            Task.WaitAll(tasks.ToArray());
        }

        public static JObject GenerateRandomObject()
        {
            var eyeColors = new[] { "brown", "blue", "green" };
{'fraction_non_alphanumeric': 0.13605263157894737, 'fraction_numerical': 0.005789473684210527, 'mean_word_length': 1.4986850756081525, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4535294', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1690, 'n_words': 409}