Ejemplo n.º 1
0
        public async Task <APIResponse> PostRecordToCollection(string collectionName, string id, [FromBody] ExpandoObject payload)
        {
            _logger.LogInformation($"PostRecordToCollection request recieved");

            id = id.Replace("[NewGuid]", Guid.NewGuid().ToString());

            var payloadDictionary = (IDictionary <String, object>)payload;

            if (!payloadDictionary.ContainsKey("timestamp"))
            {
                payloadDictionary.Add("timestamp", DateTime.UtcNow);
            }

            if (payloadDictionary.ContainsKey("id"))
            {
                payloadDictionary["id"] = id;
            }
            else
            {
                payloadDictionary.Add("id", id);
            }

            if (payloadDictionary.ContainsKey("passwordToHash"))
            {
                var passwordToHash = payloadDictionary["passwordToHash"].ToString();
                var hashedPassword = Argon2.Hash(passwordToHash);
                payloadDictionary.Remove("passwordToHash");
                payloadDictionary.Add("hashedPassword", hashedPassword);
            }

            var collection = await GetCollection(collectionName).ConfigureAwait(false);

            if (collection == null)
            {
                return(APIResponse.NotOk(Request.ToRequestString(), $"Cant find collection with name {collectionName}", HttpStatusCode.BadRequest));
            }

            var response = await _elasticBand.Index <object>(collection.Index, payload, id).ConfigureAwait(false);

            if (response.Ok)
            {
                return new APIResponse
                       {
                           Request    = Request.ToRequestString(),
                           Ok         = true,
                           Result     = response.Result,
                           StatusCode = response.StatusCode,
                           Data       = $"{collection.Index}/_doc/{response.Id}"
                       }
            }
            ;

            return(APIResponse.NotOk(Request.ToRequestString(), "Failed to index data", HttpStatusCode.BadRequest));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Index an object(T) into Elasticsearch (create or update).
 /// </summary>
 /// <param name="id">A string containing the id of the document, if empty a new Guid will be generated.</param>
 /// <param name="data">The object to index.</param>
 /// <returns>An ElasticBandResponse</returns>
 public async Task <ElasticBandResponse <T> > Index(string id, T data)
 {
     return(await _elasticBand.Index(IndexName, data, id));
 }