1- using System ;
2- using System . Collections . Generic ;
3- using System . IO ;
4- using System . Linq ;
5- using System . Threading . Tasks ;
6- using AzureFunctions . OidcAuthentication ;
1+ using AzureFunctions . OidcAuthentication ;
72using Microsoft . AspNetCore . Http ;
83using Microsoft . AspNetCore . Mvc ;
94using Microsoft . Azure . Documents ;
138using Microsoft . Azure . WebJobs . Extensions . Http ;
149using Microsoft . Extensions . Logging ;
1510using Newtonsoft . Json ;
11+ using System ;
12+ using System . Collections . Generic ;
13+ using System . IO ;
14+ using System . Linq ;
15+ using System . Threading . Tasks ;
1616using TwoWeeksReady . Common ;
1717using TwoWeeksReady . Common . FamilyPlans ;
1818
@@ -128,8 +128,9 @@ public async Task<IActionResult> UpsertFamilyPlan(
128128
129129 [ FunctionName ( "familyplans-delete" ) ]
130130 public async Task < IActionResult > DeleteFamilyPlan (
131- [ HttpTrigger ( AuthorizationLevel . Anonymous , "delete" , Route = null ) ]
131+ [ HttpTrigger ( AuthorizationLevel . Anonymous , "delete" , Route = "familyplans-delete/{id}" ) ]
132132 HttpRequest req ,
133+ string id ,
133134 [ CosmosDB ( databaseName : DefaultDatabaseName ,
134135 collectionName : CollectionName ,
135136 ConnectionStringSetting = DefaultDbConnectionName ) ]
@@ -141,26 +142,34 @@ public async Task<IActionResult> DeleteFamilyPlan(
141142
142143 log . LogInformation ( $ "Deleting a Family Plan") ;
143144
144- try
145- {
146- var content = await new StreamReader ( req . Body ) . ReadToEndAsync ( ) ;
147- var thePlan = JsonConvert . DeserializeObject < FamilyPlan > ( content ) ;
145+ Uri collectionUri = UriFactory . CreateDocumentCollectionUri ( DefaultDatabaseName , CollectionName ) ;
148146
149- Uri collectionUri = UriFactory . CreateDocumentCollectionUri ( DefaultDatabaseName , CollectionName ) ;
150147
151- // Update Plan
152- var docUri = UriFactory . CreateDocumentUri ( DefaultDatabaseName , CollectionName , thePlan . Id ) ;
153- Document document = await client . DeleteDocumentAsync ( docUri ) ;
154148
155- return new OkResult ( ) ;
149+ //verify existing document (not upserting as this is an update only function)
150+ var options = new FeedOptions ( )
151+ {
152+ EnableCrossPartitionQuery = true
153+ } ;
154+ var existingDocument = client . CreateDocumentQuery < FamilyPlan > ( collectionUri , options )
155+ . Where ( d => d . Id == id )
156+ . AsEnumerable ( ) . FirstOrDefault ( ) ;
156157
157- }
158- catch ( Exception ex )
158+ if ( existingDocument == null )
159159 {
160- log . LogCritical ( $ "Failed to delete the new Family Plan: { ex } ") ;
160+ log . LogWarning ( $ "{ id } not found.") ;
161+ return new BadRequestObjectResult ( $ "Family Plan not found.") ;
161162 }
162163
163- return new BadRequestResult ( ) ;
164+ var documentUri = UriFactory . CreateDocumentUri ( DefaultDatabaseName , CollectionName , id ) ;
165+
166+ await client . DeleteDocumentAsync ( documentUri , new RequestOptions
167+ {
168+ PartitionKey = new PartitionKey ( existingDocument . UserId )
169+ } ) ;
170+
171+ return new OkObjectResult ( true ) ;
172+
164173 }
165174
166175 }
0 commit comments