Beispiel #1
0
        public async Task <int> Handle(AddArticlesRangeCommand request, CancellationToken cancellationToken)
        {
            var articles = _mapper.Map <IEnumerable <Article> >(request.ArticleDtos);
            await _dbContext.Articles.AddRangeAsync(articles);

            return(await _dbContext.SaveChangesAsync(cancellationToken));
        }
Beispiel #2
0
        public async Task <int> Handle(UpdateCommentsRangeCommand request, CancellationToken cancellationToken)
        {
            var comments = _mapper.Map <IEnumerable <Comment> >(request.CommentDtos);

            _dbContext.Comments.UpdateRange(comments);
            return(await _dbContext.SaveChangesAsync(cancellationToken));
        }
Beispiel #3
0
        public async Task <int> Handle(AddRssCommand request, CancellationToken cancellationToken)
        {
            var rss = _mapper.Map <Rss>(request);
            await _dbContext.Rss.AddAsync(rss);

            return(await _dbContext.SaveChangesAsync(cancellationToken));
        }
Beispiel #4
0
        public async Task <int> Handle(AddCommentCommand request, CancellationToken cancellationToken)
        {
            var comment = _mapper.Map <Comment>(request);
            await _dbContext.Comments.AddAsync(comment);

            return(await _dbContext.SaveChangesAsync(cancellationToken));
        }
        public async Task <int> Handle(AddArticleCommand request, CancellationToken cancellationToken)
        {
            var article = _mapper.Map <Article>(request);
            await _dbContext.Articles.AddAsync(article);

            return(await _dbContext.SaveChangesAsync(cancellationToken));
        }
        public async Task <int> Handle(AddOrUpdateRefreshTokenCommand request, CancellationToken cancellationToken)
        {
            var refreshToken = _dbContext.RefreshTokens.AsNoTracking()
                               .FirstOrDefault(rt => rt.UserId == request.UserId);

            if (refreshToken == null)
            {
                refreshToken = new RefreshToken()
                {
                    Id       = request.Id,
                    UserId   = request.UserId,
                    ExpireAt = request.ExpireAt
                };

                await _dbContext.RefreshTokens.AddAsync(refreshToken);
            }
            else
            {
                refreshToken.UserId   = request.UserId;
                refreshToken.ExpireAt = request.ExpireAt;

                _dbContext.RefreshTokens.Update(refreshToken);
            }

            return(await _dbContext.SaveChangesAsync(cancellationToken));
        }
Beispiel #7
0
        public async Task <int> Handle(AddRssRangeCommand request, CancellationToken cancellationToken)
        {
            var rss = _mapper.Map <IEnumerable <Rss> >(request.RssDtos);
            await _dbContext.Rss.AddRangeAsync(rss);

            return(await _dbContext.SaveChangesAsync(cancellationToken));
        }
Beispiel #8
0
 public async Task <int> Handle(RemoveCommentsRangeCommand request, CancellationToken cancellationToken)
 {
     _dbContext.Comments.RemoveRange(_mapper.Map <IEnumerable <Comment> >(request.CommentDtos));
     return(await _dbContext.SaveChangesAsync(cancellationToken));
 }
Beispiel #9
0
 public async Task <int> Handle(UpdateArticlesRangeCommand request, CancellationToken cancellationToken)
 {
     _dbContext.Articles.UpdateRange(_mapper.Map <IEnumerable <Article> >(request.ArticleDtos));
     return(await _dbContext.SaveChangesAsync(cancellationToken));
 }
 public async Task <int> Handle(RemoveRefreshTokenCommand request, CancellationToken cancellationToken)
 {
     _dbContext.RefreshTokens.Remove(_mapper.Map <RefreshToken>(request.RefreshToken));
     return(await _dbContext.SaveChangesAsync(cancellationToken));
 }
Beispiel #11
0
 public async Task <int> SaveChangesAsync()
 {
     return(await _db.SaveChangesAsync());
 }
Beispiel #12
0
        public async Task <int> Handle(RateNewsCommand request, CancellationToken cancellationToken)
        {
            var articles =
                _mapper.Map <List <ArticleDto> >(await _dbContext.Articles
                                                 .Where(a => !a.GoodFactor.HasValue).Take(30)
                                                 .AsNoTracking()
                                                 .ToListAsync());

            var articleContent = new Dictionary <Guid, List <string> >();

            foreach (var item in articles)
            {
                articleContent.Add(item.Id, new List <string>());

                var text = Regex.Replace(item.Content, "<[^>]+>", " ");
                text = Regex.Replace(text, @"[\u0000-\u001F]", " ");

                var responseString = string.Empty;
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    var httpRequest = new HttpRequestMessage(HttpMethod.Post,
                                                             "http://api.ispras.ru/texterra/v1/nlp?targetType=lemma&apikey=e4d5ecb62d3755f4845dc098adf3d25993efc96c")
                    {
                        Content = new StringContent("[{\"text\":\"" + text + "\"}]", Encoding.UTF8, "application/json")
                    };
                    var httpResponse = await httpClient.SendAsync(httpRequest);

                    responseString = await httpResponse.Content.ReadAsStringAsync();
                }

                if (string.IsNullOrWhiteSpace(responseString))
                {
                    Log.Error("Error while response getting.");
                    continue;
                }

                using (var doc = JsonDocument.Parse(responseString))
                {
                    try
                    {
                        var         root               = doc.RootElement;
                        var         arrayElement       = root[0];
                        var         annotationsElement = arrayElement.GetProperty("annotations");
                        var         lemmaElement       = annotationsElement.GetProperty("lemma");
                        JsonElement valueJson;

                        foreach (var element in lemmaElement.EnumerateArray())
                        {
                            if (element.TryGetProperty("value", out valueJson))
                            {
                                string valueString = valueJson.ToString();
                                if (!string.IsNullOrWhiteSpace(valueString))
                                {
                                    articleContent[item.Id].Add(valueString);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Error while adding words from article. More:\n" + ex.Message);
                    }
                }
            }

            var  temp        = 0;
            var  jsonContent = String.Empty;
            bool saveUnknownWords;

            if (!Boolean.TryParse(_configuration["Constants:SaveUnknownWords"], out saveUnknownWords))
            {
                Log.Error("Constants:SaveUnknownWords field is not valid");
                saveUnknownWords = false;
            }

            using (var sr = new StreamReader(AFINNRUJSON))
            {
                jsonContent = await sr.ReadToEndAsync();
            }

            if (string.IsNullOrWhiteSpace(jsonContent))
            {
                Log.Error("Empty afinn json file content");
                return(-1);
            }

            var notFoundWords = new Dictionary <string, int>();

            using (var doc = JsonDocument.Parse(jsonContent))
            {
                var         root = doc.RootElement;
                JsonElement valueJson;
                var         valueCounter = 0;
                var         wordsCounter = 0;
                var         goodFactor   = 0f;

                foreach (var item in articleContent)
                {
                    valueCounter = 0;
                    wordsCounter = 0;

                    foreach (var word in item.Value)
                    {
                        if (root.TryGetProperty(word, out valueJson) && valueJson.TryGetInt32(out temp))
                        {
                            valueCounter += temp;
                            wordsCounter++;

                            temp = 0;
                        }
                        else if (saveUnknownWords && !notFoundWords.ContainsKey(word))
                        {
                            notFoundWords.Add(word, 0);
                        }
                    }

                    if (wordsCounter == 0)
                    {
                        goodFactor = 0;
                        Log.Warning("0 words found for article " + item.Key.ToString());
                    }
                    else
                    {
                        goodFactor = (float)valueCounter / wordsCounter;
                    }

                    articles[articles.FindIndex(a => a.Id == item.Key)].GoodFactor = goodFactor;
                }
            }

            _dbContext.Articles.UpdateRange(_mapper.Map <List <Article> >(articles));
            var result = await _dbContext.SaveChangesAsync(cancellationToken);

            if (saveUnknownWords)
            {
                var fileName      = Path.Combine(UNKNOWNWORDSDIR, UNKNOWNWORDSFILE);
                var existingItems = new Dictionary <string, int>();

                try
                {
                    if (!Directory.Exists(UNKNOWNWORDSDIR))
                    {
                        Directory.CreateDirectory(UNKNOWNWORDSDIR);
                    }
                    if (!File.Exists(fileName))
                    {
                        File.Create(fileName);
                    }

                    using (var r = new StreamReader(fileName))
                    {
                        var json = await r.ReadToEndAsync();

                        try
                        {
                            existingItems = JsonSerializer.Deserialize <Dictionary <string, int> >(json);
                        }
                        catch (Exception e)
                        {
                            Log.Error(e.Message);
                            existingItems = new Dictionary <string, int>();
                        }
                    }

                    foreach (var notFoundWord in notFoundWords)
                    {
                        if (!existingItems.ContainsKey(notFoundWord.Key))
                        {
                            existingItems.Add(notFoundWord.Key, notFoundWord.Value);
                        }
                    }

                    using (var fs = new StreamWriter(fileName, false, Encoding.UTF8))
                    {
                        var encoderSettings = new TextEncoderSettings();
                        encoderSettings.AllowRange(UnicodeRanges.All);
                        var options = new JsonSerializerOptions
                        {
                            Encoder       = JavaScriptEncoder.Create(encoderSettings),
                            WriteIndented = true
                        };

                        var jsonString = JsonSerializer.Serialize(existingItems, options);
                        await fs.WriteLineAsync(jsonString);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message);
                }
            }

            return(result);
        }
Beispiel #13
0
 public async Task <int> Handle(RemoveArticleCommand request, CancellationToken cancellationToken)
 {
     _dbContext.Articles.Remove(_mapper.Map <Article>(request.ArticleDto));
     return(await _dbContext.SaveChangesAsync(cancellationToken));
 }