public IActionResult Index(IFormFile file, string word)
        {
            using (var reader = new StreamReader(file.OpenReadStream()))
            {
                var fileContent = reader.ReadToEnd();
                var sentences   = fileContent.Split('.');
                foreach (var s in sentences)
                {
                    if (s.Contains(word))
                    {
                        var count = s.Split(' ').Count(x => x == word);

                        Sentence sentanceToAdd = new Sentence
                        {
                            Count            = count,
                            SentenceInRevert = RevertHelper.Revert(s),
                            Word             = word
                        };

                        _context.Add(sentanceToAdd);
                    }
                }

                _context.SaveChanges();
                ViewBag.Sentences = GetSavedSentences();
                return(View());
            }
        }
示例#2
0
        public CommandResponse Execute(SaveSentenceCommand command)
        {
            var response = new CommandResponse()
            {
                Success = false
            };

            try
            {
                _sentenceContext.Add(command.Sentence);
                _sentenceContext.SaveChanges();

                response.Id      = command.Sentence.Id;
                response.Success = true;
                response.Message = "Persisted sentence successfully";

                return(response);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = "Error occured when persisting data";
                _logger.LogCritical(ex, ex.Message);
                return(response);
            }
        }