public async override Task <ListWordsResponse> ListWords(ListWordsRequest request,
                                                                 ServerCallContext context)
        {
            if (request.PageSize <= 0)
            {
                request.PageSize = DefaultPageSize;
            }
            if (request.PageSize > MaxPageSize)
            {
                request.PageSize = MaxPageSize;
            }
            if (request.Offset < 0)
            {
                request.Offset = 0;
            }
            var count = await _dbContext.Words.CountAsync();

            var result = _dbContext.WordsWithPronIds.FromSqlRaw(@"
                SELECT words.*,  ARRAY(
	                SELECT pron_id FROM prons WHERE prons.word_id = words.word_id
                ) as pron_ids FROM words
                ORDER BY words.word_id ASC")
                         .Skip(request.Offset)
                         .Take(request.PageSize)
                         .Select(x => Renderers.ToWord(x));

            return(new ListWordsResponse
            {
                TotalSize = count,
                Words = { await result.ToListAsync() }
            });
        }
        public async override Task <Yngdieng.Admin.V1.Protos.Word> CreateWord(CreateWordRequest request,
                                                                              ServerCallContext context)
        {
            int presetWordId = -1;

            if (!string.IsNullOrEmpty(request.WordId))
            {
                try
                {
                    presetWordId = int.Parse(request.WordId, System.Globalization.NumberStyles.HexNumber);
                }
                catch (ArgumentException e)
                {
                    throw new RpcException(new Status(StatusCode.InvalidArgument, "Invalid word_id", e));
                }
                catch (FormatException e)
                {
                    throw new RpcException(new Status(StatusCode.InvalidArgument, "Invalid word_id", e));
                }
            }
            if (request.Word == null)
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "word must be set"));
            }
            if (string.IsNullOrEmpty(request.Word.Hanzi))
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "word.hanzi must not be empty"));
            }
            if (request.Word.Extensions.Count > 0)
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "word.extensions must not be set on create"));
            }
            if (request.Word.Prons.Count > 0)
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "word.prons must not be set on create"));
            }
            var newWord = new Db.Word
            {
                Hanzi             = request.Word.Hanzi,
                HanziAlternatives = new List <string>(request.Word.HanziAlternatives),
                MandarinWords     = new List <string>(request.Word.MandarinWords),
                Gloss             = string.IsNullOrEmpty(request.Word.Gloss) ? null : request.Word.Gloss,
            };

            if (presetWordId > 0)
            {
                newWord.WordId = presetWordId;
            }

            _dbContext.Words.Add(newWord);
            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                throw new RpcException(new Status(StatusCode.FailedPrecondition, "word id already exists", e));
            }
            return(Renderers.ToWord(newWord));
        }
        public async override Task <Yngdieng.Admin.V1.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 wordRef = ResourceNames.ToWordRef(request.Name);

            return(Renderers.ToWord(
                       await _dbContext.Words.Where(w => w.WordId == wordRef.WordId).SingleAsync(),
                       await _dbContext.Prons.Where(p => p.WordId == wordRef.WordId)
                       .Select(p => new PronRef {
                WordId = wordRef.WordId, PronId = p.PronId
            })
                       .ToListAsync()
                       ));
        }