Exemplo n.º 1
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));
        }
 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 <MyEmpty> DeletePron(DeletePronRequest request,
                                                        ServerCallContext context)
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                throw new RpcException(new Status(StatusCode.InvalidArgument, "parent name not be empty"));
            }
            var pronRef = ResourceNames.ToPronRef(request.Name);

            _dbContext.Remove(
                await _dbContext.Prons.Where(p => p.WordId == pronRef.WordId &&
                                             p.PronId == pronRef.PronId).SingleOrDefaultAsync()
                );
            await _dbContext.SaveChangesAsync();

            return(new MyEmpty
            {
            });
        }