예제 #1
0
        /// <summary>
        /// Создает слова для группы
        /// </summary>
        /// <param name="groupForUser">группа, к которой нужно добавить слово</param>
        /// <param name="source">слово</param>
        /// <param name="translation">перевод</param>
        /// <param name="image">изображение для слова</param>
        /// <param name="rating">рейтинг</param>
        /// <returns>созданные слова для группы, или ничего</returns>
        public SourceWithTranslation GetOrCreate(GroupForUser groupForUser,
                                                 PronunciationForUser source,
                                                 PronunciationForUser translation,
                                                 byte[] image,
                                                 int?rating)
        {
            var wordsQuery = new WordsQuery();
            WordWithTranslation wordWithTranslation = wordsQuery.GetOrCreate(source, translation, image,
                                                                             WordType.Default, null);

            if (wordWithTranslation == null)
            {
                return(null);
            }
            SourceWithTranslation result = Adapter.ReadByContext(c => {
                var wordsWithTranslationsQuery = (from w1 in c.Word
                                                  join wt in c.WordTranslation on w1.Id equals wt.WordId1
                                                  join gw in c.GroupWord on wt.Id equals gw.WordTranslationId
                                                  join w2 in c.Word on wt.WordId2 equals w2.Id
                                                  where gw.GroupId == groupForUser.Id && wt.Id == wordWithTranslation.Id
                                                  select new { gw, wt, w1, w2 });
                var firstRecord = wordsWithTranslationsQuery.AsEnumerable().FirstOrDefault();
                if (firstRecord == null)
                {
                    return(null);
                }
                //сохранить возможно изменившийся рейтинг
                GroupWord groupWord = firstRecord.gw;
                groupWord.Rating    = rating;
                c.SaveChanges();

                SourceWithTranslation innerResult = ConverterEntities.ConvertToSourceWithTranslation(firstRecord.wt.Id,
                                                                                                     firstRecord.wt.
                                                                                                     Image,
                                                                                                     wordWithTranslation
                                                                                                     .Source.
                                                                                                     LanguageId,
                                                                                                     firstRecord.w1,
                                                                                                     firstRecord.w2);
                return(innerResult);
            });

            if (result == null)
            {
                result = Create(groupForUser, wordWithTranslation, rating);
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// Создает слова по типу популярности
        /// </summary>
        /// <param name="source">слово</param>
        /// <param name="translation">перевод</param>
        /// <param name="type">тип популярности</param>
        /// <returns>созданные слова для группы, или ничего</returns>
        public SourceWithTranslation GetOrCreate(PronunciationForUser source,
                                                 PronunciationForUser translation,
                                                 PopularWordType type)
        {
            var wordsQuery = new WordsQuery();
            WordWithTranslation wordWithTranslation = wordsQuery.GetOrCreate(source, translation, null, WordType.Default,
                                                                             null);

            if (wordWithTranslation == null)
            {
                return(null);
            }
            var parsedType = (int)type;
            SourceWithTranslation result = Adapter.ReadByContext(c => {
                var wordsWithTranslationsQuery = (from w1 in c.Word
                                                  join wt in c.WordTranslation on w1.Id equals wt.WordId1
                                                  join pw in c.PopularWord on wt.Id equals pw.WordTranslationId
                                                  join w2 in c.Word on wt.WordId2 equals w2.Id
                                                  where pw.Type == parsedType && wt.Id == wordWithTranslation.Id
                                                  select new { wt, w1, w2 });
                var firstRecord = wordsWithTranslationsQuery.AsEnumerable().FirstOrDefault();
                if (firstRecord == null)
                {
                    return(null);
                }

                SourceWithTranslation innerResult = ConverterEntities.ConvertToSourceWithTranslation(firstRecord.wt.Id,
                                                                                                     firstRecord.wt.
                                                                                                     Image,
                                                                                                     wordWithTranslation
                                                                                                     .Source.
                                                                                                     LanguageId,
                                                                                                     firstRecord.w1,
                                                                                                     firstRecord.w2);
                return(innerResult);
            });

            if (result == null)
            {
                result = Create(wordWithTranslation, parsedType);
            }
            return(result);
        }