Пример #1
0
        public async Task Seed()
        {
            try
            {
                await _semaphoreSlim.WaitAsync();
                await AllClear();

                var result = await _http.Get(new AiurUrl(_configuration["ResourcesUrl"] + "structure.json"), false);

                var sourceObject = JsonConvert.DeserializeObject <List <Collection> >(result);

                // Get all collections
                foreach (var collection in sourceObject)
                {
                    // Insert collection
                    var newCollection = new Collection
                    {
                        CollectionTitle = collection.CollectionTitle,
                        DocAPIAddress   = collection.DocAPIAddress
                    };
                    _dbContext.Collections.Add(newCollection);
                    await _dbContext.SaveChangesAsync();

                    // Get markdown from existing documents.
                    foreach (var article in collection.Articles ?? new List <Article>())
                    {
                        // markdown http url.
                        if (!string.IsNullOrEmpty(article.ArticleAddress))
                        {
                            var newarticle = new Article
                            {
                                ArticleTitle   = article.ArticleTitle,
                                ArticleAddress = article.ArticleAddress,
                                ArticleContent = await _http.Get(new AiurUrl(article.ArticleAddress), false),
                                CollectionId   = newCollection.CollectionId
                            };
                            _dbContext.Article.Add(newarticle);
                            await _dbContext.SaveChangesAsync();
                        }
                        // GitHub repo.
                        else
                        {
                            var newarticle = new Article
                            {
                                ArticleTitle   = article.ArticleTitle,
                                ArticleContent = await _http.Get(new AiurUrl($"{_configuration["ResourcesUrl"]}{collection.CollectionTitle}/{article.ArticleTitle}.md"), false),
                                CollectionId   = newCollection.CollectionId
                            };
                            _dbContext.Article.Add(newarticle);
                            await _dbContext.SaveChangesAsync();
                        }
                    }

                    // Parse the appended doc.
                    if (!string.IsNullOrWhiteSpace(collection.DocAPIAddress))
                    {
                        // Generate markdown from doc generator
                        var docString = await _http.Get(new AiurUrl(collection.DocAPIAddress), false);

                        var docModel   = JsonConvert.DeserializeObject <List <API> >(docString);
                        var docGrouped = docModel.GroupBy(t => t.ControllerName);
                        var apiRoot    = collection.DocAPIAddress.ToLower().Replace("/doc", "");
                        foreach (var docController in docGrouped)
                        {
                            var markdown   = _markDownGenerator.GenerateMarkDownForAPI(docController, apiRoot);
                            var newarticle = new Article
                            {
                                ArticleTitle   = docController.Key.TrimController(),
                                ArticleContent = markdown,
                                CollectionId   = newCollection.CollectionId,
                                BuiltByJson    = true
                            };
                            _dbContext.Article.Add(newarticle);
                            await _dbContext.SaveChangesAsync();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                var accessToken = await _appsContainer.AccessToken();

                await _eventService.LogAsync(accessToken, e.Message, e.StackTrace, EventLevel.Exception);

                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Пример #2
0
        public async Task Seed()
        {
            try
            {
                Seeding = true;
                await AllClear();

                var result = await _http.Get(new AiurUrl(_configuration["ResourcesUrl"] + "structure.json"), false);

                var sourceObject = JsonConvert.DeserializeObject <List <Collection> >(result);

                // Get all collections
                foreach (var collection in sourceObject)
                {
                    // Insert collection
                    var newCollection = new Collection
                    {
                        CollectionTitle = collection.CollectionTitle,
                        DocAPIAddress   = collection.DocAPIAddress
                    };
                    _dbContext.Collections.Add(newCollection);
                    await _dbContext.SaveChangesAsync();

                    // Get markdown from GitHub
                    foreach (var article in collection.Articles ?? new List <Article>())
                    {
                        if (!string.IsNullOrEmpty(article.ArticleAddress))
                        {
                            var newarticle = new Article
                            {
                                ArticleTitle   = article.ArticleTitle,
                                ArticleAddress = article.ArticleAddress,
                                ArticleContent = await _http.Get(new AiurUrl(article.ArticleAddress), false),
                                CollectionId   = newCollection.CollectionId
                            };
                            _dbContext.Article.Add(newarticle);
                            await _dbContext.SaveChangesAsync();
                        }
                        else
                        {
                            var newarticle = new Article
                            {
                                ArticleTitle   = article.ArticleTitle,
                                ArticleContent = await _http.Get(new AiurUrl($"{_configuration["ResourcesUrl"]}{collection.CollectionTitle}/{article.ArticleTitle}.md"), false),
                                CollectionId   = newCollection.CollectionId
                            };
                            _dbContext.Article.Add(newarticle);
                            await _dbContext.SaveChangesAsync();
                        }
                    }

                    if (string.IsNullOrWhiteSpace(collection.DocAPIAddress))
                    {
                        continue;
                    }

                    // Generate markdown from doc generator
                    var docString = await _http.Get(new AiurUrl(collection.DocAPIAddress), false);

                    var docModel   = JsonConvert.DeserializeObject <List <API> >(docString);
                    var docGrouped = docModel.GroupBy(t => t.ControllerName);
                    var apiRoot    = collection.DocAPIAddress.ToLower().Replace("/doc", "");
                    foreach (var docController in docGrouped)
                    {
                        var markdown   = _markdown.GenerateMarkDownForAPI(docController, apiRoot);
                        var newarticle = new Article
                        {
                            ArticleTitle   = docController.Key.TrimController(),
                            ArticleContent = markdown,
                            CollectionId   = newCollection.CollectionId
                        };
                        _dbContext.Article.Add(newarticle);
                        await _dbContext.SaveChangesAsync();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                Seeding = false;
            }
        }