コード例 #1
0
        /// <summary>
        /// Indexes a searchword, finds all texts containig it and adds
        /// a connection to database between seachword and text.
        /// </summary>
        /// <param name="searchword">
        /// Provided searchword
        /// </param>
        /// <returns>
        /// boolean with value depending if indexing was succesfull
        /// </returns>
        public bool IndexSearchWord(SearchWords searchword)
        {
            try
            {
                using (var db = new SearcherContext())
                {
                    //List of texts containing searchword
                    List <Texts> texts = db.Texts.Where(t => t.text.Contains(searchword.SearchWord))
                                         .Include(t => t.SearchWords)
                                         .ToList();
                    foreach (var text in texts)
                    {
                        bool update = true;
                        //For each loop checks if there is already such searchword for this text
                        foreach (var searchWordInText in text.SearchWords)
                        {
                            if (searchWordInText.SearchWord.Contains(searchword.SearchWord))
                            {
                                update = false;
                            }
                        }
                        //If there is no such search word for text, it is indexed
                        if (update)
                        {
                            //Fiding the actual searchword from table
                            SearchWords existingSearchword =
                                db.SearchWords.FirstOrDefault(s => s.SearchWord.Equals(searchword.SearchWord));
                            if (existingSearchword != null)
                            {
                                text.SearchWords.Add(existingSearchword);
                                db.Entry(text).State = System.Data.Entity.EntityState.Modified;
                                db.SaveChanges();
                            }
                            else
                            {
                                text.SearchWords.Add(searchword);
                                db.Entry(text).State = System.Data.Entity.EntityState.Modified;
                                db.SaveChanges();
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
コード例 #2
0
        public async Task <IHttpActionResult> PutSearchWords(int id, SearchWords searchWords)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != searchWords.id)
            {
                return(BadRequest());
            }

            db.Entry(searchWords).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SearchWordsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }