Exemplo n.º 1
0
        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));
        }
Exemplo n.º 3
0
        public async override Task <Yngdieng.Admin.V1.Protos.Pron> UpdatePron(UpdatePronRequest request,
                                                                              ServerCallContext context)
        {
            if (request.Pron == null)
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "pron must be set"));
            }
            if (string.IsNullOrEmpty(request.Pron.Name))
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "pron.name must not be empty"));
            }
            var pronRef = ResourceNames.ToPronRef(request.Pron.Name);

            Db.Pron pron;
            try
            {
                pron = await _dbContext.Prons.Where(p => p.WordId == pronRef.WordId && p.PronId == pronRef.PronId)
                       .SingleAsync();
            }
            catch (Exception e)
            {
                throw new RpcException(new Status(StatusCode.NotFound, $"Pron {request.Pron.Name} does not exist", e));
            }
            if (request.UpdateMask.Paths.Contains("pronunciation"))
            {
                pron.Pronunciation = request.Pron.Pronunciation;
            }
            if (request.UpdateMask.Paths.Contains("weight"))
            {
                pron.Weight = request.Pron.Weight;
            }
            if (request.UpdateMask.Paths.Contains("sandhi_category"))
            {
                pron.SandhiCategory = ProtoSCToDbSC[request.Pron.SandhiCategory];
            }
            if (request.UpdateMask.Paths.Contains("variant"))
            {
                pron.Variant = ProtoVariantToDbVariant[request.Pron.Variant];
            }
            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                throw new RpcException(new Status(StatusCode.FailedPrecondition, "database error", e));
            }
            return(Renderers.ToPron(pron));
        }
Exemplo n.º 4
0
        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()
                       ));
        }
 public async override Task <BatchGetPronsResponse> BatchGetProns(BatchGetPronsRequest request,
                                                                  ServerCallContext context)
 {
     if (string.IsNullOrEmpty(request.Parent))
     {
         throw new RpcException(new Status(StatusCode.InvalidArgument, "parent must not be empty"));
     }
     return(new BatchGetPronsResponse
     {
         Prons =
         {
             request.Names.Select(x => ResourceNames.ToPronRef(x))
             .SelectMany(pRef => _dbContext.Prons.Where(pron => pron.WordId == pRef.WordId &&
                                                        pron.PronId == pRef.PronId))
             .Select(dbP => Renderers.ToPron(dbP))
         }
     });
 }
        public async override Task <Yngdieng.Admin.V1.Protos.Pron> CreatePron(CreatePronRequest request,
                                                                              ServerCallContext context)
        {
            if (request.Pron == null)
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "pron must be set"));
            }
            if (string.IsNullOrEmpty(request.Pron.Pronunciation))
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "pron.pronunciation must not be empty"));
            }
            var wordRef = ResourceNames.ToWordRef(request.Parent);

            if (!await _dbContext.Words.Where(w => w.WordId == wordRef.WordId).AnyAsync())
            {
                throw new RpcException(new Status(StatusCode.FailedPrecondition, $"Word {request.Parent} does not exist"));
            }
            var newPron = new Db.Pron
            {
                WordId         = wordRef.WordId,
                Pronunciation  = request.Pron.Pronunciation,
                Weight         = request.Pron.Weight,
                SandhiCategory = ProtoSCToDbSC[request.Pron.SandhiCategory],
                Variant        = ProtoVariantToDbVariant[request.Pron.Variant],
            };

            _dbContext.Prons.Add(newPron);
            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                throw new RpcException(new Status(StatusCode.FailedPrecondition, "database error", e));
            }
            return(Renderers.ToPron(newPron));
        }