コード例 #1
0
        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);
            }
        }
コード例 #3
0
 public void Save()
 {
     using (var context = new SentenceContext())
     {
         context.Sentences.AddRange(Sentences);
         context.SaveChanges();
     }
 }
コード例 #4
0
 public SentenceRepository(SentenceContext context)
 {
     _context = context;
     if (_context.Sentences.Count() == 0)
     {
         _context.Sentences.AddRange(
             new SentenceModel
         {
             Id = 1,
             ReversedSentence = "Bal Lab"
         }
             );
         _context.SaveChanges();
     }
 }
コード例 #5
0
        public ActionResult Upload(HttpPostedFileBase upload, string word)
        {
            if (upload != null)
            {
                fileName = Path.GetFileName(upload.FileName);
                upload.SaveAs(Server.MapPath("~/Files/" + fileName));

                string       appDataPath        = System.Web.HttpContext.Current.Server.MapPath(@"~/Files");
                string       absolutePathToFile = Path.Combine(appDataPath, fileName);
                StreamReader sr = new StreamReader(absolutePathToFile);


                string s_test = System.IO.File.ReadAllText(absolutePathToFile).Replace("\n", " ");


                if (s_test.Any(wordByte => wordByte > 127))
                {
                    s = System.IO.File.ReadAllText(absolutePathToFile, Encoding.GetEncoding(1251)).Replace("\n", " ");
                }
                else
                {
                    s = System.IO.File.ReadAllText(absolutePathToFile).Replace("\n", " ");
                }

                string[] bufs = s.Split('.');


                foreach (string s in bufs)
                {
                    Sentence copy = null;

                    bool res    = Regex.IsMatch(s, "\\b" + word + "\\b");
                    int  amount = new Regex(word).Matches(s).Count;

                    if (res is true)
                    {
                        string Strif = new string(s.ToCharArray().Reverse().ToArray());
                        copy = db.Sentences.FirstOrDefault(u => u.SentenceData == Strif);
                        if (copy == null)
                        {
                            string output = new string(s.ToCharArray().Reverse().ToArray());

                            db.Sentences.Add(new Sentence {
                                SentenceData = output, Amount = amount
                            });
                        }
                        else
                        {
                            string output = new string(s.ToCharArray().Reverse().ToArray());
                            output += "(copy)";
                            db.Sentences.Add(new Sentence {
                                SentenceData = output, Amount = amount
                            });
                        }

                        res = false;
                    }
                }

                sr.Close();
                db.SaveChanges();
            }

            return(RedirectToAction("index"));
        }
コード例 #6
0
 public void Add(SentenceModel entity)
 {
     _context.Sentences.Add(entity);
     _context.SaveChanges();
 }