private static async Task InitGalleryAsync(IConfiguration configuration)
        {
            // Init Products
            DocumentDBStoreRepository storeRepository = new DocumentDBStoreRepository(configuration);

            await storeRepository.InitAsync("Products");

            var productsDB = await storeRepository.GetItemsAsync <ProductDTO>();

            if (productsDB.Count() == 0)
            {
                List <ProductDTO> products = null;

                using (StreamReader r = new StreamReader(Path.Combine(Config.ContentRootPath, @"App_Data\products.json")))
                {
                    string json = r.ReadToEnd();
                    products = JsonConvert.DeserializeObject <List <ProductDTO> >(json);

                    foreach (var product in products)
                    {
                        Document document = await storeRepository.CreateItemAsync(product);

                        string contentType = string.Empty;

                        new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().TryGetContentType(product.ProductPicUrl, out contentType);
                        var attachment = new Attachment {
                            ContentType = contentType, Id = "picture", MediaLink = product.ProductPicUrl
                        };
                        ResourceResponse <Attachment> createdAttachment = await storeRepository.CreateAttachmentAsync(document.AttachmentsLink, attachment, new RequestOptions()
                        {
                            PartitionKey = new PartitionKey(product.Category)
                        });
                    }
                }
            }

            await storeRepository.InitAsync("Categories");

            var categoriesDB = await storeRepository.GetItemsAsync <CustomItem>();

            if (categoriesDB.Count() == 0)
            {
                List <CustomItem> categories = null;

                using (StreamReader r = new StreamReader(Path.Combine(Config.ContentRootPath, @"App_Data\categories.json")))
                {
                    string json = r.ReadToEnd();
                    categories = JsonConvert.DeserializeObject <List <CustomItem> >(json);

                    foreach (var category in categories)
                    {
                        Document document = await storeRepository.CreateItemAsync(category);
                    }
                }
            }

            await storeRepository.InitAsync("Suppliers");

            var suppliersDB = await storeRepository.GetItemsAsync <CustomItem>();

            if (suppliersDB.Count() == 0)
            {
                List <CustomItem> suppliers = null;

                using (StreamReader r = new StreamReader(Path.Combine(Config.ContentRootPath, @"App_Data\suppliers.json")))
                {
                    string json = r.ReadToEnd();
                    suppliers = JsonConvert.DeserializeObject <List <CustomItem> >(json);

                    foreach (var supplier in suppliers)
                    {
                        Document document = await storeRepository.CreateItemAsync(supplier);
                    }
                }
            }
        }
Esempio n. 2
0
        private static async Task InitStoreAsync(IConfiguration configuration)
        {
            // Init Products
            DocumentDBStoreRepository storeRepository = new DocumentDBStoreRepository(configuration);

            await storeRepository.InitAsync("Items");

            var productsDB = await storeRepository.GetItemsAsync <Product>();

            if (productsDB.Count() == 0)
            {
                List <Product> products = null;

                using (StreamReader r = new StreamReader(Path.Combine(Config.WebRootPath, @"wwwroot/products.json")))
                {
                    string json = r.ReadToEnd();
                    products = JsonConvert.DeserializeObject <List <Product> >(json);

                    foreach (var product in products)
                    {
                        await UploadProductImagesAsync(product);

                        foreach (var component in product.Components)
                        {
                            component.Id = Guid.NewGuid().ToString();
                            foreach (var media in component.Medias)
                            {
                                media.Id = Guid.NewGuid().ToString();
                            }
                        }
                        Document document = await storeRepository.CreateItemAsync(product);

                        string contentType = string.Empty;
                    }
                }
            }

            // Init Forum
            await storeRepository.InitAsync("Forum");

            var topicsDB = await storeRepository.GetItemsAsync <Topic>();

            if (topicsDB.Count() == 0)
            {
                List <Topic> topics = null;

                using (StreamReader r = new StreamReader(Path.Combine(Config.WebRootPath, @"wwwroot/topics.json")))
                {
                    string json = r.ReadToEnd();
                    topics = JsonConvert.DeserializeObject <List <Topic> >(json);

                    foreach (var topic in topics)
                    {
                        // await UploadProductImagesAsync(product);

                        foreach (var post in topic.Posts)
                        {
                            post.Id = Guid.NewGuid().ToString();
                        }

                        Document document = await storeRepository.CreateItemAsync(topic);
                    }
                }
            }
        }