public async Task <ActionResult> Edit( [Bind(Include = "Id,Title,Description,CategoryId,ImageUrl,ThumbnailUrl,Price,Phone,Posted")] Ad ad, //ImageUrl,ThumbnailUrl, HttpPostedFileBase imageFile) { if (ad == null) { return(View(ad)); } try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { if (ModelState.IsValid && ad.Id != null) { CloudBlockBlob imageBlob = null; if (imageFile != null && imageFile.ContentLength != 0) { // User is changing the image -- delete the existing // image blobs and then upload and save a new one. await DeleteAdBlobsAsync(ad); imageBlob = await AzureConfig.UploadAndSaveBlobAsync(imageFile); ad.ImageUrl = imageBlob.Uri.ToString(); } await dataApiClient.Ads.PutAdAsync(ad.Id.Value, ad); await AzureConfig.AddAdBlobToQueueAsync(ad.Id, imageBlob); return(RedirectToAction("Index")); } await InitCategoriesAsync(dataApiClient, ad.CategoryId); return(View(ad)); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } catch (HttpOperationException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }
public async Task <ActionResult> Create( [Bind(Include = "Id,Title,Description,CategoryId,Price,Phone,Posted")] Ad ad, HttpPostedFileBase imageFile) { try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { if (ModelState.IsValid) { CloudBlockBlob imageBlob = null; if (imageFile != null && imageFile.ContentLength != 0) { imageBlob = await AzureConfig.UploadAndSaveBlobAsync(imageFile); ad.ImageUrl = imageBlob.Uri.ToString(); } ad.Posted = DateTime.Now; ad = await dataApiClient.Ads .PostAdAsync(ad) .ConfigureAwait(false); _logger.Log($"Created Ad (Id={ad.Id}) in database"); await AzureConfig.AddAdBlobToQueueAsync(ad.Id, imageBlob); return(RedirectToAction("Index")); } await InitCategoriesAsync(dataApiClient, ad.CategoryId); return(View(ad)); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } catch (HttpOperationException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }
// Code is taken from // see https://docs.microsoft.com/en-us/azure/app-service-web/websites-dotnet-webjobs-sdk-get-started private async Task DeleteAdBlobsAsync(Ad ad) { if (!string.IsNullOrWhiteSpace(ad.ImageUrl)) { var blobUri = new Uri(ad.ImageUrl); await AzureConfig.DeleteAdBlobAsync(blobUri); } if (!string.IsNullOrWhiteSpace(ad.ThumbnailUrl)) { var blobUri = new Uri(ad.ThumbnailUrl); await AzureConfig.DeleteAdBlobAsync(blobUri); } _logger.Log($"Deleted ad {ad.Id}"); }