コード例 #1
0
        public static async Task UpdateSearchIndex <T>(T entityToUpdate, CrudAction action) where T : Entity, new()
        {
            if (_updateSearchService)
            {
                if (entityToUpdate is ISearcheable searchable)
                {
                    var indexObject = searchable.GetIndexObject();
                    if (indexObject != null)
                    {
                        var targetType = typeof(T).Name.ToLower();
                        try
                        {
                            StringResponse response;
                            switch (action)
                            {
                            case CrudAction.Create:
                            {
                                indexObject.CreatedAt = DateTime.Now;
                                response = await _lowlevelClient.IndexAsync <StringResponse>(targetType, targetType,
                                                                                             indexObject.Id, PostData.Serializable(indexObject));
                            }
                            break;

                            case CrudAction.Update:
                            {
                                indexObject.UpdatedAt = DateTime.Now;
                                response =
                                    await _lowlevelClient.IndexPutAsync <StringResponse>(targetType, targetType,
                                                                                         indexObject.Id, PostData.Serializable(indexObject));

                                break;
                            }

                            case CrudAction.Delete:
                            {
                                response =
                                    await _lowlevelClient.DeleteAsync <StringResponse>(targetType, targetType,
                                                                                       indexObject.Id);

                                break;
                            }
                            }
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
            }
        }