示例#1
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()));
        }
示例#2
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
                }
            });
        }