// Delete synset of term public int DeleteSynsetOfTerm(string headWord, int synsetId) { // Check term isExist WordIndex index = context.WordIndexes.Find(headWord); Synset synset = context.Synsets.Find(synsetId); if (index != null && synset != null) { int countSynsets = index.Synsets.Count; index.Synsets.Remove(synset); if (countSynsets <= 1) { context.WordIndexes.Remove(index); } if (synset.WordIndexes.Count == 0) { context.Synsets.Remove(synset); } } else { return(-1); } // Remove PK context.Comments.Delete(x => x.HeadWord == headWord); context.Favorites.Delete(x => x.HeadWord == headWord); // Save changes return(context.SaveChanges()); }
// Delete synset by synsetId public int DeleteSynsetBySynsetId(int synsetId, string headWord) { Synset synset = context.Synsets.Find(synsetId); WordIndex term = context.WordIndexes.Find(headWord); term.Synsets.Remove(synset); context.Synsets.Remove(synset); return(context.SaveChanges()); }
// Edit synset by synsetId public int EditSynsetBySynsetId(AddTermViewModel editedSynset, int synsetId) { Synset synset = context.Synsets.Find(synsetId); synset.Category = editedSynset.Catagory; synset.Def = editedSynset.Def; synset.Exa = editedSynset.Exa; //delete all synonym relation synset.WordIndexes.Clear(); synset.WordIndexes.Add(context.WordIndexes.Find(editedSynset.HeadWord)); // If synset has synonym(s) if (!String.IsNullOrWhiteSpace(editedSynset.Synonyms)) { string[] synonyms = editedSynset.Synonyms.Split(','); foreach (string headWord in synonyms) { // Check headWord exist in database WordIndex wordIndex = context.WordIndexes.Find(headWord); if (wordIndex != null) { //synset.WordIndexes.Add(wordIndex); } else { //entry wordIndex = new WordIndex(); wordIndex.HeadWord = headWord; string[] word = headWord.Split(' '); if (word.Count() > 1) { wordIndex.WordType = "c"; } else { wordIndex.WordType = "s"; } } synset.WordIndexes.Add(wordIndex); } } context.Entry(synset).State = EntityState.Modified; return(context.SaveChanges()); }
// Add new term or new synset public int AddNewTermOrSynset(AddTermViewModel term) { // Check isExist term WordIndex checkTerm = context.WordIndexes.Find(term.HeadWord); if (checkTerm == null) { // Entry checkTerm = new WordIndex(); checkTerm.HeadWord = term.HeadWord; string[] word = term.HeadWord.Split(' '); if (word.Count() > 1) { checkTerm.WordType = "c"; } else { checkTerm.WordType = "s"; } } else { // Check duplicate content //var checkDup = checkTerm.Synsets.Contains(context.Synsets.Where(x => x.Def.Contains(term.Def)).FirstOrDefault()); bool checkDup2 = false; foreach (var item in checkTerm.Synsets) { if (item.Def.Equals(term.Def)) { checkDup2 = true; break; } } if (checkDup2) { return(0); } } // Synset Synset synset = new Synset(); synset.Category = term.Catagory; synset.Def = term.Def; synset.Exa = term.Exa; // If synset has synonym(s) if (!String.IsNullOrWhiteSpace(term.Synonyms)) { string[] synonyms = term.Synonyms.Split(','); foreach (string headWord in synonyms) { // Check headWord exist in database WordIndex wordIndex = context.WordIndexes.Find(headWord); if (wordIndex != null) { //synset.WordIndexes.Add(wordIndex); } else { //entry wordIndex = new WordIndex(); wordIndex.HeadWord = headWord; string[] words = headWord.Split(' '); if (words.Count() > 1) { wordIndex.WordType = "c"; } else { wordIndex.WordType = "s"; } } synset.WordIndexes.Add(wordIndex); } } // synset.WordIndexes.Add(checkTerm); context.Synsets.Add(synset); int result = context.SaveChanges(); return(result); }