public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Admin, "post", Route = null)] HttpRequest req, ILogger log) { var entries = req.Query["entries"]; try { DictDb.AddRange(MongoDB.Bson.Serialization.BsonSerializer.Deserialize <List <DictEntry> >(entries)); return(new OkResult()); } catch { return(new UnprocessableEntityObjectResult("Failed to parse")); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { String sourceLanguage = req.Query["sourceLanguage"]; if (sourceLanguage == null) { return(new UnprocessableEntityObjectResult(MISSING_PARAMETER_TEXT + "sourceLanguage")); } String targetLanguage = req.Query["targetLanguage"]; if (targetLanguage == null) { return(new UnprocessableEntityObjectResult(MISSING_PARAMETER_TEXT + "targetLanguage")); } String payload = req.Query["payload"]; if (payload == null) { return(new UnprocessableEntityObjectResult(MISSING_PARAMETER_TEXT + "payload")); } String requestorId = req.Query["requestorId"]; if (requestorId == null) { return(new UnprocessableEntityObjectResult(MISSING_PARAMETER_TEXT + "requestorId")); } else if (requestorId.Length > 255) { return(new UnprocessableEntityObjectResult("Payload length above 255 chars")); } // Lookup var result = await DictDb.FindTranslation(sourceLanguage, targetLanguage, payload); return(result != null ? (ActionResult) new OkObjectResult(result) : new NotFoundObjectResult("Could not find " + sourceLanguage + "->" + targetLanguage + ": " + payload)); }
private async void CsvToDb() { //读取csv文件,缺少异步方法,目前发现会阻塞,用Task避免 var config = new CsvConfiguration(CultureInfo.InvariantCulture) { PrepareHeaderForMatch = args => args.Header.ToLower(), }; using var reader = new StreamReader(CsvFileName); using var csv = new CsvReader(reader, config); var records = csv.GetRecords <Dict>(); //写入数据库 await using (var context = new Context()) { long rateTotal = 0; long rateNew = 0; long rateUpdate = 0; foreach (var dict in records) { rateTotal++; var oldDictDb = (from d in context.DictDbs where d.Word == dict.Word select d).FirstOrDefault(); if (oldDictDb is null) { var dictDb = new DictDb { Word = dict.Word, Audio = dict.Audio, Bnc = dict.Bnc, Collins = dict.Collins, Definition = dict.Definition, Translation = dict.Translation, Pos = dict.Pos, Detail = dict.Detail, Exchange = dict.Exchange, Frq = dict.Frq, Oxford = dict.Oxford, Tag = dict.Tag, Phonetic = dict.Phonetic, }; context.DictDbs.Add(dictDb); rateNew++; } else { oldDictDb.Word = dict.Word; oldDictDb.Audio = dict.Audio; oldDictDb.Bnc = dict.Bnc; oldDictDb.Collins = dict.Collins; oldDictDb.Definition = dict.Definition; oldDictDb.Translation = dict.Translation; oldDictDb.Pos = dict.Pos; oldDictDb.Detail = dict.Detail; oldDictDb.Exchange = dict.Exchange; oldDictDb.Frq = dict.Frq; oldDictDb.Oxford = dict.Oxford; oldDictDb.Tag = dict.Tag; oldDictDb.Phonetic = dict.Phonetic; rateUpdate++; } Rate = "新增:" + rateNew.ToString() + "更新:" + rateUpdate.ToString() + "总数:" + rateTotal.ToString(); } await context.SaveChangesAsync(); } MessageBox.Show("读取成功"); }