public async Task <bool> SetAsync <TProperties>(TProperties properties)
            where TProperties : PropertiesSet, new()
        {
            var schema = Schema.Where(s => s.Value.Type == properties.GetType()).Select(s => s.Value)
                         .FirstOrDefault();

            if (schema == null)
            {
                throw new ArgumentException($"Schema for type {typeof(TProperties)} is not registered");
            }

            var record = await LoadFromDatabaseAsync <TProperties>() ?? new PropertiesRecord
            {
                Key = schema.Key
            };


            record.DateUpdated = DateTimeOffset.UtcNow;
            record.Data        = JsonConvert.SerializeObject(properties);
            if (record.Id != Guid.Empty)
            {
                _dbContext.Update(record);
            }
            else
            {
                _dbContext.Add(record);
            }

            await WriteChangesAsync();

            return(true);
        }
Exemple #2
0
        protected override void InitDbContext(BioContext dbContext)
        {
            var site = new Site {
                Id = SiteId, Title = "Test site", Url = "https://test.ru"
            };

            dbContext.Add(site);
            dbContext.SaveChanges();

            var section = new TestSection
            {
                Title         = "Test section",
                Url           = "test",
                DatePublished = DateTimeOffset.Now,
                IsPublished   = true,
                SiteIds       = new[] { site.Id }
            };

            dbContext.Add(section);
            dbContext.SaveChanges();

            var content = new TestContent
            {
                Title         = "Test content",
                Url           = "test2",
                DatePublished = DateTimeOffset.Now,
                IsPublished   = true,
                SiteIds       = new[] { site.Id },
                SectionIds    = new[] { section.Id }
            };

            dbContext.Add(content);

            var page = new TestContent
            {
                Title         = "Test page",
                Url           = "test3",
                DatePublished = DateTimeOffset.Now,
                IsPublished   = true,
                SiteIds       = new[] { site.Id }
            };

            dbContext.Add(page);
            dbContext.SaveChanges();
        }
        public StorageItem UploadByPath(string path, int size, DateTimeOffset date)
        {
            var file = new StorageItem
            {
                Id          = Guid.NewGuid(),
                FileName    = Path.GetFileName(path),
                DateAdded   = date,
                DateUpdated = date,
                FilePath    = $"files{path}",
                Path        = Path.GetDirectoryName($"files{path}").Replace("\\", "/"),
                FileSize    = size,
                Type        = StorageItemType.Other,
                PublicUri   = new Uri($"https://s3.bioware.ru/files/{path}")
            };

            _dbContext.Add(file);

            return(file);
        }
Exemple #4
0
        private async Task ProcessPostsAsync(Post[] posts, IPBPublishRecord[] records)
        {
            var postIds       = posts.Select(p => p.Id).ToArray();
            var existingPosts = await _dbContext.Set <IPBComment>().Where(p => postIds.Contains(p.PostId)).ToListAsync();

            foreach (var post in posts)
            {
                var record = records.FirstOrDefault(r => r.TopicId == post.ItemId);
                if (record == null)
                {
                    continue;
                }

                if (record.PostId == post.Id)
                {
                    continue;
                }

                var comment = existingPosts.FirstOrDefault(p => p.PostId == post.Id) ?? new IPBComment
                {
                    ContentType = record.Type,
                    ContentId   = record.ContentId,
                    AuthorId    = post.Author?.Id ?? "0",
                    PostId      = post.Id,
                    TopicId     = post.ItemId,
                    DateAdded   = post.Date
                };
                comment.DateUpdated = DateTimeOffset.Now;
                comment.SiteIds     = record.SiteIds;
                if (comment.Id == Guid.Empty)
                {
                    _dbContext.Add(comment);
                }
                else
                {
                    _dbContext.Update(comment);
                }
            }
        }
        protected async Task <(bool isValid, IList <ValidationFailure> errors)> DoAddAsync(TEntity item,
                                                                                           IBioRepositoryOperationContext?operationContext = null)
        {
            if (item.Id == Guid.Empty)
            {
                item.Id = Guid.NewGuid();
            }

            (bool isValid, IList <ValidationFailure> errors)validationResult = (false, new List <ValidationFailure>());
            if (await BeforeValidateAsync(item, validationResult, null, operationContext))
            {
                validationResult = await ValidateAsync(item);

                if (validationResult.isValid)
                {
                    if (await BeforeSaveAsync(item, validationResult, null, operationContext))
                    {
                        DbContext.Add(item);
                    }
                }
            }

            return(validationResult);
        }
        public async Task ImportAsync(Guid siteId, Export data)
        {
            _logger.LogCritical("Begin import");
            await PrintStatsAsync();

            var transaction = await _dbContext.Database.BeginTransactionAsync();

            var site = await _dbContext.Sites.FirstOrDefaultAsync(s => s.Id == siteId);

            if (site == null)
            {
                throw new Exception($"Site with id {siteId.ToString()} not found");
            }

            _tags = await _dbContext.Tags.ToListAsync();

            _filesUploader.BeginBatch();
            _propertiesProvider.BeginBatch();
            _propertiesProvider.DisableChecks();

            try
            {
                _logger.LogInformation($"Developers: {data.Developers.Count.ToString()}");

                _redirects = new Dictionary <string, string>();

                _developersMap = new Dictionary <int, Developer>();
                await ImportDevelopersAsync(data, site);

                _logger.LogInformation($"Games: {data.Games.Count.ToString()}");
                _gamesMap = new Dictionary <int, Game>();
                await ImportGamesAsync(data, site);

                _logger.LogInformation($"Topics: {data.Topics.Count.ToString()}");
                _topicsMap = new Dictionary <int, Topic>();
                await ImportTopicsAsync(data, site);

                var posts   = new List <Post <string> >();
                var newsMap = new Dictionary <NewsExport, Post <string> >();
                if (_options.ImportNews)
                {
                    _logger.LogWarning("News");
                    await ImportNewsAsync(data, site, posts, newsMap);
                }

                //articles
                if (_options.ImportArticles)
                {
                    _logger.LogWarning("Articles");
                    await ImportArticlesAsync(data, site, posts);
                }

                // files
                if (_options.ImportFiles)
                {
                    _logger.LogWarning("Files");
                    ImportFiles(data, site, posts);
                }

                // pictures
                if (_options.ImportGallery)
                {
                    _logger.LogWarning("Gallery");
                    await ImportGalleryAsync(data, site, posts);
                }

                if (posts.Any())
                {
                    var urls         = posts.Select(p => p.Url).ToArray();
                    var existedPosts = await _dbContext.Set <Post <string> >().Where(p => urls.Contains(p.Url))
                                       .ToListAsync();

                    var blocks = await BlocksHelper.GetBlocksAsync(existedPosts.ToArray(), _dbContext);

                    foreach (var existedPost in existedPosts)
                    {
                        existedPost.Blocks = blocks[existedPost.Id];
                    }

                    var toRemove = new List <ContentBlock>();
                    foreach (var post in posts.OrderBy(p => p.DateAdded))
                    {
                        var version = new PostVersion <string> {
                            Id = Guid.NewGuid()
                        };
                        var existedPost = existedPosts.FirstOrDefault(p => p.Url == post.Url);
                        if (existedPost != null)
                        {
                            toRemove.AddRange(existedPost.Blocks);
                            existedPost.Blocks = new List <ContentBlock>();
                            foreach (ContentBlock block in post.Blocks)
                            {
                                block.ContentId = existedPost.Id;
                                existedPost.Blocks.Add(block);
                                _dbContext.Blocks.Add(block);
                            }

                            existedPost.Title      = post.Title;
                            existedPost.SectionIds = post.SectionIds;
                            existedPost.SiteIds    = post.SiteIds;
                            _dbContext.Update(existedPost);
                            _dbContext.Add(existedPost.Blocks);
                            version.ContentId = existedPost.Id;
                            version.SetContent(existedPost);
                        }
                        else
                        {
                            await _dbContext.AddAsync(post);

                            version.ContentId = post.Id;
                            version.SetContent(post);
                        }

                        version.ChangeAuthorId = post.AuthorId;
                        await _dbContext.AddAsync(version);
                    }

                    if (toRemove.Any())
                    {
                        _dbContext.Blocks.RemoveRange(toRemove);
                    }

                    _logger.LogWarning("Properties");
                    foreach (var(news, post) in newsMap)
                    {
                        if (news.TwitterId > 0)
                        {
                            _dbContext.Add(new TwitterPublishRecord
                            {
                                Type      = post.GetKey(),
                                ContentId = post.Id,
                                TweetId   = news.TwitterId.Value,
                                SiteIds   = post.SiteIds
                            });
                        }

                        if (!string.IsNullOrEmpty(news.FacebookId))
                        {
                            _dbContext.Add(new FacebookPublishRecord
                            {
                                Type      = post.GetKey(),
                                ContentId = post.Id,
                                PostId    = news.FacebookId,
                                SiteIds   = post.SiteIds
                            });
                        }

                        if (news.ForumTopicId > 0 && news.ForumPostId > 0)
                        {
                            _dbContext.Add(new IPBPublishRecord
                            {
                                Type      = post.GetKey(),
                                ContentId = post.Id,
                                TopicId   = news.ForumTopicId.Value,
                                PostId    = news.ForumPostId.Value,
                                SiteIds   = post.SiteIds
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                Rollback(transaction);
                return;
            }

            await _dbContext.SaveChangesAsync();

            await PrintStatsAsync();

            transaction.Commit();

            // export redirects
            var redirects = string.Join("\n", _redirects.Select(p => $"{p.Key}          {p.Value};"));
            var nginxMap  = "map $request_uri $redirect_uri {\n" + redirects + "\n}";

            File.WriteAllText($"{_options.OutputPath}/redirects.conf", nginxMap);
            _logger.LogCritical("Done!");
        }