private SourceWithTranslation Create(GroupForUser groupForUser, SourceWithTranslation sentenceWithTranslation, int?rating) { SourceWithTranslation result = null; Adapter.ActionByContext(context => { var groupSentence = new GroupSentence { SentenceTranslationId = sentenceWithTranslation.Id, GroupId = groupForUser.Id, Rating = rating }; context.GroupSentence.Add(groupSentence); context.SaveChanges(); if (IdValidator.IsValid(groupSentence.Id)) { result = new SourceWithTranslation(); result.Set(groupSentence.Id, sentenceWithTranslation.Source, sentenceWithTranslation.Translation); } else { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "GroupSentencesQuery.Create can't add sentence with translation for sentence with id {0}, translation with id {1}, rating {2}", sentenceWithTranslation.Source.Id, sentenceWithTranslation.Translation.Id, rating); } }); return(result); }
/// <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 sentencesQuery = new SentencesQuery(); SourceWithTranslation sentenceWithTranslation = sentencesQuery.GetOrCreate(SentenceType.FromGroup, source, translation, image, null); if (sentenceWithTranslation == null || IdValidator.IsInvalid(sentenceWithTranslation.Id)) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "GroupSentencesQuery.GetOrCreate can't add sentence {0} with translation {1}, image {2}, rating {3}", source.Text, translation.Text, image != null ? image.Length.ToString(CultureInfo.InvariantCulture) : "<NULL>", rating); return(null); } SourceWithTranslation result = Adapter.ReadByContext(c => { var wordsWithTranslationsQuery = (from s1 in c.Sentence join st in c.SentenceTranslation on s1.Id equals st.SentenceId1 join gs in c.GroupSentence on st.Id equals gs.SentenceTranslationId join s2 in c.Sentence on st.SentenceId2 equals s2.Id where gs.GroupId == groupForUser.Id && st.Id == sentenceWithTranslation.Id select new { gs, st, s1, s2 }); var firstRecord = wordsWithTranslationsQuery.AsEnumerable().FirstOrDefault(); if (firstRecord == null) { return(null); } //сохранить возможно изменившийся рейтинг GroupSentence groupSentence = firstRecord.gs; groupSentence.Rating = rating; c.SaveChanges(); SourceWithTranslation innerResult = ConvertToGroupSentenceWithTranslation(firstRecord.st.Id, firstRecord.st.Image, sentenceWithTranslation .Source.LanguageId, firstRecord.s1, firstRecord.s2); return(innerResult); }); if (result == null) { result = Create(groupForUser, sentenceWithTranslation, rating); } return(result); }
private Tuple <UserKnowledge, UserRepetitionInterval> ConvertRow(GroupSentence groupSentence, UserRepetitionInterval userRepetitionInterval) { DateTime minDateTime = new DateTime().GetDbDateTime(); var userKnowledge = new UserKnowledge { Id = groupSentence.Id, DataId = groupSentence.SentenceTranslationId, DataType = _dataType, Data = null, UserId = _userId, LanguageId = _languageId, CreationDate = minDateTime, DeletedDate = minDateTime, Hash = null, Tip = null, SystemData = null, Status = (int)KnowledgeStatus.Unknown }; return(new Tuple <UserKnowledge, UserRepetitionInterval>(userKnowledge, userRepetitionInterval)); }