public async Task <ActionResult> IndexAsync([FromBody] InsertUpdateModel <DemoEntity> insertUpdateModel) { if (insertUpdateModel == null) { return(BadRequest(MessageFactory.CreateParamsIsNullMessage())); } var result = await commandDemoService.IndexAsync(insertUpdateModel).ConfigureAwait(false); return(Ok(result)); }
public async Task <ActionResult> UpdateByIdAsync(long id, [FromBody] InsertUpdateModel <DemoEntity> insertUpdateModel) { if (insertUpdateModel == null) { return(BadRequest(MessageFactory.CreateParamsIsNullMessage())); } insertUpdateModel.Entity.Id = id; var result = await commandDemoService.UpdateAsync(id, insertUpdateModel).ConfigureAwait(false); return(Ok(result)); }
/// <summary> /// 新增单条索引记录 /// </summary> /// <param name="entity"></param> /// <param name="routing"></param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > IndexAsync <T>(InsertUpdateModel <T> insertUpdateModel) where T : class, new() { HttpResponseResultModel <bool> httpResponseResultModel = new HttpResponseResultModel <bool>(); string indexName = insertUpdateModel.IndexName?.ToLower(); if (string.IsNullOrEmpty(indexName)) { indexName = PocoIndexName; } IndexRequest <T> indexRequest = new IndexRequest <T>(insertUpdateModel.Entity, indexName); if (!string.IsNullOrEmpty(insertUpdateModel.Routing)) { indexRequest.Routing = insertUpdateModel.Routing; } var result = await client.IndexAsync(indexRequest).ConfigureAwait(false); GetDebugInfo(result); httpResponseResultModel.IsSuccess = result.IsValid; return(httpResponseResultModel); }
/// <summary> /// 批量索引 /// </summary> /// <param name="entities">实体集合</param> /// <param name="routing">路由键</param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > IndexBatchAsync <T>(InsertUpdateModel <IList <T> > insertUpdateModel) where T : class, new() { HttpResponseResultModel <bool> httpResponseResultModel = new HttpResponseResultModel <bool>(); string indexName = insertUpdateModel.IndexName?.ToLower(); if (string.IsNullOrEmpty(indexName)) { indexName = PocoIndexName; } BulkDescriptor bulkDescriptor = new BulkDescriptor(); bulkDescriptor.IndexMany(insertUpdateModel.Entity); bulkDescriptor.Index(indexName); if (!string.IsNullOrEmpty(insertUpdateModel.Routing)) { bulkDescriptor.Routing(new Routing(insertUpdateModel.Routing)); } var result = await client.BulkAsync(bulkDescriptor).ConfigureAwait(false); GetDebugInfo(result); //if (result.Errors || !result.ItemsWithErrors.IsNullOrEmpty()) //{ // Task.Run(() => // { // var json = JsonHelper.SerializeObject(insertUpdateModel.Entity); // WriteLog.WriteLogsAsync(json, "batchError"); // }); //} var isSuccess = result.ItemsWithErrors.IsListNullOrEmpty(); string errorMessage = ""; if (!isSuccess) { var errorIdList = result.ItemsWithErrors.Select(t => t.Id); errorMessage = string.Join(",", errorIdList); } httpResponseResultModel.IsSuccess = isSuccess; httpResponseResultModel.ErrorMessage = errorMessage; return(httpResponseResultModel); }
/// <summary> /// 根据id更新部分实体字段 /// </summary> /// <typeparam name="TPartialDocument">包含实体T中部分字段的是model</typeparam> /// <param name="id">主键</param> /// <param name="partEntity">部分实体model</param> /// <param name="routing">路由键</param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > UpdatePartialDocumentAsync <TPartialDocument, TPrimaryKeyType>(TPrimaryKeyType id, InsertUpdateModel <TPartialDocument> insertUpdateModel) where TPartialDocument : class, new() { HttpResponseResultModel <bool> httpResponseResultModel = new HttpResponseResultModel <bool>(); string indexName = insertUpdateModel.IndexName?.ToLower(); if (string.IsNullOrEmpty(indexName)) { indexName = PocoIndexName; } UpdateDescriptor <TEntity, TPartialDocument> updRequest = new UpdateDescriptor <TEntity, TPartialDocument>(indexName, id.ToString()); updRequest.Doc(insertUpdateModel.Entity); updRequest.Index(indexName); if (!string.IsNullOrEmpty(insertUpdateModel.Routing)) { updRequest.Routing(insertUpdateModel.Routing); } var response = await client.UpdateAsync <TEntity, TPartialDocument>(updRequest).ConfigureAwait(false); GetDebugInfo(response); httpResponseResultModel.IsSuccess = (response.Result == Result.Updated); return(httpResponseResultModel); }
/// <summary> /// 根据id更新部分实体字段 /// </summary> /// <typeparam name="TPartialDocument">包含实体T中部分字段的是model</typeparam> /// <param name="id">主键</param> /// <param name="insertUpdateModel"></param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > UpdatePartialDocumentAsync(long id, InsertUpdateModel <PartOfDemoEntity> insertUpdateModel) { return(await repository.UpdatePartialDocumentAsync(id, insertUpdateModel).ConfigureAwait(false)); }
/// <summary> /// 批量插入实体 /// </summary> /// <param name="insertUpdateModel"></param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > IndexBatchAsync(InsertUpdateModel <IList <DemoEntity> > insertUpdateModel) { return(await repository.IndexBatchAsync(insertUpdateModel).ConfigureAwait(false)); }