Example #1
0
        public async override Task <Yngdieng.Frontend.V3.Protos.Word> GetWord(GetWordRequest request,
                                                                              ServerCallContext context)
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "name must not be empty"));
            }
            var docId = ResourceNames.ToDocId(request.Name);

            if (_indexHolder.GetIndex().DocIdRedirections.ContainsKey(docId))
            {
                var redirectionTarget = _indexHolder.GetIndex().DocIdRedirections[docId];
                _logger.LogInformation($"DocId Redirection: {docId} -> {redirectionTarget}");
                docId = redirectionTarget;
            }
            DocRef docRef;

            try
            {
                docRef = DocRefs.Decode(docId);
            }
            catch (Exception e)
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, $"{request.Name} is not a valid name.", e));
            }
            var userPreference = UserPreferences.FromContext(context);
            var zhConverter    = new ZhConverter(_openCc, userPreference.ZhConversionPreference);

            return(await Words.GetWord(_indexHolder, _dbContext, zhConverter, docRef));
        }
Example #2
0
 public static Yngdieng.Frontend.V3.Protos.WordList ToWordList(Db.WordList wordList)
 {
     return(new Yngdieng.Frontend.V3.Protos.WordList()
     {
         Name = ResourceNames.ToWordListName(wordList.WordListId),
         Title = wordList.Title,
         Description = wordList.Description,
         Upvotes = 123,
     });
 }
Example #3
0
        public static async Task <Word> GetWord(
            IIndexHolder _indexHolder,
            AdminContext _dbContext,
            ZhConverter zhConverter,
            DocRef docRef,
            Mode mode = Mode.Full)
        {
            var maybeYngdiengDocument = _indexHolder.GetIndex().YngdiengDocuments
                                        .SingleOrDefault(yDoc => yDoc.DocId == DocRefs.Encode(docRef));
            var maybeWordId = string.IsNullOrEmpty(docRef.ZingzeuId)
                ? (int?)null : int.Parse(docRef.ZingzeuId, NumberStyles.HexNumber);
            var maybeWord = maybeWordId == null
                ? null
                : await _dbContext.Words
                            .Where(w => w.WordId == maybeWordId)
                            .Include(w => w.PreferredSandhiAudio)
                            .SingleOrDefaultAsync();

            if (maybeYngdiengDocument == null && maybeWord == null)
            {
                throw new RpcException(new Status(StatusCode.NotFound, "Not found."));
            }
            var extensions = await _dbContext.Extensions.Where(e => e.WordId == maybeWordId).ToListAsync();

            var prons = await _dbContext.Prons.Where(p => p.WordId == maybeWordId).ToListAsync();

            var hanzi            = zhConverter.tH(await GetHanzi(_dbContext, maybeYngdiengDocument, maybeWordId));
            var recommendedProns = mode == Mode.Snippet
                ? PronChooser.GetSnippetPronunciations(maybeYngdiengDocument, prons, maybeWord?.PreferredSandhiAudio)
                : PronChooser.GetRecommendedPronunciations(maybeYngdiengDocument, prons, maybeWord?.PreferredSandhiAudio);
            var explanations = mode == Mode.Snippet
                ? Enumerable.Empty <RichTextNode>()
                : GetExplanations(zhConverter, hanzi, maybeYngdiengDocument, extensions);
            var audioCards = mode == Mode.Snippet ? Enumerable.Empty <Word.Types.AudioCard>()
                : await GetAudioCards(_dbContext, recommendedProns, maybeWordId);

            var wordLists = mode == Mode.Full && maybeWordId.HasValue
                ? await Queries.QueryWordListsByWordId(_dbContext, maybeWordId.Value).Select(wl => Renderers.ToWordList(wl)).ToListAsync()
                : Enumerable.Empty <WordList>();

            return(new Word
            {
                Name = ResourceNames.ToWordName(docRef),
                Hanzi = hanzi,
                Pronunciations = { recommendedProns },
                Explanation = { explanations },
                Snippet = zhConverter.tH(GetSnippet(maybeYngdiengDocument, extensions)),
                AudioCards = { audioCards },
                WordLists =
                {
                    wordLists
                }
            });
        }
Example #4
0
        public async override Task <Yngdieng.Frontend.V3.Protos.WordList> GetWordList(GetWordListRequest request,
                                                                                      ServerCallContext context)
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "name must not be empty"));
            }
            var wordListId = ResourceNames.ToWordListId(request.Name);

            return(Renderers.ToWordList(await _dbContext.WordLists.Where(w => w.WordListId == wordListId).SingleAsync()));
        }