public static HttpResponseMessage Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "todo/{id?}")] HttpRequestMessage req, string id, ILogger log) { try { var query = string.IsNullOrEmpty(id) ? "SELECT * FROM d" : $"SELECT * FROM d WHERE d.id = '{id}'"; var collectionLink = UriFactory.CreateDocumentCollectionUri(Constants.DatabaseId, Constants.CollectionId); using var client = DocumentDbClient.GetClient(); var feedOptions = new FeedOptions { EnableCrossPartitionQuery = true }; var results = client.CreateDocumentQuery <Todo>(collectionLink, query, feedOptions).ToList(); return(req.CreateResponse(HttpStatusCode.OK, results)); } catch (Exception ex) { log.LogError(ex, $"Failed to get todo documents. {ex.Message}"); return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "todo")] HttpRequestMessage req, ILogger log) { try { var todo = await req.Content.ReadAsAsync <Todo>(); if (string.IsNullOrEmpty(todo?.Task)) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Task value was not provided.")); } todo.Id = Guid.NewGuid(); var collectionLink = UriFactory.CreateDocumentCollectionUri(Constants.DatabaseId, Constants.CollectionId); using var client = DocumentDbClient.GetClient(); await client.CreateDocumentAsync(collectionLink, todo, disableAutomaticIdGeneration : true); return(req.CreateResponse(HttpStatusCode.Created, todo)); } catch (Exception ex) { log.LogError(ex, $"Failed to create todo document. {ex.Message}"); return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "todo/{id}")] HttpRequestMessage req, string id, ILogger log) { try { if (string.IsNullOrEmpty(id)) { return(req.CreateResponse(HttpStatusCode.OK)); } var todo = await req.Content.ReadAsAsync <Todo>(); todo.Id = Guid.Parse(id); var collectionLink = UriFactory.CreateDocumentCollectionUri(Constants.DatabaseId, Constants.CollectionId); using var client = DocumentDbClient.GetClient(); await client.UpsertDocumentAsync(collectionLink, todo); return(req.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { log.LogError(ex, $"Failed to update todo document. {ex.Message}"); return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "todo/{id}")] HttpRequestMessage req, string id, ILogger log) { try { if (string.IsNullOrEmpty(id)) { return(req.CreateResponse(HttpStatusCode.OK)); } var documentLink = UriFactory.CreateDocumentUri(Constants.DatabaseId, Constants.CollectionId, id); using var client = DocumentDbClient.GetClient(); var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(id) }; await client.DeleteDocumentAsync(documentLink, requestOptions); return(req.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { if (ex is DocumentClientException dce && string.Equals(dce.Error?.Code, "NotFound", StringComparison.InvariantCultureIgnoreCase)) { return(req.CreateResponse(HttpStatusCode.OK)); } log.LogError(ex, $"Failed to delete todo document. {ex.Message}"); return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }