public static void PushWord(LanguageType language, SpeachPartType speachPart, string word) { #if !NO_REDIS string key = GetListKey(language, speachPart); Db.ListLeftPush(key, word); #endif }
public static void DelWords(LanguageType language, SpeachPartType speachPart) { #if !NO_REDIS string key = GetListKey(language, speachPart); Db.KeyDelete(key); #endif }
public string FormatWord(string word, SpeachPartType speachPart) { switch (this._languageType) { case LanguageType.de: return word[0].ToString().ToUpper() + word.Substring(1, word.Length - 1).ToLower(); default: return word.ToLower(); } }
public static async Task<List<string>> GetRandomWordsAsync(LanguageType language, SpeachPartType speachPart, int count) { #if !NO_REDIS var key = GetListKey(language, speachPart); int listSize = (int)(await Db.ListLengthAsync(key)); var startIndex = listSize > count ? new Random().Next(listSize - count) : 0; return Db.ListRange(key, startIndex, startIndex + count).Select(v => v.ToString()).ToList<string>(); #endif return null; }
public string FormatWord(string word, SpeachPartType speachPart) { switch (this._languageType) { case LanguageType.de: return(word[0].ToString().ToUpper() + word.Substring(1, word.Length - 1).ToLower()); default: return(word.ToLower()); } }
private async Task AddRandomWordsAsync(List<dynamic> words, LanguageType language, SpeachPartType speachPart, int count) { #if !NO_REDIS List<string> list = await Redis.GetRandomWordsAsync(language, speachPart, count); foreach (string word in list) { words.Add(new { id = ObjectId.GenerateNewId().ToString(), language = language.ToString(), speachPart = (int)speachPart, word = word }); } #endif }
private async Task AddRandomWordsAsync(List <dynamic> words, LanguageType language, SpeachPartType speachPart, int count) { #if !NO_REDIS List <string> list = await Redis.GetRandomWordsAsync(language, speachPart, count); foreach (string word in list) { words.Add(new { id = ObjectId.GenerateNewId().ToString(), language = language.ToString(), speachPart = (int)speachPart, word = word }); } #endif }
private static string GetListKey(LanguageType language, SpeachPartType speachPart) { return language.ToString() + "_" + speachPart.ToString(); }
public static async Task <List <string> > GetRandomWordsAsync(LanguageType language, SpeachPartType speachPart, int count) { #if !NO_REDIS var key = GetListKey(language, speachPart); int listSize = (int)(await Db.ListLengthAsync(key)); var startIndex = listSize > count ? new Random().Next(listSize - count) : 0; return(Db.ListRange(key, startIndex, startIndex + count).Select(v => v.ToString()).ToList <string>()); #endif return(null); }
private static string GetListKey(LanguageType language, SpeachPartType speachPart) { return(language.ToString() + "_" + speachPart.ToString()); }
// POST api/Translation public async Task <IHttpActionResult> PostTranslation(TranslationData data) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } LanguageType wordLanguage; LanguageType translationLanguage; if (!Enum.TryParse <LanguageType>(data.wordLanguage, out wordLanguage)) { return(BadRequest("Invalid word language")); } if (!Enum.TryParse <LanguageType>(data.translationLanguage, out translationLanguage)) { return(BadRequest("Invalid translation language")); } var user = this.GetUser(); // . add word SpeachPartType sp = GetSpeachPart(data.translationWords, data.translationWord); LanguageFormatter languageFormatter = new LanguageFormatter(wordLanguage); Word word = await db.AddWord(languageFormatter.FormatWord(data.word, sp), wordLanguage, data.wordPronunciation, data.wordSoundUrls, data.wordPictureUrls); // . add word translations to mongo await mongoDb.AddTranslations( data.word.ToLower(), data.wordLanguage, data.translationLanguage, data.translationWords, data.translationCards); // . if book id is not specified - add translation to 'Cool Translator' book UserBook userBook; if (data.bookId == 0) { userBook = await db.GetCTUserBook(User.Identity.GetUserId(), wordLanguage); data.bookId = userBook.BookId; } else { userBook = await db.FindUserBookAsync(user.Id, data.bookId); if (userBook == null) { return(BadRequest("Invalid bookId")); } if (!userBook.Book.CanBeUpdatedBy(user.Id)) { return(BadRequest(string.Format("User {0} is not author of the book", user.DisplayName))); } } // . add translation Tuple <BookWord, Translation> bwt = await db.AddTranslation(data.bookId, word.Id, data.translationWord, translationLanguage, sp); Redis.PushWord(wordLanguage, sp, word.Value); Redis.PushWord(translationLanguage, sp, bwt.Item2.Value); var userBookDto = new UserBookDto(userBook); return(CreatedAtRoute("DefaultApi", new { id = bwt.Item2.Id }, new { userBook = userBookDto, book = userBookDto.BookDto, word = new WordDto(word), bookWord = new BookWordDto(bwt.Item1), translation = new TranslationDto(bwt.Item2) })); } catch (Exception e) { _logger.Error(e, "Unable to add translation"); throw; } }
public async Task <Tuple <BookWord, Translation> > AddTranslation(int bookID, int wordID, string value, LanguageType language, SpeachPartType speachPart) { // . search for BookWord BookWord bookWordEntity = await BookWords.Include("Translations").SingleOrDefaultAsync(bw => bw.BookId == bookID && bw.WordId == wordID && bw.SpeachPart == (int)speachPart); Translation translationEntity = null; if (bookWordEntity == null) { // . create BookWord if not exists bookWordEntity = new BookWord { BookId = bookID, WordId = wordID, SpeachPart = (int)speachPart }; BookWords.Add(bookWordEntity); await SaveChangesAsync(); } else { // . search for Translation translationEntity = bookWordEntity.Translations.SingleOrDefault(t => t.Value == value && t.Language == (int)language); } // . add Translation if not exists if (translationEntity == null) { translationEntity = new Translation { BookWordId = bookWordEntity.Id, Value = value, Language = (byte)language }; Translations.Add(translationEntity); await SaveChangesAsync(); } return(new Tuple <BookWord, Translation>(bookWordEntity, translationEntity)); }