public bool UpdateDictionary(int dictionaryId) { var dictionary = DictionariesRepository.GetById(dictionaryId); if (dictionary.ParentDictionaryId == null) { return(false); } var currentTranslations = TranslationsRepository.GetTranslationsForDictionary(dictionaryId).ToList(); var parentTranslations = TranslationsRepository.GetTranslationsForDictionary(dictionary.ParentDictionaryId.Value).ToList(); var toAdd = parentTranslations.Except(currentTranslations).ToList(); toAdd.ForEach(trans => { var translation = new Translation { DictionaryId = dictionaryId, FirstLangWord = trans.FirstLangWord, SecondLangWord = trans.SecondLangWord }; TranslationsRepository.Insert(translation); }); return(TranslationsRepository.Save());; }
public int CopyDictionary(int dictionaryId, int userId) { var translations = TranslationsRepository.GetTranslationsForDictionary(dictionaryId).ToList(); var dictionary = DictionariesRepository.GetById(dictionaryId); Context.Entry(dictionary).State = EntityState.Detached; dictionary.Id = 0; dictionary.UserId = userId; dictionary.ParentDictionaryId = dictionaryId; dictionary.User = null; dictionary.Date = DateTime.Today; dictionary.IsPublic = false; DictionariesRepository.Insert(dictionary); DictionariesRepository.Save(); translations.ForEach(translation => { Context.Entry(translation).State = EntityState.Detached; translation.Id = 0; translation.DictionaryId = dictionary.Id; translation.Dictionary = null; }); var check = TranslationsRepository.Insert(translations) && TranslationsRepository.Save(); return(check ? dictionary.Id : -1); }
public IList <Translation> GetTranslationsById(IEnumerable <int> ids, bool reverse) { var idsList = ids.ToList(); var data = TranslationsRepository.GetAll().Where(x => idsList.Contains(x.Id)); return(reverse ? ReverseTranslations(data).ToList() : data.ToList()); }
public bool Delete(int dictionaryId) { TranslationsRepository.DeleteByDictionaryId(dictionaryId); TranslationsRepository.Save(); GameSessionTranslationsRepository.DeleteByDictionaryId(dictionaryId); GameSessionTranslationsRepository.Save(); GameSessionsRepository.DeleteByDictionaryId(dictionaryId); GameSessionsRepository.Save(); DictionariesRepository.Delete(dictionaryId); DictionariesRepository.Save(); return(true); }
public MemoryStream ExportDictionary(int dictionaryId) { var dictionary = DictionariesRepository.GetById(dictionaryId); var translations = TranslationsRepository.GetTranslationsForDictionary(dictionaryId); var translationPairsList = new List <TranslationPair>(); translationPairsList.Add(new TranslationPair { FirstLanguageWord = dictionary.FirstLanguage.Name, SecondLanguageWord = dictionary.SecondLanguage.Name }); foreach (var translation in translations) { translationPairsList.Add(new TranslationPair { FirstLanguageWord = translation.FirstLangWord, SecondLanguageWord = translation.SecondLangWord }); } return(CsvService.CreateCsv(translationPairsList)); }
public bool ImportDictionary(MemoryStream csv, int dictionaryId) { try { var list = CsvService.FormatCsv(csv); var toAdd = new List <Translation>(); foreach (TranslationPair translationPair in list) { toAdd.Add(new Translation { DictionaryId = dictionaryId, FirstLangWord = translationPair.FirstLanguageWord, SecondLangWord = translationPair.SecondLanguageWord }); } TranslationsRepository.Insert(toAdd); TranslationsRepository.Save(); return(true); } catch (Exception) { return(false); } }
public IList <Translation> GetDictionaryTranslations(int dictionaryId) { return(TranslationsRepository.GetTranslationsForDictionary(dictionaryId).ToList()); }
public int InsertOrUpdate(DictionaryDTO dictionaryVo) { using (var transaction = Context.Database.BeginTransaction()) { try { var dictionary = Mapper.Map <Dictionary>(dictionaryVo); if (dictionary.Id > 0) { Context.Entry(dictionary).State = EntityState.Modified; DictionariesRepository.Update(dictionary); } else { dictionary.Date = DateTime.Now; dictionary.ParentDictionaryId = null; DictionariesRepository.Insert(dictionary); } DictionariesRepository.Save(); dictionaryVo.TranslationList.ToList().ForEach(x => x.DictionaryId = dictionary.Id); var translations = dictionaryVo.TranslationList.Select(x => x.Id).ToList(); var currentTranslations = TranslationsRepository .GetAll() .Where(x => x.DictionaryId == dictionary.Id) .ToList(); var ids = currentTranslations.Select(x => x.Id).ToList(); var toDelete = currentTranslations.Where(x => !translations.Contains(x.Id)).ToList(); var toAdd = dictionaryVo.TranslationList?.Where(x => !ids.Contains(x.Id)).ToList() ?? new List <Translation>(); var toUpdate = dictionaryVo.TranslationList?.Where(x => ids.Contains(x.Id)).ToList() ?? new List <Translation>(); foreach (var trans in toDelete) { GameSessionTranslationsRepository.DeleteByTranslationId(trans.Id); TranslationsRepository.Delete(trans); } foreach (var trans in toUpdate) { TranslationsRepository.Update(trans); } foreach (var trans in toAdd) { TranslationsRepository.Insert(trans); } TranslationsRepository.Save(); transaction.Commit(); return(dictionary.Id); } catch (Exception) { transaction.Rollback(); return(-1); } } }