コード例 #1
0
        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());;
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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));
        }
コード例 #6
0
 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);
     }
 }
コード例 #7
0
 public IList <Translation> GetDictionaryTranslations(int dictionaryId)
 {
     return(TranslationsRepository.GetTranslationsForDictionary(dictionaryId).ToList());
 }
コード例 #8
0
        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);
                }
            }
        }