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 static Word ToWord(Db.Word word, IEnumerable <PronRef> pronRefs) { return(new Word { Name = ResourceNames.ToWordName(new WordRef { WordId = word.WordId }), Hanzi = word.Hanzi, HanziAlternatives = { word.HanziAlternatives }, MandarinWords = { word.MandarinWords }, Gloss = word.Gloss ?? string.Empty, Prons = { pronRefs.Select(p => ResourceNames.ToPronName(p)) } }); }
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 <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 <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 { }); }
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)); }