예제 #1
0
        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));
            }
        }