Esempio n. 1
0
 public static Pron ToPron(Db.Pron pron)
 {
     return(new Pron
     {
         Name = ResourceNames.ToPronName(new PronRef {
             WordId = pron.WordId, PronId = pron.PronId
         }),
         Pronunciation = pron.Pronunciation,
         Variant = DbVariantToProtoVariant[pron.Variant ?? Db.Variant.UNSPECIFIED],
         SandhiCategory = DbSCToProtoSC[pron.SandhiCategory ?? Db.SandhiCategory.UNSPECIFIED],
         Weight = pron.Weight ?? 0,
     });
 }
        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));
        }