private bool CreateWordWithTranslation(string englishWord, IEnumerable <string> translatedWords, WordType wordType) { string sourceText = englishWord; var wordWithTranslation = new WordWithTranslation(GetWord(_englishLanguageId, sourceText)); foreach (string translatedWord in translatedWords) { wordWithTranslation.AddTranslation(GetWord(_russianLanguageId, translatedWord)); } bool result = _words.Create(wordWithTranslation); return(result); }
public WordWithTranslation GetOrCreate(PronunciationForUser source, PronunciationForUser translation, byte[] image, WordType wordType = WordType.Default, int?rating = null) { //TODO: проверять корректность параметров WordWithTranslation result = null; Adapter.ActionByContext(context => { IEnumerable <Word> sourceWords = FindWords(context, new List <PronunciationForUser> { source }); IEnumerable <Word> translateWords = FindWords(context, new List <PronunciationForUser> { translation }); Word sourceWord = GetOrAddWord(source, wordType, sourceWords, context); Word translationWord = GetOrAddWord(translation, wordType, translateWords, context); context.SaveChanges(); if (sourceWord == null || IdValidator.IsInvalid(sourceWord.Id) || translationWord == null || IdValidator.IsInvalid(translationWord.Id)) { //все слова добавлены - если нужно, добавить связь между новым словом и переводом для него LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "WordsQuery.GetOrCreate can't add text {0} with translation {1}, image {2}, wordType {3}, rating {4}", source.Text, translation.Text, image != null ? image.Length.ToString(CultureInfo.InvariantCulture) : "<NULL>", wordType, rating); result = null; return; } WordTranslation wordTranslation = GetWordTranslation(context, sourceWord, translationWord); if (wordTranslation != null) { //обновить возможно изменившийся рейтинг и изображение wordTranslation.Image = image; wordTranslation.Rating = rating; context.SaveChanges(); } else { wordTranslation = Create(image, rating, context, sourceWord, translationWord); } if (IdValidator.IsValid(wordTranslation.Id)) { result = new WordWithTranslation(sourceWord) { Id = wordTranslation.Id }; result.AddTranslation(translationWord); } else { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "WordsQuery.GetOrCreate can't add/get wordTranslation for text {0} with translation {1}, image {2}, wordType {3}, rating {4}", source.Text, translation.Text, image != null ? image.Length.ToString(CultureInfo.InvariantCulture) : "<NULL>", wordType, rating); } }); return(result); }