public async Task <APIResponse> Get(string name) { _logger.LogInformation($"get request for id {name} recieved"); var id = CollectionDefinition.BuildId(name); var response = await _collectionRepository.GetById(id).ConfigureAwait(false); response.Data.ExampleJsonObject = JsonSerializer.Deserialize <object>(response.Data.ExampleJsonObjectString); if (response.Ok) { _logger.LogInformation($"fetched collection {response.Id}"); return(new APIResponse { Request = Request.ToRequestString(), Ok = true, Result = "found", Data = response.Data }); } _logger.LogInformation($"unable to fetch collection( for {id}"); return(APIResponse.NotOk(Request.ToRequestString(), "unable to fetch data from elasticsearch", HttpStatusCode.NotFound, response.Id)); }
public async Task <APIResponse> Post(string name, string exampleIdPattern, [FromBody] ExpandoObject exampleJsonObject) { _logger.LogInformation($"post request recieved"); var collectionDefinition = new CollectionDefinition { Name = name.ToLower(), ExampleIdPattern = exampleIdPattern, ExampleJsonObjectString = JsonSerializer.Serialize <object>(exampleJsonObject, new JsonSerializerOptions { WriteIndented = true }) }; var response = await _collectionRepository.Index(collectionDefinition.Id, collectionDefinition).ConfigureAwait(false); if (response.Ok) { _logger.LogInformation($"posted collection {response.Result} {response.Id}"); return(new APIResponse { Request = Request.ToRequestString(), Ok = true, Result = response.Result, StatusCode = response.StatusCode, Data = response.Id }); } _logger.LogInformation($"unable to post collection to elasticsearch {response.StatusCode}"); return(APIResponse.NotOk(Request.ToRequestString(), "unable to post data to elasticsearch", HttpStatusCode.BadRequest, response.Id)); }
public async Task <APIResponse> Get() { _logger.LogInformation($"get request recieved"); var response = await _collectionRepository.Query("").ConfigureAwait(false); foreach (var item in response.Data) { item.ExampleJsonObject = JsonSerializer.Deserialize <object>(item.ExampleJsonObjectString); } if (response.Ok) { _logger.LogInformation($"fetched all {response.Data.Count} collection(s)"); return(new APIResponse { Request = Request.ToRequestString(), Ok = true, Result = "found", Data = response.Data }); } _logger.LogInformation($"no collections to fetch"); return(APIResponse.NotOk(Request.ToRequestString(), "unable to fetch data to elasticsearch", HttpStatusCode.NoContent, response.Id)); }
public async Task <APIResponse> DeleteRecordById(string collectionName, string id) { _logger.LogInformation($"DeleteRecordById request recieved"); 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.Delete(collection.Index, id).ConfigureAwait(false); if (response.Ok) { return new APIResponse { Request = Request.ToRequestString(), Ok = true, Result = "deleted", Data = id } } ; return(APIResponse.NotOk(Request.ToRequestString(), "Failed to delete record", HttpStatusCode.NotFound)); }
public async Task <APIResponse> GetRecordByQuery(string collectionName, string searchString, [FromBody] object query) { _logger.LogInformation($"GetRecordByQuery request recieved"); var collection = await GetCollection(collectionName).ConfigureAwait(false); if (collection == null) { return(APIResponse.NotOk(Request.ToRequestString(), $"Cant find collection with name {collectionName}", HttpStatusCode.BadRequest)); } // json query from body should override search string. var queryAsString = JsonSerializer.Serialize(query); if (queryAsString != "{}") { searchString = queryAsString; } var response = await _elasticBand.Query <object>(collection.Index, searchString).ConfigureAwait(false); if (response.Ok) { return new APIResponse { Request = Request.ToRequestString(), Ok = true, Result = response.Data.Count == 0 ? "not found": "found", Data = response.Data } } ; return(APIResponse.NotOk(Request.ToRequestString(), "Failed to return any data", HttpStatusCode.NotFound)); }
public async Task <APIResponse> GetAllRecords(string collectionName) { _logger.LogInformation($"GetAllRecords request recieved"); 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.Query <object>(collection.Index, "").ConfigureAwait(false); if (response.Ok) { return new APIResponse { Request = Request.ToRequestString(), Ok = true, Result = "found", Data = response.Data } } ; return(APIResponse.NotOk(Request.ToRequestString(), "No data or index doesn't exist", HttpStatusCode.NotFound)); }
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)); }
public async Task <APIResponse> Delete(string name) { var id = CollectionDefinition.BuildId(name); _logger.LogInformation($"delete request recieved for id {id}"); var collectionResponse = await _collectionRepository.GetById(id); if (!collectionResponse.Ok) { return(APIResponse.NotOk(Request.ToRequestString(), $"unable to delete data from elasticsearch, collection {name} not found", HttpStatusCode.NotFound)); } await _collectionRepository.GetElasticBand().GetClient().DeleteAsync(collectionResponse.Data.Index); _logger.LogInformation($"deleted index {collectionResponse.Data.Index}"); var deleteResponse = await _collectionRepository.Delete(id).ConfigureAwait(false); if (deleteResponse.Ok) { _logger.LogInformation($"deleted collection {name}"); return(new APIResponse { Request = Request.ToRequestString(), Ok = true, Result = "deleted", Data = deleteResponse.Id }); } _logger.LogInformation($"unable to delete collection with id {name} {deleteResponse.StatusCode}"); return(APIResponse.NotOk(Request.ToRequestString(), "unable to delete data from elasticsearch", HttpStatusCode.NotFound, deleteResponse.Id)); // TODO could do with logging helper which creates a trace id and logs request and response data automatically? // or stores up messages and appends them all as one log per request?? }